From patchwork Mon Aug 19 18:53: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: 19327 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-gh0-f198.google.com (mail-gh0-f198.google.com [209.85.160.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 33E33246F8 for ; Mon, 19 Aug 2013 18:53:28 +0000 (UTC) Received: by mail-gh0-f198.google.com with SMTP id r13sf2961542ghr.9 for ; Mon, 19 Aug 2013 11:53: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=aQno6QiGv1B8k+v/mGEpr9AHpxyXfn7AU6H+/WFOCuo=; b=E6RNvrdwh2tTxr1RQ7QtnXPVvIAUuwheNOOKAki1SkKo6X642cqdssrQuVudX83T6G lUgZZTyFdp5jIbHFIw8h4DVOQ7ZkRXWE/acOkiR3cpYdic41t3EgR+PYPm2taHjIOTv8 y7nCdyWjyRrFi+1t06HSxfZ1cllVAPyZy8KFp0QIKH75JBzFrUY6xW0FHJex/xVKYpCf IPIyEgISH4hGpEiDf/FEkFeh4Cd7AKrU7Mw7K8H40zkr5yu4byy7d6YfAoNNg2a38NQ7 WioHzpfrb8LtuXtYkoSFv9RHAtBqk8QgZZhzfw4VQ3gQm0G8XtuUvVzXnKNQ/pZbyONo 8xVQ== X-Received: by 10.236.53.70 with SMTP id f46mr4676869yhc.17.1376938407723; Mon, 19 Aug 2013 11:53:27 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.50.169 with SMTP id d9ls1688274qeo.21.gmail; Mon, 19 Aug 2013 11:53:27 -0700 (PDT) X-Received: by 10.221.40.10 with SMTP id to10mr3822897vcb.22.1376938407607; Mon, 19 Aug 2013 11:53:27 -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 sk7si2315807vdc.51.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 19 Aug 2013 11:53:27 -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 ha12so3431507vcb.9 for ; Mon, 19 Aug 2013 11:53:27 -0700 (PDT) X-Gm-Message-State: ALoCoQncV863IxcPdo4ONgvvE7CkWNctna36RnNJCzYtoPuRtOa9BX9RN3D6uX2n3dypwCfye47S X-Received: by 10.52.92.15 with SMTP id ci15mr1426406vdb.34.1376938407466; Mon, 19 Aug 2013 11:53: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 u4csp111875vcz; Mon, 19 Aug 2013 11:53:26 -0700 (PDT) X-Received: by 10.14.176.8 with SMTP id a8mr25133751eem.12.1376938406133; Mon, 19 Aug 2013 11:53:26 -0700 (PDT) Received: from mail-ea0-f169.google.com (mail-ea0-f169.google.com [209.85.215.169]) by mx.google.com with ESMTPS id i5si10874466eeg.145.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 19 Aug 2013 11:53:26 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.215.169 is neither permitted nor denied by best guess record for domain of will.newton@linaro.org) client-ip=209.85.215.169; Received: by mail-ea0-f169.google.com with SMTP id k11so1036255eaj.14 for ; Mon, 19 Aug 2013 11:53:25 -0700 (PDT) X-Received: by 10.14.198.73 with SMTP id u49mr25240141een.13.1376938405474; Mon, 19 Aug 2013 11:53:25 -0700 (PDT) Received: from localhost.localdomain (cpc6-seac21-2-0-cust453.7-2.cable.virginmedia.com. [82.1.113.198]) by mx.google.com with ESMTPSA id bn13sm19209493eeb.11.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 19 Aug 2013 11:53:24 -0700 (PDT) Message-ID: <521269A2.7040207@linaro.org> Date: Mon, 19 Aug 2013 19:53:22 +0100 From: Will Newton User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130805 Thunderbird/17.0.8 MIME-Version: 1.0 To: libc-alpha@sourceware.org CC: patches@linaro.org Subject: [PATCH v3] 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-19 Will Newton * malloc/Makefile: Add tst-realloc to tests. * malloc/tst-realloc.c: New file. --- malloc/Makefile | 3 +- malloc/tst-realloc.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 malloc/tst-realloc.c Changes in v3: - Add comments relating code to specification - Add tests for realloc (..., 0) cases 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..9d290d2 --- /dev/null +++ b/malloc/tst-realloc.c @@ -0,0 +1,146 @@ +/* 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; + + /* realloc (NULL, ...) behaves similarly to malloc (C89). */ + p = realloc (NULL, -1); + save = errno; + + if (p != NULL) + merror ("realloc (NULL, -1) succeeded."); + + /* errno should be set to ENOMEM on failure (POSIX). */ + if (p == NULL && save != ENOMEM) + merror ("errno is not set correctly"); + + errno = 0; + + /* realloc (NULL, ...) behaves similarly to malloc (C89). */ + p = realloc (NULL, 10); + save = errno; + + if (p == NULL) + merror ("realloc (NULL, 10) failed."); + + /* errno should be clear on success (POSIX). */ + if (p != NULL && save != 0) + merror ("errno is set but should not be"); + + free (p); + + p = calloc (20, 1); + if (p == NULL) + merror ("calloc (20, 1) failed."); + + /* Check increasing size preserves contents (C89). */ + 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); + + p = realloc (NULL, 100); + if (p == NULL) + merror ("realloc (NULL, 100) failed."); + + memset (p, 0xff, 100); + + /* Check decreasing size preserves contents (C89). */ + 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 (C89). */ + 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"); + + /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */ + p = realloc (p, 0); + if (p != NULL) + merror ("realloc (p, 0) returned non-NULL."); + + /* realloc (NULL, 0) acts like malloc (0) (glibc). */ + p = realloc (NULL, 0); + if (p == NULL) + merror ("realloc (NULL, 0) returned NULL."); + + free (p); + + return errors != 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"