3 * * should probably use execve() for portability
11 #include <stdlib.h> /* malloc */
16 #define STEPS_DEFAULT 10
17 #define SAFETY_MARGIN 2048
18 #define TEST_PROGRAM "/bin/true"
23 * ``It is implementation‐defined whether null terminators, pointers, and/or any
24 * alignment bytes are included in this total.''
25 * -- IEEE Std 1003.1‐2003 on ARG_MAX
27 * http://www.in-ulm.de/~mascheck/various/argmax/
33 #include <string.h> /* temp: strnlen */
35 static int test_maxsize_binary()
37 const long sys_arg_max
= sysconf(_SC_ARG_MAX
);
38 char* testarg
= malloc(sys_arg_max
);
39 int n
= (sys_arg_max
-1)/2;
42 for (i
=sys_arg_max
; i
>=0; --i
)
48 //printf("ARG_MAX: %d\n", sys_arg_max);
49 //printf("testarg@%p\n", testarg);
53 printf("strlen: %d\n", strnlen(testarg
, sys_arg_max
));
59 execl("/bin/true", testarg
, (char*)NULL
);
70 switch (WEXITSTATUS(status
))
73 printf("current fuzz: %d\n", n
*0.5);
76 printf("%d iterations\n", i
);
79 printf("worked, n=%d/%f\n", n
, n
*1.5);
81 #define MIN(x,y) (x<y?x:y)
82 n
= MIN(1.5*n
, sys_arg_max
-1);
86 case E2BIG
: /* argument list too long */
87 printf("E2BIG, n=%d/%d\n", n
, n
*0.5);
92 default: /* other error */
93 fprintf(stderr
, "WARNING: child encountered unexpected error: %s\n",
94 strerror(WEXITSTATUS(status
)));
99 return 0; /* NEVER REACHED */
102 static int test_maxsize_linear()
104 const long sys_arg_max
= sysconf(_SC_ARG_MAX
);
105 char* testarg
= malloc(sys_arg_max
);
106 int n
= sys_arg_max
-1;
118 execl("/bin/true", testarg
, (char*)NULL
);
129 switch (WEXITSTATUS(status
))
132 printf("%d iterations\n", sys_arg_max
-n
);
136 case E2BIG
: /* argument list too long */
140 default: /* other error */
141 fprintf(stderr
, "WARNING: child encountered unexpected error: %s\n",
142 strerror(WEXITSTATUS(status
)));
147 return 0; /* NEVER REACHED */
153 argmax_binary(int steps
)
156 steps
= STEPS_DEFAULT
;
177 main(int ac
, char** av
, char** env
)
179 // printf("maximum size of args (linear search): %d\n", test_maxsize_linear());
180 printf("maximum size of args (binary search): %d\n", test_maxsize_binary());
181 //printf("maximum size of args: %d\n", test_maxsize_linear());
182 //printf("maximum number of args: %d\n", test_maxnum_linear());