Silence -O3 -Wall warning in malloc/hooks.c with GCC 7 [BZ #22052]
Commit Message
On Mon, Sep 18, 2017 at 7:02 AM, Florian Weimer <fweimer@redhat.com> wrote:
> On 09/04/2017 10:29 PM, H.J. Lu wrote:
>>
>> On Mon, Sep 4, 2017 at 1:33 AM, Florian Weimer <fweimer@redhat.com> wrote:
>>>
>>> On 09/03/2017 05:48 PM, H.J. Lu wrote:
>>>>
>>>> + unsigned char *magic_p = NULL;
>>>
>>>
>>> I think the current practice is to use <libc-diag.h> to suppress the
>>> warning.
>>
>>
>> Is there a usage example for this warning?
>
>
> Search for DIAG_PUSH_NEEDS_COMMENT in the sources.
>
Here is a patch. Tested on x86-64. I am going to check it in.
Thanks.
From aca3e9b42f9c172af368f2fc54d2c7299b231cc0 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sun, 3 Sep 2017 08:39:55 -0700
Subject: [PATCH] Silence -O3 -Wall warning in malloc/hooks.c with GCC 7 [BZ
#22052]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
realloc_check has
unsigned char *magic_p;
...
__libc_lock_lock (main_arena.mutex);
const mchunkptr oldp = mem2chunk_check (oldmem, &magic_p);
__libc_lock_unlock (main_arena.mutex);
if (!oldp)
malloc_printerr ("realloc(): invalid pointer");
...
if (newmem == NULL)
*magic_p ^= 0xFF;
with
static void malloc_printerr(const char *str) __attribute__ ((noreturn));
GCC 7 -O3 warns
hooks.c: In function ‘realloc_check’:
hooks.c:352:14: error: ‘magic_p’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
*magic_p ^= 0xFF;
This patch silences GCC 7 by using DIAG_IGNORE_NEEDS_COMMENT.
[BZ #22052]
* malloc/hooks.c (realloc_check): Use DIAG_IGNORE_NEEDS_COMMENT
to silence -O3 -Wall warning GCC 7.
---
malloc/hooks.c | 7 +++++++
1 file changed, 7 insertions(+)
@@ -345,11 +345,18 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
newmem = _int_realloc (&main_arena, oldp, oldsize, nb);
}
+ DIAG_PUSH_NEEDS_COMMENT;
+#if __GNUC_PREREQ (7, 0)
+ /* GCC 7 warns about magic_p may be used uninitialized. But we never
+ reach here if magic_p is uninitialized. */
+ DIAG_IGNORE_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
+#endif
/* mem2chunk_check changed the magic byte in the old chunk.
If newmem is NULL, then the old chunk will still be used though,
so we need to invert that change here. */
if (newmem == NULL)
*magic_p ^= 0xFF;
+ DIAG_POP_NEEDS_COMMENT;
__libc_lock_unlock (main_arena.mutex);
--
2.13.5