我正在尝试在C中编写自己的shell。下面的代码适用于没有管道的命令,但不适用。
使用--trace-children = yes和--track-origin = yes运行valgrind给我一个“Syscall param execve(argv)指向未初始化的字节”(参见下面的完整错误)。
在相关方法中(参见下面的makeargs),valgrind告诉我“未分配的值是由堆分配创建的”这一行“ argv =(char * )malloc((count + 1) * sizeof(char *));“
使用我的测试输入“ls | sort”valgrind说“块大小为12分配”。我不知道这是怎么可能的,因为ls和sort每个都调用makeargs,并且两者都应该分配8个字节,因为char应该有4个字节,然后4个用于execvp最后需要的(char *)NULL参数数组。
执行此命令后程序挂起。
我不确定为什么会发生这种情况,因为只有一次调用makeargs(没有管道)它才有效。任何输入将不胜感激。
void execCommand(char** commandParts, int pipeCount)
{
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;
int numCommands = pipeCount + 1;
int newfds[2];
int oldfds[2];
int k = 0;
for(k; k < numCommands; k++)
{
//more commands exist
if(k < pipeCount)
{
if (pipe(newfds) == -1)
{
perror("new pipe error");
exit(EXIT_FAILURE);
}
}
if(fork() == 0) //child
{
//is prev command
if(k > 0)
{
dup2(oldfds[PIPE_READ], STDIN_FILENO);
close(oldfds[PIPE_READ]);
close(oldfds[PIPE_WRITE]);
}
//more commands exist
if(k < pipeCount)
{
close(newfds[PIPE_READ]);
dup2(newfds[PIPE_WRITE], STDOUT_FILENO);
close(newfds[PIPE_WRITE]);
}
char** args = NULL;
int argcount = makeargs(commandParts[k], &args);
if(execvp(args[0], args) == -1)
{
printf("%s: command not found \n", args[0]);
}
}
else //parent
{
int status;
waitpid(-1, &status, NULL);
//is prev command
if(k > 0)
{
close(oldfds[PIPE_READ]);
close(oldfds[PIPE_WRITE]);
}
//more commands exist
if(k < pipeCount)
{
oldfds[PIPE_READ] = newfds[PIPE_READ];
oldfds[PIPE_WRITE] = newfds[PIPE_WRITE];
}
}
//there are pipes
if(pipeCount > 0 && k > 0)
{
close(newfds[PIPE_READ]);
close(newfds[PIPE_WRITE]);
}
// if(argcount > 0)
// cleanArgs(argcount, args);
}
}
被调用的make args方法
int makeargs(char *s, char *** argv)
{
stripLeadingAndTrailingSpaces(s);
int k =0, count = 0;
for(k; k < strlen(s); k++)
{
if(s[k] == ' ')
count++;
}
count++;
char* parts = strtok (s," ");
strip(parts);
*argv = (char **)malloc((count+1) * sizeof(char*));
(*argv)[0] = (char *)malloc(strlen(parts)+1);
strcpy((*argv)[0], parts);
int i = 1;
for(i; i < count; i++)
{
parts = strtok (NULL, " ");
if(parts != NULL)
{
strip(parts);
(*argv)[i] = (char *)malloc(strlen(parts)+1);
strcpy((*argv)[i], parts);
}
}
(*argv)[count] = NULL;
return count;
}
valgrind输出
==3603== Syscall param execve(argv) points to uninitialised byte(s)
==3603== at 0x40E2CDF: execve (execve.c:60)
==3603== by 0x40E314E: execvp (execvp.c:30)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
==3603== Address 0x41c617c is 4 bytes inside a block of size 12 alloc'd
==3603== at 0x4028876: malloc (vg_replace_malloc.c:236)
==3603== by 0x8049416: makeargs (ush.c:100)
==3603== by 0x8048E61: execCommand (cscd340_s12_hw2.c:191)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
==3603== Uninitialised value was created by a heap allocation
==3603== at 0x4028876: malloc (vg_replace_malloc.c:236)
==3603== by 0x8049416: makeargs (ush.c:100)
==3603== by 0x8048E61: execCommand (cscd340_s12_hw2.c:191)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
答案 0 :(得分:0)
你有一些不好的计数:count
是空格的数量,而不是命令行中的单词数。这会影响复制代码:
for(i; i < count; i++)
{
parts = strtok (NULL, " ");
if(parts != NULL)
{
...
}
}
如果输入命令行中的单词太少,argv
中的某些尾随条目将被取消初始化。你可能想做这样的事情:
for(i; i <= count; i++)
{
parts = strtok (NULL, " ");
if(parts != NULL)
{
... // consider using strdup
}
else
{
(*argv)[i] = NULL;
break;
}
}
答案 1 :(得分:0)
最后,我将问题跟踪到我的stripLeadingAndTrailingSpaces(s);
我有
void stripLeadingAndTrailingSpaces(char temp[])
{
int len = strlen(temp), start = 0;
while(isspace(temp[len]))
{
len--;
}
while(isspace(temp[start]))
{
start++;
}
memmove(temp, temp + start, len);
}
但需要
void stripLeadingAndTrailingSpaces(char temp[])
{
int len = strlen(temp)+1, start = 0;
while(isspace(temp[len-2]))
{
temp[len-2] = '\0';
len--;
}
while(isspace(temp[start]))
{
start++;
len--;
}
memmove(temp, temp + start, len);
}
旧的剥离方法存在一些问题: