| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2013-2016 Free Software Foundation, Inc. | 
 | 2 |    This file is part of the GNU C Library. | 
 | 3 |  | 
 | 4 |    The GNU C Library is free software; you can redistribute it and/or | 
 | 5 |    modify it under the terms of the GNU Lesser General Public | 
 | 6 |    License as published by the Free Software Foundation; either | 
 | 7 |    version 2.1 of the License, or (at your option) any later version. | 
 | 8 |  | 
 | 9 |    The GNU C Library is distributed in the hope that it will be useful, | 
 | 10 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 11 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 12 |    Lesser General Public License for more details. | 
 | 13 |  | 
 | 14 |    You should have received a copy of the GNU Lesser General Public | 
 | 15 |    License along with the GNU C Library; if not, see | 
 | 16 |    <http://www.gnu.org/licenses/>.  */ | 
 | 17 |  | 
 | 18 | #include <errno.h> | 
 | 19 | #include <malloc.h> | 
 | 20 | #include <stdio.h> | 
 | 21 | #include <string.h> | 
 | 22 |  | 
 | 23 | static int errors = 0; | 
 | 24 |  | 
 | 25 | static void | 
 | 26 | merror (const char *msg) | 
 | 27 | { | 
 | 28 |   ++errors; | 
 | 29 |   printf ("Error: %s\n", msg); | 
 | 30 | } | 
 | 31 |  | 
 | 32 | static int | 
 | 33 | do_test (void) | 
 | 34 | { | 
 | 35 |   void *p; | 
 | 36 |   unsigned char *c; | 
 | 37 |   int save, i, ok; | 
 | 38 |  | 
 | 39 |   errno = 0; | 
 | 40 |  | 
 | 41 |   /* realloc (NULL, ...) behaves similarly to malloc (C89).  */ | 
 | 42 |   p = realloc (NULL, -1); | 
 | 43 |   save = errno; | 
 | 44 |  | 
 | 45 |   if (p != NULL) | 
 | 46 |     merror ("realloc (NULL, -1) succeeded."); | 
 | 47 |  | 
 | 48 |   /* errno should be set to ENOMEM on failure (POSIX).  */ | 
 | 49 |   if (p == NULL && save != ENOMEM) | 
 | 50 |     merror ("errno is not set correctly"); | 
 | 51 |  | 
 | 52 |   errno = 0; | 
 | 53 |  | 
 | 54 |   /* realloc (NULL, ...) behaves similarly to malloc (C89).  */ | 
 | 55 |   p = realloc (NULL, 10); | 
 | 56 |   save = errno; | 
 | 57 |  | 
 | 58 |   if (p == NULL) | 
 | 59 |     merror ("realloc (NULL, 10) failed."); | 
 | 60 |  | 
 | 61 |   /* errno should be clear on success (POSIX).  */ | 
 | 62 |   if (p != NULL && save != 0) | 
 | 63 |     merror ("errno is set but should not be"); | 
 | 64 |  | 
 | 65 |   free (p); | 
 | 66 |  | 
 | 67 |   p = calloc (20, 1); | 
 | 68 |   if (p == NULL) | 
 | 69 |     merror ("calloc (20, 1) failed."); | 
 | 70 |  | 
 | 71 |   /* Check increasing size preserves contents (C89).  */ | 
 | 72 |   p = realloc (p, 200); | 
 | 73 |   if (p == NULL) | 
 | 74 |     merror ("realloc (p, 200) failed."); | 
 | 75 |  | 
 | 76 |   c = p; | 
 | 77 |   ok = 1; | 
 | 78 |  | 
 | 79 |   for (i = 0; i < 20; i++) | 
 | 80 |     { | 
 | 81 |       if (c[i] != 0) | 
 | 82 |         ok = 0; | 
 | 83 |     } | 
 | 84 |  | 
 | 85 |   if (ok == 0) | 
 | 86 |     merror ("first 20 bytes were not cleared"); | 
 | 87 |  | 
 | 88 |   free (p); | 
 | 89 |  | 
 | 90 |   p = realloc (NULL, 100); | 
 | 91 |   if (p == NULL) | 
 | 92 |     merror ("realloc (NULL, 100) failed."); | 
 | 93 |  | 
 | 94 |   memset (p, 0xff, 100); | 
 | 95 |  | 
 | 96 |   /* Check decreasing size preserves contents (C89).  */ | 
 | 97 |   p = realloc (p, 16); | 
 | 98 |   if (p == NULL) | 
 | 99 |     merror ("realloc (p, 16) failed."); | 
 | 100 |  | 
 | 101 |   c = p; | 
 | 102 |   ok = 1; | 
 | 103 |  | 
 | 104 |   for (i = 0; i < 16; i++) | 
 | 105 |     { | 
 | 106 |       if (c[i] != 0xff) | 
 | 107 |         ok = 0; | 
 | 108 |     } | 
 | 109 |  | 
 | 110 |   if (ok == 0) | 
 | 111 |     merror ("first 16 bytes were not correct"); | 
 | 112 |  | 
 | 113 |   /* Check failed realloc leaves original untouched (C89).  */ | 
 | 114 |   c = realloc (p, -1); | 
 | 115 |   if (c != NULL) | 
 | 116 |     merror ("realloc (p, -1) succeeded."); | 
 | 117 |  | 
 | 118 |   c = p; | 
 | 119 |   ok = 1; | 
 | 120 |  | 
 | 121 |   for (i = 0; i < 16; i++) | 
 | 122 |     { | 
 | 123 |       if (c[i] != 0xff) | 
 | 124 |         ok = 0; | 
 | 125 |     } | 
 | 126 |  | 
 | 127 |   if (ok == 0) | 
 | 128 |     merror ("first 16 bytes were not correct after failed realloc"); | 
 | 129 |  | 
 | 130 |   /* realloc (p, 0) frees p (C89) and returns NULL (glibc).  */ | 
 | 131 |   p = realloc (p, 0); | 
 | 132 |   if (p != NULL) | 
 | 133 |     merror ("realloc (p, 0) returned non-NULL."); | 
 | 134 |  | 
 | 135 |   /* realloc (NULL, 0) acts like malloc (0) (glibc).  */ | 
 | 136 |   p = realloc (NULL, 0); | 
 | 137 |   if (p == NULL) | 
 | 138 |     merror ("realloc (NULL, 0) returned NULL."); | 
 | 139 |  | 
 | 140 |   free (p); | 
 | 141 |  | 
 | 142 |   return errors != 0; | 
 | 143 | } | 
 | 144 |  | 
 | 145 | #define TEST_FUNCTION do_test () | 
 | 146 | #include "../test-skeleton.c" |