0

我正在尝试用 C 编写我自己的 shell。下面的代码适用于没有管道的命令,否则不能。

使用 --trace-children=yes 和 --track-origins=yes 运行 valgrind 会给我一个“Syscall param execve(argv) points to uninitialised byte(s)”(请参阅​​下面的完整错误)。

在相关方法中(参见下面的 makeargs),valgrind 在“ argv = (char * )malloc((count+1) * sizeof(char*));”这一行告诉我“未初始化的值是由堆分配创建的”

使用我的“ls | sort”valgrind 的测试输入表示“分配了一个大小为 12 的块”。我看不出这是怎么可能的,因为 ls 和 sort 每个都调用 makeargs 并且两者都应该分配 8 个字节,因为 char* 应该有 4 个字节,然后 execvp 最后需要的 (char*)NULL 应该有 4 个字节的参数数组。

执行此命令后程序挂起。

我不确定为什么会发生这种情况,因为如果只有一次调用 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)
4

2 回答 2

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;
  }
}
于 2012-04-22T16:49:05.373 回答
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);
}

旧的 strip 方法有几个问题:

  1. 我正在检查'/0',看看它最后是否是一个空格。
  2. 我没有将新的空终止符应用于新端。
  3. 切割前面的空间时,我并没有减少长度。
于 2012-04-22T17:53:30.897 回答