Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:30:15+00:00 2026-06-02T16:30:15+00:00

I am trying to write my own shell in C. The code below works

  • 0

I am trying to write my own shell in C. The code below works for commands without a pipe but otherwise does not.

Running valgrind with –trace-children=yes and –track-origins=yes give me a “Syscall param execve(argv) points to uninitialised byte(s)” (See the full error below).

In the relevant method (see makeargs below) valgrind is telling me “Uninitialised value was created by a heap allocation” at this line “argv = (char *)malloc((count+1) * sizeof(char*));”

Using my test input of “ls | sort” valgrind says that a “block of size 12 alloc’d”. I do not see how this is possible because ls and sort each make a call to makeargs and both should be 8 bytes allocated beacause there should be 4 bytes for the char* then 4 for the (char*)NULL that execvp needs at the end of the argument array.

The program hangs after executing this command.

I am not sure why this is happening because it works if there is only one call to makeargs (no pipes). Any input would be appreciated.

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);   
  }
}

the make args method that gets called

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 output

==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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:30:16+00:00Added an answer on June 2, 2026 at 4:30 pm

    Finally, I tracked the problem to my stripLeadingAndTrailingSpaces(s);

    I had

    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);
    }
    

    but needed

    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);
    }
    

    There were a couple of problems with the old strip method:

    1. I was checking ‘/0’ to see if it was a space at the end.
    2. I was not applying a new null terminator to the new end.
    3. I was not reducing the length when cutting spaces off the front.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a code library for my own personal use and I'm
I'm trying to write my own Custom Validation attribute but I'm having some problems.
I'm trying to write my own code for a 'friends-of-friends' algorithm. This algorithm acts
I'm trying to write my own Python code to compute t-statistics and p-values for
I am trying to write my own edit-in-place using jQuery. My code is something
I'm trying to write my own function like with no luck. ...is not a
I'm trying to write my own lightbox script but I'm stuck on a problem.
Java, i was trying to write my own STUN client, but it seems like
I'm trying to write my own code to walk the PATH to find an
I'm trying to write my own toy My Toy Language -> MSIL compiler in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.