From patchwork Tue Jun 7 15:51:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 631695 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rPGK54lQVz9t3Z for ; Wed, 8 Jun 2016 01:51:53 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=H1VDD9Jq; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; q=dns; s=default; b=wZCjg6x cViQzgv991Ilkfw0pRr1XWMDJHqjGh9Xjzs+aRi9Xid4qljFd/uLzLL17zKyQQe0 YaHmUO2k/R4vcT7HTLsUpZiw0HXqpv+K+llCizeUJ6Vgo3Cdr/0JKI+lWTzpSA5d lYL+ZEc5SJ7L22q/YZmLAo3Jg635E9SCBXEU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; s=default; bh=OtJNmWfTSKePk 9/kyWEWHpExbwQ=; b=H1VDD9Jqu3kl1Q4X3JW01Amu1o27ifWMF89t9Jpa0TevG GQmkORZYkGrffpoxnzkDm9BiX7mm/o0bGBbubnGX2dzovIyj+WdZQ6Y+5tq1sl1V jl0G+19vTXn62V7RSjnz5TEt1okTH5AxdDK75YRws/SiZdGlh83oJr3uXbzaiA= Received: (qmail 59344 invoked by alias); 7 Jun 2016 15:51:45 -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 59324 invoked by uid 89); 7 Jun 2016 15:51:45 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=1289, our X-HELO: mail-vk0-f47.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=d5oEDrwy/VB814cSXVHxOJjP8KcSGfdsy5kI6eK0L3w=; b=i74AeEFrbc5yf0CJUUztJ0kTKnvtEO4PQpTo4domjLxVsKC2KaC2oRAKO4rYBCFUBe LJJIy9OKcSunDzSdKqX8daqXgt5T/xWSxotF0Y5uyZvgFvGq0nvaTheVRqcZrAHr1Rw2 AzYBbLinhOlQVH2ZpL2+bSfB4Y4sm2f3F2H1TXsfeg8cIsbwARe8hz/pcOE53yZCua/7 PUB1ME/KyVYOm92PlVtCgP+ZVBVUo0php1QyjMDh+RvvuG4cjwe5g2t2AX54clUG9e5S cAk0EY5hejjyGEcoWtCbZkIZ3HhaU8/vio+IcZpyJ6bb34o9NAHkU2wEI9SmgJYze/r6 /Mag== X-Gm-Message-State: ALyK8tI1F/C54UBXSUluyUaFW/KgKBK/pL48/PpDJzuboGA5ZfQzQmHpbF22V7Sy5QfzK6UG8rLVJoZOgzjPbS6J X-Received: by 10.176.7.7 with SMTP id h7mr55501uah.9.1465314700276; Tue, 07 Jun 2016 08:51:40 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20160606021231.GH5431@vapier.lan> References: <20160531220247.GC7717@vapier.lan> <20160606015434.GG5431@vapier.lan> <20160606021231.GH5431@vapier.lan> From: Paul Pluzhnikov Date: Tue, 7 Jun 2016 08:51:10 -0700 Message-ID: Subject: Re: Do we care about malloc failure in tests? To: Paul Pluzhnikov , GLIBC Devel , Bill Parker On Sun, Jun 5, 2016 at 7:12 PM, Mike Frysinger wrote: > On 05 Jun 2016 19:00, Paul Pluzhnikov wrote: >> I think I can also remove this from xrealloc, since our realloc does >> the right thing when p==NULL. >> >> + if (p == 0) >> + return xmalloc (n); > > right, we don't want/need that > > i think all the use of 0 should be changed to NULL too I'll submit attached patch tomorrow if there are no further comments. Thanks, 2016-06-07 Paul Pluzhnikov * test-skeleton.c (oom_error, xmalloc, xcalloc, xrealloc): New functions. (add_temp_file): Use them. diff --git a/test-skeleton.c b/test-skeleton.c index 29bdc9c..e091d7f 100644 --- a/test-skeleton.c +++ b/test-skeleton.c @@ -70,6 +70,51 @@ static pid_t pid; /* Directory to place temporary files in. */ static const char *test_dir; +static void +oom_error (const char *fn) +{ + printf ("%s: allocation failed: %m\n", fn); + exit (1); +} + +/* Allocate N bytes of memory dynamically, with error checking. */ +static void * +__attribute__ ((used)) +xmalloc (size_t n) +{ + void *p; + + p = malloc (n); + if (p == NULL) + oom_error ("malloc"); + return p; +} + +/* Allocate memory for N elements of S bytes, with error checking. */ +static void * +__attribute__ ((used)) +xcalloc (size_t n, size_t s) +{ + void *p; + + p = calloc (n, s); + if (p == NULL) + oom_error ("calloc"); + return p; +} + +/* Change the size of an allocated block of memory P to N bytes, + with error checking. */ +static void * +__attribute__ ((used)) +xrealloc (void *p, size_t n) +{ + p = realloc (p, n); + if (p == NULL) + oom_error ("realloc"); + return p; +} + /* List of temporary files. */ struct temp_name_list { @@ -83,9 +128,9 @@ __attribute__ ((unused)) add_temp_file (const char *name) { struct temp_name_list *newp - = (struct temp_name_list *) calloc (sizeof (*newp), 1); + = (struct temp_name_list *) xcalloc (sizeof (*newp), 1); char *newname = strdup (name); - if (newp != NULL && newname != NULL) + if (newname != NULL) { newp->name = newname; if (temp_name_list == NULL) @@ -124,13 +169,8 @@ create_temp_file (const char *base, char **filename) char *fname; int fd; - fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base) - + sizeof ("XXXXXX")); - if (fname == NULL) - { - puts ("out of memory"); - return -1; - } + fname = (char *) xmalloc (strlen (test_dir) + 1 + strlen (base) + + sizeof ("XXXXXX")); strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX"); fd = mkstemp (fname);