是一个C程序的参数保证是'\\ 0'结束的? [英] Are the arguments of a C program guaranteed to be '\0'-terminated?

查看:129
本文介绍了是一个C程序的参数保证是'\\ 0'结束的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于的主要参数(),2011年C标准说(5.1.2.2.1:2):


  

如果argc个的值是大于零,阵列成员通过的argv ARGV [0] [的argc-1]包括应包含指向字符串,其通过编程启动之前主机环境给定实现所定义的值。


在这方面应该单词串是PTED为0结尾的字符串跨$ P $,也就是说,接着是最终的'\\ 0'非0字符序列,或者做/可能有的实现参数传递给节目不同?

在一个POSIX平台,是的一个参数的exec * 家庭由验证功能的exec * 函数指针以及形成串(如何?),还是应该从假设它已经通过了setuid程序副歌合式0结尾的字符串作为参数?


解决方案

  

在这方面应该单词串是PTED为0结尾的字符串跨$ P $,也就是说,接着是最终的'\\ 0'非0字符序列,或者做/可能有的实现参数传递给节目不同?


7.1.1定义字符串:


  

一个字符串是终止字符的连续序列,并包括第一个空字符。




  

是EXEC *系列由exec *功能验证功能的指针以及形成串(如何?)。

之一的参数

借助 POSIX规范指出,args设置为在 EXEC 的家人都空值终止字符串,不指定会发生什么,如果他们不。 presumably这是不确定的行为。这似乎是合理的,因为没有为 EXEC没有合理的方式函数来验证每一个论点是正确空值终止。 (虽然记住, EXEC * 必须复制它的参数,如地址空间即将被换出)。

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?

解决方案

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.)

这篇关于是一个C程序的参数保证是'\\ 0'结束的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆