aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstringop-overflow-51.c
blob: 6f36643c8bb60216f0b955244cce9bdb393ce8fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Test case derived from Binutils/GDB's readline/readline/histexpand.c.
   { dg-do compile }
   { dg-options "-O2 -Wall" } */

char *
get_subst_pattern (char *str, int *iptr, int delimiter, int is_rhs, int *lenptr)
{
  int si, i, j, k;
  char *s;

  s = 0;
  i = *iptr;

  for (si = i; str[si] && str[si] != delimiter; si++)
      if (str[si] == '\\' && str[si + 1] == delimiter)
	si++;

  if (si > i || is_rhs)
    {
      s = (char *)__builtin_malloc (si - i + 1);
      for (j = 0, k = i; k < si; j++, k++)
	{
	  /* Remove a backslash quoting the search string delimiter. */
	  if (str[k] == '\\' && str[k + 1] == delimiter)
	    k++;
	  s[j] = str[k];   // { dg-bogus "-Wstringop-overflow" }
	}
      s[j] = '\0';
      if (lenptr)
	*lenptr = j;
    }

  return s;
}