From patchwork Mon Aug 12 07:57:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Newton X-Patchwork-Id: 19003 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-gh0-f200.google.com (mail-gh0-f200.google.com [209.85.160.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5A27A248EC for ; Mon, 12 Aug 2013 07:57:26 +0000 (UTC) Received: by mail-gh0-f200.google.com with SMTP id 10sf4789976ghy.3 for ; Mon, 12 Aug 2013 00:57:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-gm-message-state:delivered-to:message-id:date:from:user-agent :mime-version:to:cc:subject:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=JKlzPH1KCfuz8fbHCZQeIsPbpS5wFcbrFZe6dYoH9ic=; b=KVSq39iT4Bf757hVKORt+winT+r65iVaGLW/98RQw2iQyMUOGCeENJ+Mn6o7gazjnt ETLcx9DZNHxYDGR5HwLQUbv5uxaOzMGMuW8+uMFpgGcJyLWqASEZynqDq3DwrTHhNXJP tG3kvxa/r8E3CGxo/Olp/+WEee/VyRibO2fQCBf+vQ/Q8GG5J3jfS2SZHOG+O5fHBdiR iFrY0QjN+CBiMA8vtGaJ4R4IwQQySyTiA3gsdqCfvIJsTvX7DD3bFrSXzoc6YpPgD3H2 BL1x394Vj8P7k9KFgxvRkDlgiJ3hZ+0rWZrfGzpEYTgJS4tjV9OcVHbKbDR0lHPDvrHw s/uw== X-Received: by 10.236.91.173 with SMTP id h33mr10344320yhf.17.1376294246070; Mon, 12 Aug 2013 00:57:26 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.121.195 with SMTP id lm3ls2237573qeb.89.gmail; Mon, 12 Aug 2013 00:57:25 -0700 (PDT) X-Received: by 10.52.163.71 with SMTP id yg7mr4882528vdb.110.1376294245942; Mon, 12 Aug 2013 00:57:25 -0700 (PDT) Received: from mail-vc0-f178.google.com (mail-vc0-f178.google.com [209.85.220.178]) by mx.google.com with ESMTPS id n1si5614102vex.64.2013.08.12.00.57.25 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 12 Aug 2013 00:57:25 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.178; Received: by mail-vc0-f178.google.com with SMTP id ha12so2581751vcb.9 for ; Mon, 12 Aug 2013 00:57:25 -0700 (PDT) X-Gm-Message-State: ALoCoQmhSgrqsuedb2J9+oEj3czfFRmyfmPoQWRykss3m/qf2Wmcd1iO/Hz+NRVPyct6DSnYdwdf X-Received: by 10.52.117.208 with SMTP id kg16mr9660903vdb.48.1376294245855; Mon, 12 Aug 2013 00:57:25 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp82842vcz; Mon, 12 Aug 2013 00:57:25 -0700 (PDT) X-Received: by 10.180.109.35 with SMTP id hp3mr5739189wib.52.1376294244959; Mon, 12 Aug 2013 00:57:24 -0700 (PDT) Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com [74.125.82.51]) by mx.google.com with ESMTPS id ep4si10277629wjd.163.2013.08.12.00.57.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 12 Aug 2013 00:57:24 -0700 (PDT) Received-SPF: neutral (google.com: 74.125.82.51 is neither permitted nor denied by best guess record for domain of will.newton@linaro.org) client-ip=74.125.82.51; Received: by mail-wg0-f51.google.com with SMTP id a12so5206743wgh.30 for ; Mon, 12 Aug 2013 00:57:24 -0700 (PDT) X-Received: by 10.180.185.166 with SMTP id fd6mr5927093wic.5.1376294244441; Mon, 12 Aug 2013 00:57:24 -0700 (PDT) Received: from localhost.localdomain (cpc11-seac20-2-0-cust84.7-2.cable.virginmedia.com. [81.108.156.85]) by mx.google.com with ESMTPSA id r6sm14605424wiw.0.2013.08.12.00.57.22 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 00:57:23 -0700 (PDT) Message-ID: <52089561.2080608@linaro.org> Date: Mon, 12 Aug 2013 08:57:21 +0100 From: Will Newton User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 To: libc-alpha@sourceware.org CC: patches@linaro.org Subject: [PATCH] malloc: Add realloc test. X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: will.newton@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , The current tests don't test the functionality of realloc in detail. Add a new test for realloc that exercises some of the corner cases that are not otherwise tested. ChangeLog: 2013-08-07 Will Newton * malloc/Makefile: Add tst-realloc to tests. * malloc/tst-realloc.c: New file. --- malloc/Makefile | 3 +- malloc/tst-realloc.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 malloc/tst-realloc.c diff --git a/malloc/Makefile b/malloc/Makefile index 0fe31a4..b16dac8 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -25,7 +25,8 @@ all: dist-headers := malloc.h headers := $(dist-headers) obstack.h mcheck.h tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \ - tst-mallocstate tst-mcheck tst-mallocfork tst-trim1 tst-malloc-usable + tst-mallocstate tst-mcheck tst-mallocfork tst-trim1 \ + tst-malloc-usable tst-realloc test-srcs = tst-mtrace routines = malloc morecore mcheck mtrace obstack diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c new file mode 100644 index 0000000..e6f15f9 --- /dev/null +++ b/malloc/tst-realloc.c @@ -0,0 +1,123 @@ +/* Copyright (C) 2013 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static int errors = 0; + +static void +merror (const char *msg) +{ + ++errors; + printf ("Error: %s\n", msg); +} + +int +main (void) +{ + void *p; + unsigned char *c; + int save, i, ok; + + errno = 0; + + p = realloc (NULL, -1); + save = errno; + + if (p != NULL) + merror ("realloc (NULL, -1) succeeded."); + + if (p == NULL && save != ENOMEM) + merror ("errno is not set correctly"); + + p = realloc (NULL, 10); + if (p == NULL) + merror ("realloc (NULL, 10) failed."); + + free (p); + + p = calloc (20, 1); + if (p == NULL) + merror ("calloc (20, 1) failed."); + + /* Check increasing size preserves contents. */ + p = realloc (p, 200); + if (p == NULL) + merror ("realloc (p, 200) failed."); + + c = p; + ok = 1; + + for (i = 0; i < 20; i++) + { + if (c[i] != 0) + ok = 0; + } + + if (ok == 0) + merror ("first 20 bytes were not cleared"); + + free (p); + + /* Check realloc(NULL, ...) works correctly. */ + p = realloc (NULL, 100); + if (p == NULL) + merror ("realloc (NULL, 100) failed."); + + memset (p, 0xff, 100); + + /* Check decreasing size preserves contents. */ + p = realloc (p, 16); + if (p == NULL) + merror ("realloc (p, 16) failed."); + + c = p; + ok = 1; + + for (i = 0; i < 16; i++) + { + if (c[i] != 0xff) + ok = 0; + } + + if (ok == 0) + merror ("first 16 bytes were not correct"); + + /* Check failed realloc leaves original untouched. */ + c = realloc (p, -1); + if (c != NULL) + merror ("realloc (p, -1) succeeded."); + + c = p; + ok = 1; + + for (i = 0; i < 16; i++) + { + if (c[i] != 0xff) + ok = 0; + } + + if (ok == 0) + merror ("first 16 bytes were not correct after failed realloc"); + + free (p); + + return errors != 0; +}