2

I've been having trouble getting this section of code to work. I'm trying to get a character array to be copied so I can get a count of how many tokens there are to dynamically allocate and save them to be examined for environment variables. However, I keep segfaulting when it tries to strncpy the original string.

    void echo(char *str1)
    {
      char *token, *temp;
      char *saveptr1;
      int j, i, k, counter;
      char *copy;

      strncpy(copy, str1, 80);

      const char *delim = " ";
      i = strlen(copy);

      for(j = 0; j < i; j++, copy = NULL)
      {
         token = strtok_r(copy, delim, &saveptr1);
         counter++;
         if(token == NULL)
         {
           counter--;
           break;
         }
      }

      // initialize token array for echo
      char *tokAr[counter];
      for(j = 0; j < counter; j++)
        tokAr[j] = malloc(80*sizeof(char));

      for(j = 0, k = 0; j < i; j++, str1 = NULL)
      {
         tokAr[k] = strtok_r(str1, delim, &saveptr1);
         if( tokAr[k] != NULL)
         {
            if(strchr(tokAr[k], 36) != NULL)
            {
              temp = enviro(tokAr[k]);
              printf("%s ", temp);
            }
         else
           printf("%s ", tokAr[k]);
         }
         else
           break;
      }

      for(k = 0; k < counter; k++)
        free(tokAr[k]);
    }

    char* enviro(char *ret)
    {
      char *copy, *expand, *saveptr;
      const char *delim = "$";
      strcpy(copy, ret);
      expand = strtok_r(copy, delim, &saveptr);

      return getenv(expand);
    }

I know it has something to do with how I copy the passed in str1 character array but I can't figure it out from gdb. Any help is greatly appreciated

GFXGunblade
  • 97
  • 3
  • 10

3 Answers3

9

You haven't allocated memory for copy.

char *copy;
strncpy(copy, str1, 80);

Try malloc or strdup if you don't need the full 81 characters.

copy = malloc(81);
strncpy(copy, str1, 80);

/* Or strdup. */
copy = strdup(str1);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 2
    Be aware that strncpy does _not_ zero-terminate the destination string! You rarely want to use this function. In this case strdup is probably what you want. Also be aware that you need to free copy since strdup allocates new memory for it! – harald Sep 26 '11 at 07:21
  • @harald: Be aware that strncpy *does* zero terminate the string as long as the source string is shorter than the given length. In fact, it not only zero terminates but zero fills the rest of the buffer. Be aware also that `strdup()` is not C in that it is not in the C99 standard. Unfortunately, neither is `strlcpy()`, the improved version of `strncpy()`. – JeremyP Sep 26 '11 at 09:37
  • @JeremyP: You're right! I was a bit too quick there, only thinking of the case where the destination buffer is too short to fit the source string. Thanks for the correction! – harald Sep 26 '11 at 09:57
2

copy does not contain a valid allocated address. Please allocate enough memory with malloc before using copy. Also remember to free copy after you have completed using it to stop memory leak in larger programs.

phoxis
  • 60,131
  • 14
  • 81
  • 117
0

I think in function echo, you haven't initialized the variable counter and trying to incrementing and decrementing it. Try to do that.