3

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?

  • Well generally I wrap main with try/catch so you could do that print an error saying that they've provided SO many args that you've run out of memory. – 111111 Dec 21 '13 at 02:12
  • See also [Limit on the number of arguments to `main()` in C](http://stackoverflow.com/questions/3724369/limit-on-the-number-of-arguments-to-main-in-c) and [To check the E2BIG error condition in `exec()`](http://stackoverflow.com/questions/18559403/to-check-the-e2big-error-condition-in-exec/). – Jonathan Leffler Dec 21 '13 at 20:35

2 Answers2

2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

To me, that's no different from any other allocation error due to insufficient memory. I'd leave it the way it is - after all, if you hit this problem there is precious little you can do other than let the program terminate (which is the default behavior anyway).

Usually, you reach a "limit" with regard to the command line when using older shells - and in that case it is a limit on the buffer that holds the command line arguments in the shell process (that is, you try to start a program with a command line that is too long, so the shell truncates it or reports an error)

Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31