#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void main(){
int x,y,status, i;
int cnt = 0;
int flag = 0;
char buf[50];
char str[50];
char * argv[10];
char * ptr;
for(i=0; i<10; i++){
printf("$");
gets(buf);
strcpy(str, buf);
ptr = strtok(buf, " ");
while(ptr != NULL){
argv[cnt] = ptr;
cnt++;
ptr = strtok(NULL," ");
}
if(!strcmp(argv[cnt-1], "&")) {
argv[cnt-1] = 0;
flag = 1;
}
else {
argv[cnt] = 0;
}
if(!strcmp(argv[cnt-1], "exit")) exit(0);
x=fork();
if (x==0){
sleep(1);
printf("I am child to execute %s\n", str);
y=execve(argv[0], argv, 0);
if (y<0){
perror("exec failed");
exit(1);
}
}
else {
if(flag == 0) {
wait(&status);
}
}
flag = 0;
cnt = 0;
}
}
在Linux中运行此代码,
分段故障(堆芯卸载)
当使用gdb时,
==========================================================
程序接收信号sigsegv,
分割错误。
0x0000003B6572FA96位于strcmp_sse42()
来自/lib64/libc.so.6
==========================================================
为什么不起作用?
如果我打字
/bin/ls-al(任何不带“&”的内容)
干得好
buf类型
/垃圾箱/垃圾箱-铝&
错误
最佳答案
如果输入&
,则变量argv[cnt-1]
将设置为空,因此在if(!strcmp(argv[cnt-1], "exit"))
中,函数strcmp
的第一个参数将为空,这将使应用程序崩溃…
此外,您的代码不会检查任何缓冲区溢出、索引越界…这个密码相当“危险”
这意味着:
不应使用gets
,请参见https://stackoverflow.com/a/1694042/808101
检查argv
(注意:这个变量名可能不太好,argv通常是程序本身的参数)数组在插入元素时是否足够大
关于c - linux execve,段错误(strcmp_sse42),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50315930/