Are the arguments of a C program guaranteed to be '\0'-terminated?

。_饼干妹妹 提交于 2019-12-07 15:14:17

问题


About the arguments of main(), the 2011 C standard says (5.1.2.2.1:2):

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup.

Should the word “string” in this context be interpreted as “0-terminated string”, that is, a sequence of non-0 characters followed by a final '\0', or do/may some implementations pass arguments to programs differently?

On a POSIX platform, are the arguments of one of the exec* family of functions validated by the exec* function as pointers to well-formed strings (and how?), or should a setuid program refrain from assuming that it has been passed well-formed 0-terminated strings as arguments?


回答1:


Should the word “string” in this context be interpreted as “0-terminated string”, that is, a sequence of non-0 characters followed by a final '\0', or do/may some implementations pass arguments to programs differently?

7.1.1 defines a string:

A string is a contiguous sequence of characters terminated by and including the first null character.


Are the arguments of one of the exec* family of functions validated by the exec* function as pointers to well-formed strings (and how?).

The POSIX spec states that args to the exec family are null-terminated strings, and doesn't specify what happens if they aren't. Presumably it's undefined behaviour. This seems reasonable, because there's no reasonable way for the exec functions to validate that each argument is correctly null-terminated. (Although bear in mind that exec* must copy its arguments, as the address space is about to be swapped out.)



来源:https://stackoverflow.com/questions/23480542/are-the-arguments-of-a-c-program-guaranteed-to-be-0-terminated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!