From patchwork Tue Jan 25 00:57:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 50422 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 50B153858005 for ; Tue, 25 Jan 2022 00:58:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50B153858005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1643072285; bh=+a1pk9gI3QCxZ+SQf8nxmI/mVkGs6nYXDg/mUed3+30=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=n36uQ5cygKA247t7sfrMRpI4jc4H2qLnVRhPjz18+sFvhJ4M7HmXYNWU5abhJOEZA 0f1XcFI4FtOKd7txnWpe3hE6gTykPBXQonJyRtrXT4a03897vVP08VxPangGtPSGV9 TF/MIQKJ7OD57sG66ioB+CIzMB7q9MQORxkHb614= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 7E4C03858437 for ; Tue, 25 Jan 2022 00:57:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E4C03858437 Received: by mail-il1-x131.google.com with SMTP id i14so15460030ila.11 for ; Mon, 24 Jan 2022 16:57:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:from:to:references:in-reply-to; bh=+a1pk9gI3QCxZ+SQf8nxmI/mVkGs6nYXDg/mUed3+30=; b=mAyfeE/vohPKB2uQiSuh0J6mqWnZ3UqfihM8jwYeXc16Kiq3qADCSmRJgAoD40swjP J5ca44aUKcD0iKTskFQ13LP8hyVWdrwPpOVpbt+k9PwdLqYIoHL4Nx+33qiAQZ95SbdP ylBa2RKsWwSf9rYpnG0tq5GaBdG5d7+MqNQ4o/YGN1ubXyh8xtrxirdtcAStnHrR6J1i MTxaWTtK+XxJAyPlLTpLS6DFhWITvrJZaKXk9YSC34W4FX8iYh5Wy4eSomxDAxYUhHjv 2ZBrEdpGWqf+wf7Hw9+cRiEht9A8mKpB/uGZZDQt4ODIpLwNAKjx7U/kuT0iTvRxZzXX jNVA== X-Gm-Message-State: AOAM532fA6dIOqD5MVwxKCyJJSvn4MFzN2W1RMyAVM8kMcfV6rHJG8RO clPettAn+9OEO7xxYvT0XuKiR5Q+9EE= X-Google-Smtp-Source: ABdhPJzXgpNUjHm2AzmrMpuq1ZIn29bUOr86b5rKJcPzZIlJPk8ZBl+WO3vLzyqkp7cAwUCVAKpyaA== X-Received: by 2002:a05:6e02:1685:: with SMTP id f5mr8416995ila.308.1643072262718; Mon, 24 Jan 2022 16:57:42 -0800 (PST) Received: from [192.168.0.41] (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.gmail.com with ESMTPSA id l12sm7600742ios.32.2022.01.24.16.57.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jan 2022 16:57:42 -0800 (PST) Message-ID: <134bf8c7-51ac-2615-7ce3-a144a5fd315f@gmail.com> Date: Mon, 24 Jan 2022 17:57:41 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: [PATCH v2 1/5] avoid -Wuse-after-free [BZ #26779] Content-Language: en-US To: libc-alpha@sourceware.org References: <53f20975-a2c9-674d-2a43-b1b323ee545c@gmail.com> In-Reply-To: <53f20975-a2c9-674d-2a43-b1b323ee545c@gmail.com> X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Libc-alpha From: Martin Sebor Reply-To: Martin Sebor Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" On 1/24/22 17:52, Martin Sebor wrote: > This is a repost of the original patch but broken down by source > file and with some suppression done by #pragma GCC diagnostic > instead of conversion to intptr_t.  It also adds fixes for > the same problem in the test suite that I overlooked before. The attached patch suppresses the -Wuse-after-free instance in elf/ldconfig.c. > > On 1/15/22 17:21, Martin Sebor wrote: >> GCC 12 features a couple of new warnings designed to detect uses >> of pointers made invalid by the pointees lifetimes having ended. >> Building Glibc with the enhanced GCC exposes a few such uses, >> mostly after successful calls to realloc.  The attached patch >> avoids the new warnings by converting the pointers to uintptr_t >> first and using the converted integers instead. >> >> The patch suppresses all instances of the warning at the strictest >> setting (-Wuse-after-free=3), which includes even uses in equality >> expressions.  The default setting approved for GCC 12 is >> -Wuse-after-free=2, which doesn't warn on such uses to accommodate >> the pointer-adjustment-after-realloc idiom.  At the default setting, >> the changes to ldconfig.c and setenv are not necessary. >> >> Martin > Reviewed-by: Carlos O'Donell Reviewed-by: Carlos O'Donell diff --git a/elf/ldconfig.c b/elf/ldconfig.c index d14633f5ec..57bb95ebc3 100644 --- a/elf/ldconfig.c +++ b/elf/ldconfig.c @@ -735,9 +735,9 @@ manual_link (char *library) create_links (real_path, path, libname, soname); free (soname); out: - free (path); if (path != real_path) free (real_path); + free (path); } From patchwork Tue Jan 25 22:50:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 50444 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2EDC6385EC52 for ; Tue, 25 Jan 2022 22:50:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2EDC6385EC52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1643151050; bh=NWxROCxMwFzFlLJZi8d/rw+J6E9ePhHVb61yPyI6GIY=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=sb1hcCOV3hhZvrTfqaA2tnyclH9L1+oBMwaW6E8X8VHfi0qQr4LdB3OwFTN5A2+Tf ko/39P4Ws+hhtX10t2FKvOBaefqG+fmlZX87USKvSBX9PjLCxC1pjvIs0SbD5jYmBT SXsRiwIDGs0gqjLKBc+/U9flXNkQ/AdrRL4H/lKE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 125AA385DC27 for ; Tue, 25 Jan 2022 22:50:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 125AA385DC27 Received: by mail-il1-x131.google.com with SMTP id 15so3852715ilg.8 for ; Tue, 25 Jan 2022 14:50:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=NWxROCxMwFzFlLJZi8d/rw+J6E9ePhHVb61yPyI6GIY=; b=DeWmZk7JpiSjrjG532VuNQgUAUDjFise4E8/Qd18rOpeZE4ZL5u+R3oyzTGfhm0+mR +EAZkgpHcENNcJFcBEfNFBR3LVWfLa/2nB2G3YndyYFW5f+Hg5RJzlzz50Wr1GZjyogj 8rE4/ok0umL5EwGN+CYaSUjDE6kHjDsNCwVRo2aje3iExdCOT62nRb89cFeRgXu3IwlO +CBz8u3C4rdX7N8+4DZfrGktzF8+YYje1HIIG6IHM/AtVpZsIhsxnN384GB0z+fs5H60 0KvluxeVjLq13XaEC70uqxWXyt9L7rpKtrHelckbVPaJpX12Ug2BWeSMJhByl+d8ZwmJ KR7Q== X-Gm-Message-State: AOAM530XBnILavaB0oClgI3Yz7kmucCVs+XVQ9SfRAaSBYeRB51UvDWa 6tGoTN3wFFfzqw1MZ7wHhDv2jDnYaCo= X-Google-Smtp-Source: ABdhPJzHzH2qXkRwkKeKQuGkCmXP5PgQEOrGiOQkI7LBm7gV+m9ODZ8XEjiTrt3WRsH+STjlPoHP5g== X-Received: by 2002:a92:c567:: with SMTP id b7mr13429354ilj.20.1643151027359; Tue, 25 Jan 2022 14:50:27 -0800 (PST) Received: from [192.168.0.41] (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.gmail.com with ESMTPSA id j2sm9540215ilu.2.2022.01.25.14.50.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Jan 2022 14:50:26 -0800 (PST) Message-ID: <3ad704bb-a061-1dc2-3b6d-8343f70a3c92@gmail.com> Date: Tue, 25 Jan 2022 15:50:26 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: [PATCH v3 5/5] avoid -Wuse-after-free [BZ #26779] Content-Language: en-US To: Carlos O'Donell , libc-alpha@sourceware.org References: <53f20975-a2c9-674d-2a43-b1b323ee545c@gmail.com> <50baa5fb-c1ee-f7dc-b7d6-cf4587782062@gmail.com> In-Reply-To: X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Libc-alpha From: Martin Sebor Reply-To: Martin Sebor Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" On 1/25/22 10:49, Carlos O'Donell wrote: > On 1/24/22 19:58, Martin Sebor via Libc-alpha wrote: >> On 1/24/22 17:52, Martin Sebor wrote: >>> This is a repost of the original patch but broken down by source >>> file and with some suppression done by #pragma GCC diagnostic >>> instead of conversion to intptr_t.  It also adds fixes for >>> the same problem in the test suite that I overlooked before. >> >> The attached patch suppresses the -Wuse-after-free instance in >> the testsuite. >> >>> >>> On 1/15/22 17:21, Martin Sebor wrote: >>>> GCC 12 features a couple of new warnings designed to detect uses >>>> of pointers made invalid by the pointees lifetimes having ended. >>>> Building Glibc with the enhanced GCC exposes a few such uses, >>>> mostly after successful calls to realloc.  The attached patch >>>> avoids the new warnings by converting the pointers to uintptr_t >>>> first and using the converted integers instead. >>>> >>>> The patch suppresses all instances of the warning at the strictest >>>> setting (-Wuse-after-free=3), which includes even uses in equality >>>> expressions.  The default setting approved for GCC 12 is >>>> -Wuse-after-free=2, which doesn't warn on such uses to accommodate >>>> the pointer-adjustment-after-realloc idiom.  At the default setting, >>>> the changes to ldconfig.c and setenv are not necessary. >>>> >>>> Martin >>> > > This patch is not ready. > > Some tests are going to do invalid things to test specific behaviour and we need > to possibly suppress those warnings. The malloc tests look correct. > > The support/tst-support-open-dev-null-range.c doesn't look correct, please send v3 > of just this *whole* patch as a new patch. I'll review again. Okay, I've moved the free() call after the FAIL_EXIT. I've also suppressed the same warning in a few more tests that I missed before. Martin > >> diff --git a/malloc/tst-malloc-backtrace.c b/malloc/tst-malloc-backtrace.c >> index ea66da23ef..8a3f4a0b55 100644 >> --- a/malloc/tst-malloc-backtrace.c >> +++ b/malloc/tst-malloc-backtrace.c >> @@ -20,6 +20,7 @@ >> #include >> >> #include >> +#include > > OK. Add header required for DIAG_* macros. > >> >> #define SIZE 4096 >> >> @@ -29,7 +30,15 @@ __attribute__((noinline)) >> call_free (void *ptr) >> { >> free (ptr); >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a valid warning about using a pointer made indeterminate >> + by a prior call to malloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> *(size_t *)(ptr - sizeof (size_t)) = 1; >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. Specifically testing use-after-free write to chunk to corrupt memory. > >> } >> >> int >> diff --git a/malloc/tst-malloc-check.c b/malloc/tst-malloc-check.c >> index 46938c0dbb..eb46cf3bbb 100644 >> --- a/malloc/tst-malloc-check.c >> +++ b/malloc/tst-malloc-check.c > > OK. Already includes libc-diag.h. > >> @@ -86,7 +86,15 @@ do_test (void) >> merror ("errno is not set correctly."); >> DIAG_POP_NEEDS_COMMENT; >> >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a valid warning about using a pointer made indeterminate >> + by a prior call to realloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> free (p); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. Previous realloc made p indeterminate. > >> >> p = malloc (512); >> if (p == NULL) >> @@ -104,7 +112,15 @@ do_test (void) >> merror ("errno is not set correctly."); >> DIAG_POP_NEEDS_COMMENT; >> >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a valid warning about using a pointer made indeterminate >> + by a prior call to realloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> free (p); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. Likewise. > >> free (q); >> >> return errors != 0; >> diff --git a/malloc/tst-malloc-too-large.c b/malloc/tst-malloc-too-large.c >> index e23aa08e4f..dac3c8086c 100644 >> --- a/malloc/tst-malloc-too-large.c >> +++ b/malloc/tst-malloc-too-large.c > > OK. Already includes libc-diag.h. > >> @@ -95,7 +95,15 @@ test_large_allocations (size_t size) >> DIAG_POP_NEEDS_COMMENT; >> #endif >> TEST_VERIFY (errno == ENOMEM); >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a warning about using a pointer made indeterminate by >> + a prior call to realloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> free (ptr_to_realloc); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. > >> >> for (size_t nmemb = 1; nmemb <= 8; nmemb *= 2) >> if ((size % nmemb) == 0) >> @@ -113,14 +121,30 @@ test_large_allocations (size_t size) >> test_setup (); >> TEST_VERIFY (reallocarray (ptr_to_realloc, nmemb, size / nmemb) == NULL); >> TEST_VERIFY (errno == ENOMEM); >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a warning about using a pointer made indeterminate by >> + a prior call to realloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> free (ptr_to_realloc); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. > >> >> ptr_to_realloc = malloc (16); >> TEST_VERIFY_EXIT (ptr_to_realloc != NULL); >> test_setup (); >> TEST_VERIFY (reallocarray (ptr_to_realloc, size / nmemb, nmemb) == NULL); >> TEST_VERIFY (errno == ENOMEM); >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a warning about using a pointer made indeterminate by >> + a prior call to realloc(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> free (ptr_to_realloc); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif > > OK. > >> } >> else >> break; >> diff --git a/support/tst-support-open-dev-null-range.c b/support/tst-support-open-dev-null-range.c >> index 3ed3177d57..e7526597ce 100644 >> --- a/support/tst-support-open-dev-null-range.c >> +++ b/support/tst-support-open-dev-null-range.c >> @@ -26,6 +26,8 @@ >> #include >> #include >> >> +#include > > OK. New macros required. > >> + >> #ifndef PATH_MAX >> # define PATH_MAX 1024 >> #endif >> @@ -41,8 +43,18 @@ check_path (int fd) >> = readlink (proc_fd_path, file_path, sizeof (file_path)); >> free (proc_fd_path); >> if (file_path_length < 0) >> - FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path, >> - sizeof (file_path)); >> + { >> +#if __GNUC_PREREQ (12, 0) >> + /* Ignore a valid warning about using a pointer made indeterminate >> + by a prior call to free(). */ >> + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); >> +#endif >> + FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path, >> + sizeof (file_path)); >> +#if __GNUC_PREREQ (12, 0) >> + DIAG_POP_NEEDS_COMMENT; >> +#endif >> + } > > We should move free (proc_fd_path) to after the check to correct the use-after-free. > >> file_path[file_path_length] = '\0'; >> TEST_COMPARE_STRING (file_path, "/dev/null"); >> } > > Reviewed-by: Carlos O'Donell commit c23cf7ff784186b8094b97a47a8445808145a69c Author: Martin Sebor Date: Tue Jan 25 15:39:38 2022 -0700 Avoid -Wuse-after-free in tests. diff --git a/malloc/tst-malloc-backtrace.c b/malloc/tst-malloc-backtrace.c index ea66da23ef..65e1a1ffbc 100644 --- a/malloc/tst-malloc-backtrace.c +++ b/malloc/tst-malloc-backtrace.c @@ -20,6 +20,7 @@ #include #include +#include #define SIZE 4096 @@ -29,7 +30,15 @@ __attribute__((noinline)) call_free (void *ptr) { free (ptr); +#if __GNUC_PREREQ (12, 0) + /* Ignore a valid warning about using a pointer made indeterminate + by a prior call to free(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif *(size_t *)(ptr - sizeof (size_t)) = 1; +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif } int diff --git a/malloc/tst-malloc-check.c b/malloc/tst-malloc-check.c index 46938c0dbb..eb46cf3bbb 100644 --- a/malloc/tst-malloc-check.c +++ b/malloc/tst-malloc-check.c @@ -86,7 +86,15 @@ do_test (void) merror ("errno is not set correctly."); DIAG_POP_NEEDS_COMMENT; +#if __GNUC_PREREQ (12, 0) + /* Ignore a valid warning about using a pointer made indeterminate + by a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif free (p); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif p = malloc (512); if (p == NULL) @@ -104,7 +112,15 @@ do_test (void) merror ("errno is not set correctly."); DIAG_POP_NEEDS_COMMENT; +#if __GNUC_PREREQ (12, 0) + /* Ignore a valid warning about using a pointer made indeterminate + by a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif free (p); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif free (q); return errors != 0; diff --git a/malloc/tst-malloc-too-large.c b/malloc/tst-malloc-too-large.c index e23aa08e4f..dac3c8086c 100644 --- a/malloc/tst-malloc-too-large.c +++ b/malloc/tst-malloc-too-large.c @@ -95,7 +95,15 @@ test_large_allocations (size_t size) DIAG_POP_NEEDS_COMMENT; #endif TEST_VERIFY (errno == ENOMEM); +#if __GNUC_PREREQ (12, 0) + /* Ignore a warning about using a pointer made indeterminate by + a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif free (ptr_to_realloc); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif for (size_t nmemb = 1; nmemb <= 8; nmemb *= 2) if ((size % nmemb) == 0) @@ -113,14 +121,30 @@ test_large_allocations (size_t size) test_setup (); TEST_VERIFY (reallocarray (ptr_to_realloc, nmemb, size / nmemb) == NULL); TEST_VERIFY (errno == ENOMEM); +#if __GNUC_PREREQ (12, 0) + /* Ignore a warning about using a pointer made indeterminate by + a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif free (ptr_to_realloc); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif ptr_to_realloc = malloc (16); TEST_VERIFY_EXIT (ptr_to_realloc != NULL); test_setup (); TEST_VERIFY (reallocarray (ptr_to_realloc, size / nmemb, nmemb) == NULL); TEST_VERIFY (errno == ENOMEM); +#if __GNUC_PREREQ (12, 0) + /* Ignore a warning about using a pointer made indeterminate by + a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif free (ptr_to_realloc); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif } else break; diff --git a/malloc/tst-obstack.c b/malloc/tst-obstack.c index 18af8ea62f..d80f471fa0 100644 --- a/malloc/tst-obstack.c +++ b/malloc/tst-obstack.c @@ -20,8 +20,8 @@ verbose_malloc (size_t size) static void verbose_free (void *buf) { - free (buf); printf ("free (%p)\n", buf); + free (buf); } static int diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c index 83f165516a..8ce59f2602 100644 --- a/malloc/tst-realloc.c +++ b/malloc/tst-realloc.c @@ -138,8 +138,16 @@ do_test (void) if (ok == 0) merror ("first 16 bytes were not correct after failed realloc"); +#if __GNUC_PREREQ (12, 0) + /* Ignore a valid warning about using a pointer made indeterminate + by a prior call to realloc(). */ + DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free"); +#endif /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */ p = realloc (p, 0); +#if __GNUC_PREREQ (12, 0) + DIAG_POP_NEEDS_COMMENT; +#endif if (p != NULL) merror ("realloc (p, 0) returned non-NULL."); diff --git a/support/tst-support-open-dev-null-range.c b/support/tst-support-open-dev-null-range.c index 3ed3177d57..690b9d30b7 100644 --- a/support/tst-support-open-dev-null-range.c +++ b/support/tst-support-open-dev-null-range.c @@ -39,10 +39,11 @@ check_path (int fd) char file_path[PATH_MAX]; ssize_t file_path_length = readlink (proc_fd_path, file_path, sizeof (file_path)); - free (proc_fd_path); if (file_path_length < 0) FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path, sizeof (file_path)); + + free (proc_fd_path); file_path[file_path_length] = '\0'; TEST_COMPARE_STRING (file_path, "/dev/null"); }