1 /* $OpenBSD: malloc_errno.c,v 1.4 2003/12/25 18:49:57 miod Exp $ */ 2 /* 3 * Public domain. 2003, Otto Moerbeek 4 */ 5 #include <err.h> 6 #include <errno.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 static void 11 testerrno(size_t sz) 12 { 13 void *p; 14 15 errno = -1; 16 p = malloc(sz); 17 18 if (p == NULL && errno != ENOMEM) 19 errx(1, "fail: %lx %p %d", (unsigned long)sz, p, errno); 20 21 /* if alloc succeeded, test if errno did not change */ 22 if (p != NULL && errno != -1) 23 errx(1, "fail: %lx %p %d", (unsigned long)sz, p, errno); 24 25 free(p); 26 } 27 28 /* 29 * Provide some (silly) arguments to malloc(), and check if ERRNO is set 30 * correctly. 31 */ 32 int 33 main(int argc, char *argv[]) 34 { 35 size_t i; 36 37 testerrno(1); 38 testerrno(100000); 39 testerrno(-1); 40 testerrno(-1000); 41 testerrno(-10000); 42 testerrno(-10000000); 43 for (i = 0; i < 0x10; i++) 44 testerrno(i * 0x10000000); 45 return 0; 46 } 47