From patchwork Mon Jun 6 00:39:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 12784 Received: (qmail 55370 invoked by alias); 6 Jun 2016 00:39:39 -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 55359 invoked by uid 89); 6 Jun 2016 00:39:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.0 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:2542 X-HELO: mail-vk0-f45.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=vItVwS8CL+dDXxZw5ZFs/Uf0FEOtBw3Xk3fXgtxCMS4=; b=XqoM+MyHf/aO6Z7w68sH3K7VMEVmxdJA7KqE4NplPNlgfj5oZwrs0mtu8RiyyTBiX+ wW7AR3WCcsc3/DqLFDspqc/73E0v/JwYOsjSuCbER8r+VGoEDwPNVK9AR0ANYpsneBWz pIFLN1JjM+A7YZHX+qtkfZ5RvHgo8/JMmdwcbK5LXKKUu000WGrhxAAXnMDaaU3UGLY3 X7znhLw7LmuKF4eTjtTYROWm8+HJaDTeAwSrORGpmmrcvcIA5gSkCuuToG6U5+OL4K1W Qx0eHjfcClhiqa5d2ACzPlRg/75fzwO5v/7aymcynrpUMD5t4iSFAG4/JlpoUHffqfa8 DaAg== X-Gm-Message-State: ALyK8tLXfnBDc4az6boYjMx0Gvn4j+La8ExQZpHGPx7+/NgrrIXWc19LIMbat+FibO9MdLKmTtKVsMr5aMRzQWUK X-Received: by 10.31.147.65 with SMTP id v62mr7083666vkd.73.1465173574292; Sun, 05 Jun 2016 17:39:34 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20160531220247.GC7717@vapier.lan> References: <20160531220247.GC7717@vapier.lan> From: Paul Pluzhnikov Date: Sun, 5 Jun 2016 17:39:04 -0700 Message-ID: Subject: Re: Do we care about malloc failure in tests? To: Paul Pluzhnikov , GLIBC Devel , Bill Parker On Tue, May 31, 2016 at 3:02 PM, Mike Frysinger wrote: > i'd really like to see xmalloc type helpers added to the test skeleton > rather than change every test to include the same boiler plate How does attached patch look? If it's ok, I can commit it and then convert malloc/calloc/realloc in tests to xmalloc and friends. Thanks, 2016-06-05 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..1be312d 100644 --- a/test-skeleton.c +++ b/test-skeleton.c @@ -70,6 +70,54 @@ static pid_t pid; /* Directory to place temporary files in. */ static const char *test_dir; +static void +oom_error (const char *fn) +{ + fprintf (stderr, "Out of memory in %s\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 == 0) + 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 == 0) + oom_error ("calloc"); + return p; +} + +/* Change the size of an allocated block of memory P to N bytes, + with error checking. + If P is NULL, run xmalloc. */ +static void * +__attribute__ ((used)) +xrealloc (void *p, size_t n) +{ + if (p == 0) + return xmalloc (n); + p = realloc (p, n); + if (p == 0) + oom_error ("realloc"); + return p; +} + /* List of temporary files. */ struct temp_name_list { @@ -83,9 +131,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 +172,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);