From patchwork Thu Jun 23 12:54:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 13323 Received: (qmail 107338 invoked by alias); 23 Jun 2016 12:54:51 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 107329 invoked by uid 89); 23 Jun 2016 12:54:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:1280, our, 2016-06-23 X-HELO: mx1.redhat.com Subject: Re: [PATCH] test-skeleton.c (xrealloc): Support realloc-as-free To: libc-alpha@sourceware.org References: <20160623122346.877DC4027F857@oldenburg.str.redhat.com> <20160623124524.GV24532@vapier.lan> From: Florian Weimer Message-ID: <4b83d238-aedc-f8c9-c324-35871ddaffc9@redhat.com> Date: Thu, 23 Jun 2016 14:54:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: <20160623124524.GV24532@vapier.lan> On 06/23/2016 02:45 PM, Mike Frysinger wrote: > On 23 Jun 2016 14:23, Florian Weimer wrote: >> If the requested size is zero, realloc returns NULL, but the >> deallocation is still successful. > > not exactly. realloc(NULL, 0) will return a valid pointer (much > like malloc(NULL) will). code relies on this to generate cookies. Ugh, right. At least our malloc behaves in this way. What about the attached patch? Thanks, Florian test-skeleton.c (xrealloc): Support realloc-as-free If the requested size is zero, realloc returns NULL, but the deallocation is still successful, unless the pointer is also NULL, when realloc behaves as malloc (0). 2016-06-23 Florian Weimer * test-skeleton.c (xrealloc): Support deallocation with n == 0. diff --git a/test-skeleton.c b/test-skeleton.c index 0be4af1..d9bf989 100644 --- a/test-skeleton.c +++ b/test-skeleton.c @@ -109,10 +109,10 @@ __attribute__ ((unused)) static void * xrealloc (void *p, size_t n) { - p = realloc (p, n); - if (p == NULL) + void *result = realloc (p, n); + if (result == NULL && (n > 0 || p == NULL)) oom_error ("realloc", n); - return p; + return result; } /* Write a message to standard output. Can be used in signal