According to standard
So from your quote:
argv[argc] is required to be a null pointer
Therefore, argc cannot overflow because the above statement will not be true.
On practice
In practice, the total size of the arguments passed to the program is limited.
On my Linux / x64 system:
$ getconf ARG_MAX
2097152
Consequently, the total size of the argument is about 2 megabytes, and argc cannot overflow. I believe this limit measures the combination of shared data in argv and the environment. If you exceed this limit when you try to execute a command, exec() will fail with an E2BIG error. From man 2 execve :
E2BIG The total number of bytes in the environment (envp) and argument
list (argv) is too large.
I believe that the 2 megabyte limit on my system is relatively large compared to other systems. My OS X system reports a limit of ~ 260KB.
But what if ARG_MAX were really big?
Well, suppose you are in an old / strange system, so int is 16 bits and ARG_MAX is more than 2 15 which is otherwise quite reasonable. Suppose you call execve() with more than two arguments 15 . Implementation has two options.
This may allow argc overflow ... basically, discarding your data, ensuring that the program you are running is executed in some unexpected and probably erroneous, and violates the C standard. Worst of all, the error is silent, so you never know.
Or it can simply return EOVERFLOW from execve() , telling you that it simply cannot start the image with so many parameters. Now the POSIX / SUS standards do not mention anything about this error result ... but I suspect that this is simply because standard authors never expected ARG_MAX be larger than INT_MAX .
Option No. 2 is the only reasonable option. If your system somehow chooses option number 1, then it is broken, and you should file a bug report.
Alternatively, you can try to run an old program compiled for a 16-bit system, but you run it through some kind of emulator or compatibility level. I expected that the emulator or compatibility level will give an error message if you try to pass more than 2 parameters to the program 15 .
Dietrich Epp Jan 21 '15 at 19:38 2015-01-21 19:38
source share