lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2005 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2005. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <features.h> |
| 24 | |
| 25 | static int errors = 0; |
| 26 | |
| 27 | static void |
| 28 | merror (const char *msg) |
| 29 | { |
| 30 | ++errors; |
| 31 | printf ("Error: %s\n", msg); |
| 32 | } |
| 33 | |
| 34 | int |
| 35 | main (void) |
| 36 | { |
| 37 | void *p, *q; |
| 38 | |
| 39 | errno = 0; |
| 40 | |
| 41 | p = malloc (-1); |
| 42 | |
| 43 | if (p != NULL) |
| 44 | merror ("malloc (-1) succeeded."); |
| 45 | else if (errno != ENOMEM) |
| 46 | merror ("errno is not set correctly."); |
| 47 | |
| 48 | p = malloc (10); |
| 49 | if (p == NULL) |
| 50 | merror ("malloc (10) failed."); |
| 51 | |
| 52 | p = realloc (p, 0); |
| 53 | if (p != NULL) |
| 54 | merror ("realloc (p, 0) failed."); |
| 55 | |
| 56 | p = malloc (0); |
| 57 | #if !defined(__UCLIBC__) || defined(__MALLOC_GLIBC_COMPAT__) |
| 58 | if (p == NULL) |
| 59 | #else |
| 60 | if (p != NULL) |
| 61 | #endif |
| 62 | merror ("malloc (0) failed."); |
| 63 | |
| 64 | p = realloc (p, 0); |
| 65 | if (p != NULL) |
| 66 | merror ("realloc (p, 0) failed."); |
| 67 | |
| 68 | q = malloc (256); |
| 69 | if (q == NULL) |
| 70 | merror ("malloc (256) failed."); |
| 71 | |
| 72 | p = malloc (512); |
| 73 | if (p == NULL) |
| 74 | merror ("malloc (512) failed."); |
| 75 | |
| 76 | if (realloc (p, -256) != NULL) |
| 77 | merror ("realloc (p, -256) succeeded."); |
| 78 | else if (errno != ENOMEM) |
| 79 | merror ("errno is not set correctly."); |
| 80 | |
| 81 | free (p); |
| 82 | |
| 83 | p = malloc (512); |
| 84 | if (p == NULL) |
| 85 | merror ("malloc (512) failed."); |
| 86 | |
| 87 | if (realloc (p, -1) != NULL) |
| 88 | merror ("realloc (p, -1) succeeded."); |
| 89 | else if (errno != ENOMEM) |
| 90 | merror ("errno is not set correctly."); |
| 91 | |
| 92 | free (p); |
| 93 | free (q); |
| 94 | |
| 95 | return errors != 0; |
| 96 | } |