Let tst-swscanf find its locale
[glibc.git] / malloc / tst-posix_memalign.c
blob27c0dd2bd406ac5db6a9e58498c2245563ac8048
1 /* Test for posix_memalign.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
25 static int errors = 0;
27 static void
28 merror (const char *msg)
30 ++errors;
31 printf ("Error: %s\n", msg);
34 static int
35 do_test (void)
37 void *p;
38 int ret;
39 unsigned long pagesize = getpagesize ();
40 unsigned long ptrval;
42 p = NULL;
44 /* An attempt to allocate a huge value should return ENOMEM and
45 p should remain NULL. */
46 ret = posix_memalign (&p, sizeof (void *), -1);
48 if (ret != ENOMEM)
49 merror ("posix_memalign (&p, sizeof (void *), -1) succeeded.");
51 if (ret == ENOMEM && p != NULL)
52 merror ("returned an error but pointer was modified");
54 free (p);
56 p = NULL;
58 /* Test to expose integer overflow in malloc internals from BZ #15857. */
59 ret = posix_memalign (&p, pagesize, -pagesize);
61 if (ret != ENOMEM)
62 merror ("posix_memalign (&p, pagesize, -pagesize) succeeded.");
64 free (p);
66 p = NULL;
68 /* A zero-sized allocation should succeed with glibc, returning zero
69 and setting p to a non-NULL value. */
70 ret = posix_memalign (&p, sizeof (void *), 0);
72 if (ret != 0 || p == NULL)
73 merror ("posix_memalign (&p, sizeof (void *), 0) failed.");
75 free (p);
77 ret = posix_memalign (&p, 0x300, 10);
79 if (ret != EINVAL)
80 merror ("posix_memalign (&p, 0x300, 10) succeeded.");
82 ret = posix_memalign (&p, 0, 10);
84 if (ret != EINVAL)
85 merror ("posix_memalign (&p, 0, 10) succeeded.");
87 p = NULL;
89 ret = posix_memalign (&p, 0x100, 10);
91 if (ret != 0)
92 merror ("posix_memalign (&p, 0x100, 10) failed.");
94 if (ret == 0 && p == NULL)
95 merror ("returned success but pointer is NULL");
97 ptrval = (unsigned long) p;
99 if (ret == 0 && (ptrval & 0xff) != 0)
100 merror ("pointer is not aligned to 0x100");
102 free (p);
104 return errors != 0;
107 #define TEST_FUNCTION do_test ()
108 #include "../test-skeleton.c"