我正在SO里闲逛,看到了this question。然后我开始想,我是否可以溢出argc。
(我wrote了一个小C程序和一个python脚本来测试它,但得到了一个MemoryError
。)
谢谢!
Rationale for International Standard — Programming Languages — C§5.1.2.2.1程序启动
根据惯例,argv[argc]
必须是空指针,以提供对列表末尾的冗余检查。
发布于 2015-01-22 03:38:00
在实践中,不能。大多数系统对argv
和envp
的总大小设置了相对较低的限制。从几十到几百KB的限制并不少见;有关各种OSes的限制的合理综合列表,请参阅http://www.in-ulm.de/~mascheck/various/argmax/。
发布于 2015-01-22 23:07:18
我试过这个:
test.c:
⚡⚡⚡ more test.c
#include <stdio.h>
int main(int argc, char **argv)
{
printf("argc = %d\n", argc);
printf("Size of argc = %d\n", sizeof(argc));
return 0;
}
然后用了一个很大的zipfile
⚡⚡⚡ ls -h bigfile
-rw-r--r-- 1 ehwas ehwas 355M Jan 22 16:54 bigfile
然后将该文件作为参数读取到测试程序:
⚡⚡⚡ ./test $(more bigfile)
结果:
5 minutes nothing happend, then everything froze
然后我尝试了一个较小的文件:
⚡⚡⚡ ls -h notsobigfile
-rw-r--r-- 1 ehwas ehwas 6.7M Jan 22 17:04 notsobigfile
和:
⚡⚡⚡ ./test $(more notsobigfile)
bash: ./test: Argument list too long
发布于 2015-01-28 22:12:46
如标准所示,argvargc必须是有效值。
因此,如果运行时环境处于不能保证这一点的情况下,就不应该启动程序。
https://stackoverflow.com/questions/28075323
复制相似问题