c - C 程序的参数是否保证以 '\0' 终止?

标签 c security posix c11 execve

关于 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/

相关文章:

c++ - 是否有 C/C++ API 用于读取 WIndows 上的 CPU 性能监控计数器 (PMC)(适用于 intel 和 AMD 处理器)?

c - 使用来自多个文件的信号量 C-Windows

c - 初始化结构内的字符

c - 关于C编译过程和库的链接

c# - 如何测试虚拟目录的用户权限?

c - 从文件中读取结构体

php - 使用全局变量字符串的替代方案以及与使用它们相关的任何安全问题?

android - 谷歌身份验证器作为单一登录方法

c - 是否为定义明确的同一文件描述符创建两个文件?

c - 是否应该使用 GLOB_MARK 将 glob 附加到符号链接(symbolic link)到目录的结果?