From patchwork Mon Aug 12 08:49:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Newton X-Patchwork-Id: 19005 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f71.google.com (mail-qe0-f71.google.com [209.85.128.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id EAB07246B8 for ; Mon, 12 Aug 2013 08:49:27 +0000 (UTC) Received: by mail-qe0-f71.google.com with SMTP id 1sf8131712qee.10 for ; Mon, 12 Aug 2013 01:49:27 -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=YELS/6YEUs91ADv6hxj5gdgZyAdRW/RKXrb+PmK6v8E=; b=S61zPAAQKjxikcGViuex4MRjQCZNZge88YJ3tRVCeK8jAHtMIKNbRqRL6VN6cpGJSY VqEVkrT9hAzUGb1ktlcZpQo2FglOBUvXsajoE5wyPUUdGQjOne/2Kz/2ylVEnpOHot3q junIrvPO5ZZlKyxtPbP0lRpppgVjdwJtN6b2T/dXpYMe4nUb/XVtRl4zZ9w+7Dewt1Sx TgsAtBn1dFvOHoYRBU+hrbJ+wLXNoEBxUEZGY/sFhyl+IvoHKeGVVp79qRBxqwgjzum1 CQZvF15bmHxfFpu2Cfgk/gMrnOaAseXc6y08WmWaQxF7kchLAVIpvLT12hh4mBGxC/K5 nCUA== X-Received: by 10.236.62.132 with SMTP id y4mr6925420yhc.13.1376297367583; Mon, 12 Aug 2013 01:49:27 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.82.101 with SMTP id h5ls2209921qey.91.gmail; Mon, 12 Aug 2013 01:49:27 -0700 (PDT) X-Received: by 10.52.89.197 with SMTP id bq5mr2175750vdb.65.1376297367458; Mon, 12 Aug 2013 01:49:27 -0700 (PDT) Received: from mail-ve0-f172.google.com (mail-ve0-f172.google.com [209.85.128.172]) by mx.google.com with ESMTPS id xb6si8032485vdb.42.2013.08.12.01.49.27 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 12 Aug 2013 01:49:27 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.172 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.172; Received: by mail-ve0-f172.google.com with SMTP id oz10so5443021veb.3 for ; Mon, 12 Aug 2013 01:49:27 -0700 (PDT) X-Gm-Message-State: ALoCoQkOdAASnBfuFS0eaIFHLVBuIWAOcYT9zUS2k5t88cGs0Y8jh+UaGmTy/UWgOaovRF2V64+f X-Received: by 10.220.164.202 with SMTP id f10mr12073497vcy.25.1376297367317; Mon, 12 Aug 2013 01:49:27 -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 u4csp84516vcz; Mon, 12 Aug 2013 01:49:26 -0700 (PDT) X-Received: by 10.180.12.243 with SMTP id b19mr5824333wic.18.1376297366299; Mon, 12 Aug 2013 01:49:26 -0700 (PDT) Received: from mail-wi0-f175.google.com (mail-wi0-f175.google.com [209.85.212.175]) by mx.google.com with ESMTPS id ep5si971749wid.10.2013.08.12.01.49.25 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 12 Aug 2013 01:49:26 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.212.175 is neither permitted nor denied by best guess record for domain of will.newton@linaro.org) client-ip=209.85.212.175; Received: by mail-wi0-f175.google.com with SMTP id hq12so1397242wib.8 for ; Mon, 12 Aug 2013 01:49:25 -0700 (PDT) X-Received: by 10.180.208.49 with SMTP id mb17mr5861553wic.64.1376297365766; Mon, 12 Aug 2013 01:49:25 -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 z2sm14853369wiv.11.2013.08.12.01.49.23 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 01:49:24 -0700 (PDT) Message-ID: <5208A192.4040409@linaro.org> Date: Mon, 12 Aug 2013 09:49:22 +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 v2] 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.128.172 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 | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 malloc/tst-realloc.c Changes in v2: - Use test-skeleton.c framework. 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..7f61075 --- /dev/null +++ b/malloc/tst-realloc.c @@ -0,0 +1,126 @@ +/* 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); +} + +static int +do_test (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; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"