3333 * OF THE POSSIBILITY OF SUCH DAMAGE.
3434 */
3535
36- #define _DEFAULT_SOURCE
36+ #define _GNU_SOURCE
3737#include <stdio.h>
3838#include <stdlib.h>
3939#include <errno.h>
4242#include <stdlib.h>
4343#include <stdint.h>
4444
45- #ifndef __clang__
45+ #ifdef __GNUC__
46+ #pragma GCC diagnostic ignored "-Wpragmas"
47+ #pragma GCC diagnostic ignored "-Wunknown-warning-option"
4648#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
4749#pragma GCC diagnostic ignored "-Wanalyzer-use-of-uninitialized-value"
50+ #pragma GCC diagnostic ignored "-Wanalyzer-use-after-free"
51+ #pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
52+ #pragma GCC diagnostic ignored "-Wuse-after-free"
53+ #pragma GCC diagnostic ignored "-Warray-bounds"
4854#endif
4955
5056int
@@ -56,24 +62,24 @@ main(void)
5662
5763 errno = 0 ;
5864 r = malloc (0 );
59- printf ("malloc(0): %p\n" , r );
65+ // printf("malloc(0): %p\n", r);
6066 if (errno != 0 ) {
6167 printf ("malloc(0) failed: %s. got %p\n" , strerror (errno ), r );
62- result ++ ;
68+ result = 1 ;
6369 }
6470 free (r );
6571
6672 r = memalign (128 , 237 );
67- printf ("memalign(128, 237): %p\n" , r );
73+ // printf("memalign(128, 237): %p\n", r);
6874 if ((uintptr_t ) r & 127 ) {
6975 printf ("memalign(128, 237) unaligned (%p)\n" , r );
70- result ++ ;
76+ result = 1 ;
7177 }
7278 free (r );
7379
7480 r = NULL ;
7581 err = posix_memalign (& r , 128 , 237 );
76- printf ("posix_memalign(128, 237): %p, err=%d\n" , r , err );
82+ // printf("posix_memalign(128, 237): %p, err=%d\n", r, err);
7783 if ((uintptr_t ) r & 127 ) {
7884 printf ("posix_memalign(128, 237) unaligned (%p)\n" , r );
7985 err ++ ;
@@ -82,7 +88,7 @@ main(void)
8288
8389 r = NULL ;
8490 err = posix_memalign (& r , 0 , 237 );
85- printf ("posix_memalign(0, 237): %p, err=%d\n" , r , err );
91+ // printf("posix_memalign(0, 237): %p, err=%d\n", r, err);
8692 if (err != EINVAL ) {
8793 printf ("posix_memalign(0, 237) should return EINVAL\n" );
8894 err ++ ;
@@ -91,7 +97,7 @@ main(void)
9197
9298 r = NULL ;
9399 err = posix_memalign (& r , 129 , 237 );
94- printf ("posix_memalign(129, 237): %p, err=%d\n" , r , err );
100+ // printf("posix_memalign(129, 237): %p, err=%d\n", r, err);
95101 if (err != EINVAL ) {
96102 printf ("posix_memalign(129, 237) should return EINVAL\n" );
97103 err ++ ;
@@ -100,7 +106,7 @@ main(void)
100106
101107 r = NULL ;
102108 err = posix_memalign (& r , 128 , PTRDIFF_MAX );
103- printf ("posix_memalign(128, PTRDIFF_MAX): %p, err=%d\n" , r , err );
109+ // printf("posix_memalign(128, PTRDIFF_MAX): %p, err=%d\n", r, err);
104110 if (err != ENOMEM ) {
105111 printf ("posix_memalign(128, PTRDIFF_MAX) should return ENOMEM\n" );
106112 err ++ ;
@@ -112,71 +118,120 @@ main(void)
112118 q = NULL ;
113119 if (r )
114120 q = malloc (PTRDIFF_MAX );
115- printf ("malloc(PTRDIFF_MAX: %p %p\n" , r , q );
121+ // printf("malloc(PTRDIFF_MAX: %p %p\n", r, q);
116122 if ((r && q ) || errno != ENOMEM ) {
117123 printf ("2*malloc(PTRDIFF_MAX) should have failed. got %p,%p error %s\n" , r , q , strerror (errno ));
118- result ++ ;
124+ result = 1 ;
119125 }
120126 free (r );
121127 free (q );
122128
123129 errno = 0 ;
124130 r = malloc (SIZE_MAX );
125- printf ("malloc(SIZE_MAX): %p\n" , r );
131+ // printf("malloc(SIZE_MAX): %p\n", r);
126132 if (r || errno != ENOMEM ) {
127133 printf ("malloc(SIZE_MAX) should have failed. got %p error %s\n" , r , strerror (errno ));
128- result ++ ;
134+ result = 1 ;
129135 }
130136
131137 errno = 0 ;
132138 r = calloc (1 , SIZE_MAX );
133- printf ("calloc(1, SIZE_MAX): %p\n" , r );
139+ // printf("calloc(1, SIZE_MAX): %p\n", r);
134140 if (r || errno != ENOMEM ) {
135141 printf ("calloc(1, SIZE_MAX) should have failed. got %p error %s\n" , r , strerror (errno ));
136- result ++ ;
142+ result = 1 ;
137143 }
138144
139145 errno = 0 ;
140146 r = reallocarray (NULL , 1 , SIZE_MAX );
141- printf ("reallocarray(NULL, 1, SIZE_MAX): %p\n" , r );
147+ // printf("reallocarray(NULL, 1, SIZE_MAX): %p\n", r);
142148 if (r || errno != ENOMEM ) {
143149 printf ("reallocarray(NULL, 1, SIZE_MAX) should have failed. got %p error %s\n" , r , strerror (errno ));
144- result ++ ;
150+ result = 1 ;
145151 }
146152
147153 for (pow = 0 ; pow < 4 ; pow ++ ) {
148154 errno = 0 ;
149155 r = calloc (SIZE_MAX >> pow , SIZE_MAX >> pow );
150- printf ("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n" , pow , pow , r );
156+ // printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
151157 if (r || errno != ENOMEM ) {
152158 printf ("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n" , pow , pow , r , strerror (errno ));
153- result ++ ;
159+ result = 1 ;
154160 }
155161 free (r );
156162 r = reallocarray (NULL , SIZE_MAX >> pow , SIZE_MAX >> pow );
157- printf ("reallocarray(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n" , pow , pow , r );
163+ // printf("reallocarray(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
158164 if (r || errno != ENOMEM ) {
159165 printf ("reallocarray(NULL, SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n" , pow , pow , r , strerror (errno ));
160- result ++ ;
166+ result = 1 ;
161167 }
162168 free (r );
163169 }
164170
171+ char * fzero = malloc (128 );
172+ int wrong = 0 ;
173+ if (fzero ) {
174+ for (pow = 0 ; pow < 128 ; pow ++ )
175+ fzero [pow ] = pow + 2 ;
176+ #ifndef _NANO_MALLOC_CLEAR_FREED
177+ #ifdef __GLIBC__
178+ explicit_bzero (fzero , 128 );
179+ #else
180+ memset_explicit (fzero , 0 , 128 );
181+ #endif
182+ #endif
183+ free (fzero );
184+ for (pow = 0 ; pow < 128 ; pow ++ )
185+ if (fzero [pow ] == pow + 2 ) {
186+ wrong ++ ;
187+ break ;
188+ }
189+ }
190+ if (wrong > 3 ) {
191+ printf ("free: %d bytes of memory not cleared\n" , wrong );
192+ result = 1 ;
193+ }
194+ wrong = 0 ;
195+ char * rzero = malloc (128 );
196+ if (rzero ) {
197+ for (pow = 0 ; pow < 128 ; pow ++ )
198+ rzero [pow ] = pow + 2 ;
199+ #ifndef _NANO_MALLOC_CLEAR_FREED
200+ #ifdef __GLIBC__
201+ explicit_bzero (rzero + 16 , 128 - 16 );
202+ #else
203+ memset_explicit (rzero + 16 , 0 , 128 - 16 );
204+ #endif
205+ #endif
206+ rzero = realloc (rzero , 16 );
207+ for (pow = 16 ; pow < 128 ; pow ++ ) {
208+ if (rzero [pow ] == pow + 2 ) {
209+ wrong ++ ;
210+ break ;
211+ }
212+ }
213+ free (rzero );
214+ }
215+ if (wrong > 3 ) {
216+ printf ("realloc: %d bytes of memory not cleared\n" , wrong );
217+ result = 1 ;
218+ }
219+
165220 /* make sure realloc doesn't read past the source */
166221
167222 void * big = malloc (1024 );
168- printf ("big %p\n" , big );
223+ // printf("big %p\n", big);
169224 if (big ) {
170225 memset (big , '1' , 1024 );
171226 void * small = malloc (128 );
172- printf ("small %p\n" , small );
227+ // printf("small %p\n", small);
173228 if (small ) {
174229 memset (small , '2' , 128 );
175230 (void ) atoi (small );
176231 free (big );
177232 char * med = realloc (small , 1024 );
178233 if (med ) {
179- printf ("med %p\n" , med );
234+ // printf("med %p\n", med);
180235#ifdef _NANO_MALLOC
181236 int i ;
182237 for (i = 128 ; i < 1024 ; i ++ )
0 commit comments