From patchwork Tue Jun 7 21:41:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 631818 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 3rPQ545S2Jz9t3k for ; Wed, 8 Jun 2016 07:42:00 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=Paf4aYaF; 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:cc:content-type; q=dns; s=default; b=oD01 CrG8dbDijJTdsKgB/phw5c2ZSbEcecsrazFOz09zW/idA/1R+D1VVJSJVvOBGSNW SYcUmrHzOV/LT5qZ+vf8vu81sIiut1YWlxxXOa0rVGJ/OeGC10C8qQFaHKg/YEz1 eNAWanWcDBkYSSivaIkW/nPc9YN5eHN93RVhNao= 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:cc:content-type; s=default; bh=UMdOjawja6 2QB2Z6zSETMRUc6VQ=; b=Paf4aYaFrp/s0njqcZbz9S1gbL3t5m+VGLLeGyhsum uJWWVZt77+6SQhAJxxptOKt+yIhes7sUsYxNHq+R7RE2hZv1vuLuBHNkS1CdGZKu q0EZOLmARRaXlmQ6pBa7++ioMUdloef0dHy7z5jIiyKF17yI+n/+bdNDabFzxS5S k= Received: (qmail 25291 invoked by alias); 7 Jun 2016 21:41:54 -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 25280 invoked by uid 89); 7 Jun 2016 21:41:53 -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= X-HELO: mail-vk0-f49.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:cc; bh=6R+EirolEs698APy2jAwwDuonPSZ/R3LssnEJhGYYEc=; b=a6wiDbnem/MmMHjv805p2sHTLKfyVXp+ggbDNKSmJem9NZx+X824oNPAzIrMUCPZ5y YkdEcdq1e6YYHbeFL8ZJYubzWHL93TtQt95FOm+hrc1cQuKNrdXlRZdfv35d4Ehd9Zt7 VbbUP7h09SL1+eRGN6jYKgGBxYIXy+U1R6kq7LsB2pd6eE4DU7xk7VLagOHUKoY59YdD LHo6MnG1v1jjJB3Lp7oKb3s4kVOQ7JFzVVkXi+CG4Df0Vr1IPx83b6YhSCyfhoz0rGDu iPN5zr1j4QlRnGG5VYe7qWac2c0MQRd+Clw7ic6Mf9AYIyeLd9YmnpsGYRWiM+mepOOa ZhsA== X-Gm-Message-State: ALyK8tIwtYOPK7mbFDN+fpq191D0ZESSatY+sEoxVYifRB1ukul/wuivJ4aonZfDhMDuLrukhgKhVGJBRKsBLsr9 X-Received: by 10.31.9.195 with SMTP id 186mr767692vkj.131.1465335710128; Tue, 07 Jun 2016 14:41:50 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <9e3d69fe-e948-1c11-10ab-ef33899df6ee@redhat.com> References: <20160531220247.GC7717@vapier.lan> <20160606015434.GG5431@vapier.lan> <9e3d69fe-e948-1c11-10ab-ef33899df6ee@redhat.com> From: Paul Pluzhnikov Date: Tue, 7 Jun 2016 14:41:20 -0700 Message-ID: Subject: Re: Do we care about malloc failure in tests? To: Florian Weimer Cc: GLIBC Devel , Bill Parker On Tue, Jun 7, 2016 at 9:44 AM, Florian Weimer wrote: > Perhaps also include the request allocation size as well. Something like attached? I am a little worried about multiplication overflow in xcalloc, but I guess this overflow shouldn't really happen in tests, and if it does happen the error report will be only a little bit wrong. Thanks, diff --git a/test-skeleton.c b/test-skeleton.c index 29bdc9c..a9ad4ab 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, size_t size) +{ + printf ("%s: unable to allocate %zu bytes: %m\n", fn, size); + 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", n); + 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", n * s); + 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", n); + 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);