From patchwork Thu Mar 3 11:24:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 51529 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 46D723857C56 for ; Thu, 3 Mar 2022 11:25:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46D723857C56 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1646306719; bh=r53k1HoCpocWc/DVvNqAZqt/i2JD88unc0PDIYYGxWM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=g6gqBTH0FU2T6xFRiYLS5bTEa0vY4qhM6rZRPGRtPQgdHsenypG8TUS8mdIS/5VK8 Fr/IZTmtJQM6iP3Gn5PvpUtx8nQdp/+0FVdCf1rfnXolSVOVy0Sp4xrhO2GN1wUUWX jr0CrTZ55lLlRa6wu7WkmEwxvF+NaueQOlqeKcH4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id AE6453858D39 for ; Thu, 3 Mar 2022 11:24:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AE6453858D39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-651-KfuwU-4pMXGkDpLHPPLYtA-1; Thu, 03 Mar 2022 06:24:51 -0500 X-MC-Unique: KfuwU-4pMXGkDpLHPPLYtA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D11A81091DA2; Thu, 3 Mar 2022 11:24:50 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.88]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EDF4B78872; Thu, 3 Mar 2022 11:24:30 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] malloc: Exit early on test failure in tst-realloc Date: Thu, 03 Mar 2022 12:24:29 +0100 Message-ID: <87v8wvffde.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Cc: Martin Sebor , Joseph Myers Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This addresses more (correct) use-after-free warnings reported by GCC 12 on some targets. Fixes commit c094c232eb3246154265bb035182f92fe1b17ab8 ("Avoid -Wuse-after-free in tests [BZ #26779]."). (This should fix all the remaining build-many-glibcs.py failures with GCC 12, with the exception of the sh4 ICE.) Reviewed-by: Siddhesh Poyarekar --- malloc/tst-realloc.c | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c index 8ce59f2602..5eb62a770f 100644 --- a/malloc/tst-realloc.c +++ b/malloc/tst-realloc.c @@ -20,15 +20,7 @@ #include #include #include - -static int errors = 0; - -static void -merror (const char *msg) -{ - ++errors; - printf ("Error: %s\n", msg); -} +#include static int do_test (void) @@ -51,11 +43,11 @@ do_test (void) save = errno; if (p != NULL) - merror ("realloc (NULL, -1) succeeded."); + FAIL_EXIT1 ("realloc (NULL, -1) succeeded."); /* errno should be set to ENOMEM on failure (POSIX). */ if (p == NULL && save != ENOMEM) - merror ("errno is not set correctly"); + FAIL_EXIT1 ("errno is not set correctly"); errno = 0; @@ -64,18 +56,18 @@ do_test (void) save = errno; if (p == NULL) - merror ("realloc (NULL, 10) failed."); + FAIL_EXIT1 ("realloc (NULL, 10) failed."); free (p); p = calloc (20, 1); if (p == NULL) - merror ("calloc (20, 1) failed."); + FAIL_EXIT1 ("calloc (20, 1) failed."); /* Check increasing size preserves contents (C89). */ p = realloc (p, 200); if (p == NULL) - merror ("realloc (p, 200) failed."); + FAIL_EXIT1 ("realloc (p, 200) failed."); c = p; ok = 1; @@ -87,20 +79,20 @@ do_test (void) } if (ok == 0) - merror ("first 20 bytes were not cleared"); + FAIL_EXIT1 ("first 20 bytes were not cleared"); free (p); p = realloc (NULL, 100); if (p == NULL) - merror ("realloc (NULL, 100) failed."); + FAIL_EXIT1 ("realloc (NULL, 100) failed."); memset (p, 0xff, 100); /* Check decreasing size preserves contents (C89). */ p = realloc (p, 16); if (p == NULL) - merror ("realloc (p, 16) failed."); + FAIL_EXIT1 ("realloc (p, 16) failed."); c = p; ok = 1; @@ -112,7 +104,7 @@ do_test (void) } if (ok == 0) - merror ("first 16 bytes were not correct"); + FAIL_EXIT1 ("first 16 bytes were not correct"); /* Check failed realloc leaves original untouched (C89). */ DIAG_PUSH_NEEDS_COMMENT; @@ -124,7 +116,7 @@ do_test (void) c = realloc (p, -1); DIAG_POP_NEEDS_COMMENT; if (c != NULL) - merror ("realloc (p, -1) succeeded."); + FAIL_EXIT1 ("realloc (p, -1) succeeded."); c = p; ok = 1; @@ -136,29 +128,21 @@ do_test (void) } if (ok == 0) - merror ("first 16 bytes were not correct after failed realloc"); + FAIL_EXIT1 ("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."); + FAIL_EXIT1 ("realloc (p, 0) returned non-NULL."); /* realloc (NULL, 0) acts like malloc (0) (glibc). */ p = realloc (NULL, 0); if (p == NULL) - merror ("realloc (NULL, 0) returned NULL."); + FAIL_EXIT1 ("realloc (NULL, 0) returned NULL."); free (p); - return errors != 0; + return 0; } #define TEST_FUNCTION do_test ()