1 /* $OpenBSD: malloc_errno.c,v 1.5 2019/06/11 22:16:13 bluhm 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 /* On arm64 with 2G of memory this test hangs while junking. */ 11 char *malloc_options = "jj"; 12 13 static void 14 testerrno(size_t sz) 15 { 16 void *p; 17 18 errno = -1; 19 p = malloc(sz); 20 21 if (p == NULL && errno != ENOMEM) 22 errx(1, "fail: %lx %p %d", (unsigned long)sz, p, errno); 23 24 /* if alloc succeeded, test if errno did not change */ 25 if (p != NULL && errno != -1) 26 errx(1, "fail: %lx %p %d", (unsigned long)sz, p, errno); 27 28 free(p); 29 } 30 31 /* 32 * Provide some (silly) arguments to malloc(), and check if ERRNO is set 33 * correctly. 34 */ 35 int 36 main(int argc, char *argv[]) 37 { 38 size_t i; 39 40 testerrno(1); 41 testerrno(100000); 42 testerrno(-1); 43 testerrno(-1000); 44 testerrno(-10000); 45 testerrno(-10000000); 46 for (i = 0; i < 0x10; i++) 47 testerrno(i * 0x10000000); 48 return 0; 49 } 50