Why is Invalid Write (Valgrind) here?

I encode the shell. When I execute it as cat /dev/urandom | valgrind ./myshellto run some test and see if there are any segfault errors or other errors, valgrindsometimes they tell me that I have an Invalid Writein function in my_wordcpythis linetab[++j] = str[*i];

This does not happen every time, but it happens, and I just don’t understand why. Here is my code:

static int      count_words(char *str, char *sep)
{
  int           quote;
  int           words;
  int           i;

  i = -1;
  if (count_quotes(str) == -1)
    return (0);
  words = 0;
  quote = 0;
  while (str[++i] != '\0')
    {
      if (str[i] == '"')
        {
          if (quote == 0)
            quote = 1;
          else
            quote = 0;
        }
      if (quote == 0
          && (is_cinside(sep, str[i]) == 0 && str[i] != '\t' &&
              (is_cinside(sep, str[i + 1]) == 1 ||
           str[i + 1] == '\t' || str[i + 1] == '\0')))
        ++words;
    }
  return (words);
}

static int      my_wordlen(char *str, int *i, char *sep)
{
  int           quote;
  int           j;

  j = 0;
  quote = 0;
  while (str[++(*i)] != '\0')
    if (str[*i] == '"' && quote == 0)
      quote = 1;
    else if (quote == 1 || (quote == 0 && is_cinside(sep, str[*i]) == 0 &&
                            str[*i] != '\t'))
      {
        ++j;
        if ((quote == 1 && str[*i + 1] == '"') ||
            (quote == 0 && (is_cinside(sep, str[*i + 1]) == 1 ||
                            str[*i + 1] == '\t' ||
                            str[*i + 1] == '\0')))
          {
            if (quote == 1 && str[*i + 1] == '"')
              ++(*i);
            return (j);
          }
      }
  return (-1);
}

static char     *my_wordcpy(char *tab, char *str, int *i, char *sep)
{
  int           quote;
  int           j;

  j = -1;
  quote = 0;
  while (str[++(*i)] != '\0')
    if (str[*i] == '"' && quote == 0)
      quote = 1;
    else if (quote == 1 || (quote == 0 &&
                        is_cinside(sep, str[*i]) == 0 && str[*i] != '\t'))
  {
    tab[++j] = str[*i];            /* here is the invalid write. */
    if ((quote == 1 && str[*i + 1] == '"') ||
        (quote == 0 && (is_cinside(sep, str[*i + 1]) == 1 ||
                        str[*i + 1] == '\t' || str[*i + 1] == '\0')))
      {
        if (quote == 1 && str[*i + 1] == '"')
          ++(*i);
        tab[++j] = '\0';
        return (tab);
      }
  }
  return (NULL);
}

char            **my_quotetowordtab(char *str, char *sep)
{
  char          **tab;
  int           words;
  int           i;
  int           j;
  int           k;

  i = -1;
  j = -1;
  k = -1;
  if (str == NULL)
    return (NULL);
  words = count_words(str, sep);
  if ((tab = malloc(sizeof(char *) * (words + 1))) == NULL)
    return (NULL);
  while (++i < words)
    {
      if ((tab[i] = malloc(sizeof(char) * (my_wordlen(str, &j, sep) + 1)))
          == NULL)
            return (NULL);
      tab[i] = my_wordcpy(tab[i], str, &k, sep);
    }
  tab[i] = NULL;
  return (tab);
}
+4
source share
2 answers

my_wordlencan return -1, and you do not check it before transferring it malloc. In this case, the 0bytes are allocated, therefore, my_wordcopya heap buffer overflow occurs.

+5
source

, str " quote? , \0 , . , NUL 2 if, .

+1

Source: https://habr.com/ru/post/1589503/


All Articles