Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max command line arguments

I know about xargs --limits and getconf ARG_MAX. Is it worth even considering the idea of someone reaching that limit, and if so, would there be any negative effects on a program? For example, say you do this:

std::vector<std::string> v(argv, argv + argc);

Would the worst thing that could happen is that std::bad_alloc is thrown?


1 Answers

If someone tries to invoke the program with too many arguments, the exec*() operation fails and your program is not run. Thus, your program will only ever be invoked with a list of arguments that fits into the space available for arguments plus environment variables.

If your program tries to invoke another program and your program creates too big an argument list, then your program will fail to exec*() the other because the arguments plus environment are too big, getting error E2BIG.

You might theoretically run into memory allocation problems copying the argument list into a vector of strings, but it is relatively unlikely. The actual limit is about 128 KiB on Linux and about 256 KiB on Mac OS X.

like image 133
Jonathan Leffler Avatar answered Dec 12 '25 08:12

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!