2013-10-03 12:26:34 +02:00
|
|
|
/* Test for valloc.
|
2014-01-01 12:03:15 +01:00
|
|
|
Copyright (C) 2013-2014 Free Software Foundation, Inc.
|
2013-08-12 15:44:36 +02:00
|
|
|
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
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
2000-02-22 06:25:08 +01:00
|
|
|
#include <stdlib.h>
|
2013-08-12 15:44:36 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2000-02-22 06:25:08 +01:00
|
|
|
|
2013-08-12 15:44:36 +02:00
|
|
|
static int errors = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
merror (const char *msg)
|
|
|
|
{
|
|
|
|
++errors;
|
|
|
|
printf ("Error: %s\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
2000-02-22 06:25:08 +01:00
|
|
|
{
|
2013-08-12 15:44:36 +02:00
|
|
|
void *p;
|
2013-10-03 12:26:34 +02:00
|
|
|
unsigned long pagesize = getpagesize ();
|
2013-08-12 15:44:36 +02:00
|
|
|
unsigned long ptrval;
|
|
|
|
int save;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
/* An attempt to allocate a huge value should return NULL and set
|
|
|
|
errno to ENOMEM. */
|
2013-08-12 15:44:36 +02:00
|
|
|
p = valloc (-1);
|
|
|
|
|
|
|
|
save = errno;
|
2000-02-22 06:25:08 +01:00
|
|
|
|
2013-08-12 15:44:36 +02:00
|
|
|
if (p != NULL)
|
|
|
|
merror ("valloc (-1) succeeded.");
|
2000-02-22 06:25:08 +01:00
|
|
|
|
2013-08-12 15:44:36 +02:00
|
|
|
if (p == NULL && save != ENOMEM)
|
|
|
|
merror ("valloc (-1) errno is not set correctly");
|
2000-02-22 06:25:08 +01:00
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
free (p);
|
|
|
|
|
2013-08-12 15:44:36 +02:00
|
|
|
errno = 0;
|
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
/* Test to expose integer overflow in malloc internals from BZ #15856. */
|
2013-08-12 15:44:36 +02:00
|
|
|
p = valloc (-pagesize);
|
|
|
|
|
|
|
|
save = errno;
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
merror ("valloc (-pagesize) succeeded.");
|
|
|
|
|
|
|
|
if (p == NULL && save != ENOMEM)
|
|
|
|
merror ("valloc (-pagesize) errno is not set correctly");
|
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
free (p);
|
|
|
|
|
|
|
|
/* A zero-sized allocation should succeed with glibc, returning a
|
|
|
|
non-NULL value. */
|
2013-08-12 15:44:36 +02:00
|
|
|
p = valloc (0);
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
merror ("valloc (0) failed.");
|
|
|
|
|
|
|
|
free (p);
|
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
/* Check the alignment of the returned pointer is correct. */
|
2013-08-12 15:44:36 +02:00
|
|
|
p = valloc (32);
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
merror ("valloc (32) failed.");
|
|
|
|
|
2013-10-03 12:26:34 +02:00
|
|
|
ptrval = (unsigned long) p;
|
2013-08-12 15:44:36 +02:00
|
|
|
|
|
|
|
if ((ptrval & (pagesize - 1)) != 0)
|
|
|
|
merror ("returned pointer is not page aligned.");
|
|
|
|
|
|
|
|
free (p);
|
|
|
|
|
|
|
|
return errors != 0;
|
2000-02-22 06:25:08 +01:00
|
|
|
}
|
2013-08-12 15:44:36 +02:00
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|