From patchwork Thu Oct 1 15:18:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dishu305 X-Patchwork-Id: 145107 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4CDF84BA23CD for ; Thu, 1 Oct 2026 15:19:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CDF84BA23CD Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=D2FhRW2N X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj2-x0d.google.com (mail-pj2-x0d.google.com [IPv6:2607:f8b0:4864:39::d]) by sourceware.org (Postfix) with ESMTPS id E47F64BA2E35 for ; Thu, 1 Oct 2026 15:19:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E47F64BA2E35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E47F64BA2E35 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:39::d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790867945; cv=none; b=D8oFbc8lxWeqHZ30ba0J/Q8mG+6iAcjbRFbuTREYNhrYpM517RCiYYl9AR/bLEZTiFVshOp2tufO5UxaUcyGbQgrT3MsdhONh3nge+wXh9vit6hpwKeyfqrBL4K8+ghL8925NOAxitAR8tv3wkdVoqbFdqmII5yZfuFG9bJFXj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790867945; c=relaxed/simple; bh=YILxrqjtTkBYSqbccUC/BMFWg9Zcv1sz4++oPW5LaFU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UECV2y66ofRp5j/BPYw3vHZLyVDR7VFlBz8ht0XKNxGKM43tv/Mr5dBVVWqX/dg5yua+9dzj6G0vgKOWCIYk9haxhvj4i6Mu9rXYAxapWu5sl8Q10s0XhX60Jpm8in3dOolxCfQ8Nuf/xbWFLzPHfdicHkFVTIL5JFdQv9MKMCo= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=D2FhRW2N DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E47F64BA2E35 Received: by mail-pj2-x0d.google.com with SMTP id 98e67ed59e1d1-3a0aa9d356eso5041776a91.0 for ; Thu, 01 Oct 2026 08:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790867944; x=1791472744; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=/CRRSSbiIOUxfNK4NMiZbpN+FzBnp/B1TqaJq3eIsaA=; b=D2FhRW2NSUL+OerOdmZi0Zz1JjUSZJgSSq3EXN82n6DICRmAy/NmDNbhvmS9eLpTl3 7i6mAd8BObmXWD7AILauWj4XoNlIs+BdjS68Jpfax1ONhCTTuqad9+jH8rgh67p7rYNC GVw/hOvqQZdoRPJ8g23KpzJ9uwJ6MtFI+p8h1T90nS0JhOWHVBhxnO4O5OTmC8CWtNmO Tp/mBwKJb5fjAgDod+ojsV0XH1qZcCpEybTv/Sf+R9nSKkVfEAfvYT+GuzsjyzEtXorx QiytUDwBVPtsk1IGJplyYeQGGS6dYayeBLgw1KgxYGoBYiju/WqIFfQhfqVI/0yLPPze d8gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790867944; x=1791472744; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/CRRSSbiIOUxfNK4NMiZbpN+FzBnp/B1TqaJq3eIsaA=; b=zvjNr7zlAqqveDJXaXDxor1Krbu/ailefV+u/7zuSen9XBJq11NCBvYHtEQEqVQj+w lVQvijqjpMXnk67cQ/8Th9Wl4RUJx0QIwpz0dF+doxq135wrrVJTBRilXNnejgrjcge0 pe5LmcL97Vm5+gvoqPPk0ULCzX9F35YU9LL+z9vXng+1i1ti0+EdMROJZcuz/c51hH7e ATYYrlyNY6TvuIt/XEdty3hvp6YLbEBG7EVJOaZRD2DnCbp8Y+aHwPZgU2wdy2eYaA5z rdjwkXSY3lJfmXy8wEFUH+otAGfPXa4XovQioS/3TOUlOIgCch+QcKcuj/Ui4HMMpjeV Re1Q== X-Gm-Message-State: AFq9FYIvtcCyk/ty1KCnEGChKhK31cwGjW9ScUzuUMEFtWVZ9tflRfJu oUikVgBviH4c935YGggTGDIuhGNvnGI3VGcTw0xuZj2VSZ5FU9ybSSae4bKkhw== X-Gm-Gg: AYBFou1BcLyLRBreIRkqwuLTicDOiDavSeAnrnho2Y7Yw4lxRAUPtoQbUFivHONdK+g D6foUg9hIIJ8mKqcstmWf/bFZQm+v5lAiJI52UdxRe8iPcG3hbIhESQooixT9xG3WsCW8YO6Cz2 jr+G5i1oeIjh9okbIx53OAUwU0fmSs5Smg2TiU81pfNhSKlManoWjhPki5nZkjm5JL6RxIgXTft fXpwQUIbv7PQLXRmAXibh7r5koUT5jVOPCZIOS88L+vDwPjVzcL5jyquc+RVYrbyrxvtnZCuNDI v6PA4W6BpBBpxhjP+XDfhhuI5gxqbhThGS/lUa2KVGozIgiUqp16lQbU+hh3ekdixM99DNid/iM FuEZnNSkCLLoiX9sddHsYIi6ypFn97Aam0hCriXVNGiLdTDpu5s3ldt+ko4s7ePgfj1q8ultW2W hP8cTZvsMqFJ8n39r8vNSXqO3kdYXFFDl2XV7ZvuzHe3nsJpJGwyMhWsqfbfac6rzTF3YkASlMU Cxj5HH4AbEv8sUy4YgNBkuywsURWBdgSjc= X-Received: by 2002:a17:90b:3b8b:b0:3a0:7cbf:b8cd with SMTP id 98e67ed59e1d1-3a4d1526e6dmr4539363a91.14.1790867943615; Thu, 01 Oct 2026 08:19:03 -0700 (PDT) Received: from DESKTOP-AGKU5VJ.localdomain ([152.56.66.186]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a6cd44c2b2sm54537a91.5.2026.10.01.08.19.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Oct 2026 08:19:02 -0700 (PDT) From: dishu305 To: gcc-patches@gcc.gnu.org Cc: dishu305 Subject: [PATCH] analyzer: add support for GLib memory functions Date: Thu, 1 Oct 2026 15:18:51 +0000 Message-ID: <20261001151851.5807-1-dishusangwan04@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_XBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org This patch teaches the GCC static analyzer to recognize GLib's memory allocation and deallocation functions, preventing false negatives for memory leaks in GTK/GNOME applications. gcc/analyzer/ChangeLog: * sm-malloc.cc (known_allocator_p): Add checks for g_malloc, g_malloc0, g_realloc, g_new, and g_new0. (get_deallocator): Add check for g_free. (malloc_state_machine::on_call): Add check for g_free. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/glib-malloc-leak.c: New test. Signed-off-by: dishu305 --- gcc/analyzer/sm-malloc.cc | 13 +++++++--- .../gcc.dg/analyzer/glib-malloc-leak.c | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/glib-malloc-leak.c diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index 72e6e28e2..ff8e19f0b 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -1964,7 +1964,8 @@ malloc_state_machine::get_or_create_deallocator (tree deallocator_fndecl) deallocator *d; if (is_named_call_p (deallocator_fndecl, "free") || is_std_named_call_p (deallocator_fndecl, "free") - || is_named_call_p (deallocator_fndecl, "__builtin_free")) + || is_named_call_p (deallocator_fndecl, "__builtin_free") + || is_named_call_p (deallocator_fndecl, "g_free")) d = &m_free.m_deallocator; else { @@ -2006,7 +2007,12 @@ known_allocator_p (const_tree fndecl, const gcall &call) || is_std_named_call_p (fndecl, "malloc", call, 1) || is_std_named_call_p (fndecl, "calloc", call, 2) || is_named_call_p (fndecl, "strdup", call, 1) - || is_named_call_p (fndecl, "strndup", call, 2)) + || is_named_call_p (fndecl, "strndup", call, 2) + || is_named_call_p (fndecl, "g_malloc", call, 1) + || is_named_call_p (fndecl, "g_malloc0", call, 1) + || is_named_call_p (fndecl, "g_realloc", call, 2) + || is_named_call_p (fndecl, "g_new", call, 2) + || is_named_call_p (fndecl, "g_new0", call, 2)) return true; /* ... or it is a builtin allocator that allocates objects freed with @@ -2161,7 +2167,8 @@ malloc_state_machine::on_stmt (sm_context &sm_ctxt, if (is_named_call_p (callee_fndecl, "free", call, 1) || is_std_named_call_p (callee_fndecl, "free", call, 1) - || is_named_call_p (callee_fndecl, "__builtin_free", call, 1)) + || is_named_call_p (callee_fndecl, "__builtin_free", call, 1) + || is_named_call_p (callee_fndecl, "g_free", call, 1)) { on_deallocator_call (sm_ctxt, call, &m_free.m_deallocator, 0); diff --git a/gcc/testsuite/gcc.dg/analyzer/glib-malloc-leak.c b/gcc/testsuite/gcc.dg/analyzer/glib-malloc-leak.c new file mode 100644 index 000000000..0a5622c1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/glib-malloc-leak.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ +/* { dg-options "-fanalyzer" } */ + +/* Test that the analyzer recognizes g_malloc and g_free. + It should report a leak for ptr1, but not for ptr2. */ + +extern void *g_malloc (unsigned long); +extern void g_free (void *); + +void test_leak (void) +{ + void *ptr1 = g_malloc (100); /* { dg-warning "leak of 'ptr1'" } */ +} + +void test_no_leak (void) +{ + void *ptr2 = g_malloc (200); + g_free (ptr2); +} + +int main (void) +{ + test_leak (); + test_no_leak (); + return 0; +}