3

I'm porting a program that works with Ubuntu 8.04 (gcc version 4.2.4) to 10.04 (gcc version 4.4.3). I have the following code:

#include <stdio.h>
#include <string.h>

int main(void) {
  char p[100] = "////abcd";
  char *t;

  /* Remove duplicate slashes, saving only one of them */
  while (t = strstr(p, "//"))
    strcpy(t, t + 1);

  printf("%s\n", p);

  return 0;
}

The result should be /abcd, which it is with gcc 4.2.4. With 4.4.3, the output is /accd.

Can you suggest me a code change that will give the correct output using both versions of gcc, and preferrably explain what is going on here.

Thanks in advance!

Mat
  • 202,337
  • 40
  • 393
  • 406
Johan
  • 5,003
  • 3
  • 36
  • 50

1 Answers1

7

You've just been lucky.

From the strcpy documentation:

The strcpy() function shall copy the string pointed to by s2 (including the terminating null byte) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

The strings overlap in your case, your program invokes undefined behavior.

A possible reason why it used to work but no longer does is that strcpy could have been implemented as a builtin by GCC like memmove (i.e. safe in this situation), but this changed to a non-safe version for performance reasons. (This is pure speculation.)

To fix it, use memmove rather than strcpy, with something like:

while (t = strstr(p, "//")) {
  memmove(t, t+1, strlen(t)); // strlen(t)'s bytes worth of data
                              // will cover the string bytes left
                              // and the \0 terminator
}

That's not terribly efficient, but it will work portably - memmove has to handle overlapping memory areas.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 3
    Johan was **unlucky** before compiling with gcc 4.4.3 – pmg Sep 23 '11 at 15:24
  • Cool. The codebase is stuffed with similar usages. :) So, now I've implemented my own strcpy to solve the problem. Thanks! – Johan Sep 23 '11 at 15:31
  • 1
    What @pmg said. We should all be blessed with bugs that present themselves in easily observable fashion, instead of lying dormant for years. – Stephen Canon Sep 23 '11 at 15:34