From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by sourceware.org (Postfix) with ESMTPS id 92D8338930F3 for ; Thu, 16 Jul 2020 18:54:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 92D8338930F3 Received: by mail-io1-xd36.google.com with SMTP id v8so7153608iox.2 for ; Thu, 16 Jul 2020 11:54:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:from:date:message-id :subject:to:content-transfer-encoding; bh=CMFoxx9PKlod6BzDjzTulpCPBPNoNjigD0qQOorBDWM=; b=oh2QgxBayuCmY1K/wa3uzOycUxDV7QD5S+9mbEt9DW55LGtfECakBlUxQbf6UFDIsU kIfdbGln4UkbmFo2r5rg7ZWHdI580/kgF1woazBaJUBsdcgJB7pCxaCuaZg9DkKAlVhk kJCBJqoY4bFe0l5vi2itV6xA8PYuPx0AjesYZFOkWL7BIcI6XqJxfFC4tgRCXDptPxgC TYLncgwBGjJKu4F/O+Boqy2T5QURZX0zxIxpdxj+BYKJBbHGfKqbVCS1bAT6/PaCyQD2 gx1NDw5Do61GldLF2jGx0h3gyxrVwcPeM/I/mHDVw2qncXK+pPZqyV+uJye+/t8hxUoU 3xsw== X-Gm-Message-State: AOAM533IOQGl48XcWpGGuv6as0gtRfNsSWHBZMDgn+7JBxc47le/32s/ Tu4IiC8lUohQenp/JHVtlGRndHwtywwhVAnWtmxqvrfl X-Google-Smtp-Source: ABdhPJzZSCU3OkrHii/XTonVgSWSPupOp2oLBYDBFcDmQUqtGWnFSaUL7jh5CoKTvaerm1xyc3d+6C3uhK6Q5H6V/08= X-Received: by 2002:a5d:8552:: with SMTP id b18mr5954360ios.28.1594925645652; Thu, 16 Jul 2020 11:54:05 -0700 (PDT) MIME-Version: 1.0 Reply-To: noloader@gmail.com From: Jeffrey Walton Date: Thu, 16 Jul 2020 14:53:55 -0400 Message-ID: Subject: Analyzer memory leak finding To: gcc-help Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Jul 2020 18:54:07 -0000 Hi Everyone, I'm using GCC 10.1 on Fedora 32, x86_64, fully patched. I'm performing some analyzer builds using -fanalyzer. GCC is flagging a function as leaking memory, but it is not really. The function is returning a malloc'd pointer, but the pointer is aligned in the function. It may be a different pointer than the one returned by malloc, but it is in the same malloc'd block. The function and analyzer complaint is below. How can I sidestep the finding in this case? Thanks in advance. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D void * mmalloca (size_t n) { /* Allocate one more word, used to determine the address to pass to freea= (), and room for the alignment =E2=89=A1 sa_alignment_max mod 2*sa_alignme= nt_max. */ size_t nplus =3D n + sizeof (small_t) + 2 * sa_alignment_max - 1; if (nplus >=3D n) { char *mem =3D (char *) malloc (nplus); if (mem !=3D NULL) { char *p =3D (char *)((((uintptr_t)mem + sizeof (small_t) + sa_alignment_max= - 1) & ~(uintptr_t)(2 * sa_alignment_max - 1)) + sa_alignment_max); /* Here p >=3D mem + sizeof (small_t), and p <=3D mem + sizeof (small_t) + 2 * sa_alignment_max - 1 hence p + n <=3D mem + nplus. So, the memory range [p, p+n) lies in the allocated memory ran= ge [mem, mem + nplus). */ ((small_t *) p)[-1] =3D p - mem; /* p =E2=89=A1 sa_alignment_max mod 2*sa_alignment_max. */ return p; } } /* Out of memory. */ return NULL; } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D malloca.c:76:11: warning: leak of =E2=80=98mem=E2=80=99 [CWE-401] [-Wanalyz= er-malloc-leak] 76 | return p; | ^~~~~~ =E2=80=98mmalloca=E2=80=99: events 1-7 | | 59 | if (nplus >=3D n) | | ^ | | | | | (1) following =E2=80=98true=E2=80=99 branch (when =E2=80= =98n <=3D nplus=E2=80=99)... | 60 | { | 61 | char *mem =3D (char *) malloc (nplus); | | ~~~~ | | | | | (2) ...to here | | (3) allocated here | 62 | | 63 | if (mem !=3D NULL) | | ~ | | | | | (4) assuming =E2=80=98mem=E2=80=99 is non-NULL | | (5) following =E2=80=98true=E2=80=99 branch (when =E2= =80=98mem=E2=80=99 is non-NULL)... | 64 | { | 65 | char *p =3D | | ~~~~ | | | | | (6) ...to here |...... | 76 | return p; | | ~~~~~~ | | | | | (7) =E2=80=98mem=E2=80=99 leaks here; was allocated = at (3)