关于 main()
的参数,2011 C 标准说 (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.
在此上下文中,单词“string”是否应解释为“0-terminated string”,即非 0 字符序列后跟最后的“\0”,或者某些实现是否/可能将参数传递给程序不同?
在 POSIX 平台上,exec*
函数族之一的参数是否被 exec*
函数验证为指向格式正确的字符串的指针(以及如何?),或者 setuid 程序是否应该避免假定它已被传递给它的以 0 结尾的格式正确的字符串作为参数?
最佳答案
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定义了一个字符串:
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?).
POSIX spec声明 exec
系列的 args 是以 null 结尾的字符串,并且没有指定如果不是这样会发生什么。大概是未定义的行为。这似乎是合理的,因为 exec
函数没有合理的方法来验证每个参数是否正确地以 null 终止。 (尽管请记住 exec*
必须复制其参数,因为地址空间即将被换出。)
关于c - C 程序的参数是否保证以 '\0' 终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23480542/