0

Okay so I have seen a few implementations of the strcat function with memcpy. I understand that it is efficient, since there no need to allocate. But how do you preserve overwriting the contents of the source string with the resultant string.

For example lets take-:

char *str1 = "Hello";
char *str2 = "World";

str1 = strcat(str1, str2);

How do I ensure that in str2 isn't overwritten with the contents of the resultant "HelloWorld" string ?

Also if strings are nothing but char arrays, and arrays are suppose to have a fixed size then without reallocation of memory if I copy bytes into the array that are larger than the array, then isn't that unsafe ?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ng.newbie
  • 2,807
  • 3
  • 23
  • 57
  • An example that *only* invoked UB because of the overlap would have likely been more to the point of what I *think* your question really is, assuming you understand how `strcat` works (it *never* allocates memory). – WhozCraig Dec 15 '16 at 05:39
  • Do you know what a Pointer is? Do you know what a String Literar is? – Michi Dec 15 '16 at 05:47
  • We just had a question about implementing `strcat()` on [Code Review](http://codereview.stackexchange.com/questions/149812/strcat-implementation). OP used `malloc()`, so there was some discussion of this versus the Standard Library approach. – ad absurdum Dec 15 '16 at 06:45
  • 1
    @DavidBowling yeah I guess my main point of confusion was that I did not realize that you had to have enough space in the destination string to hold the concatenated string. – ng.newbie Dec 15 '16 at 06:56

2 Answers2

1

It's not about unsafe, it's undefined behavior.

First of all, you're trying to modify a string literal, which inherently invokes UB.

Secondly, regarding the size of the destination buffer, quoting the man page (emphasis mine)

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • yeah so why would you use memcpy if it gives you undefined behavior ? why not just allocate a new array with malloc and use memcpy there. – ng.newbie Dec 15 '16 at 05:38
  • 1
    @ng.newbie that isn't how `strcpy` is designed to work. If that is what *you* want to do (dynamic allocation, two buffer copies, and hopefully, someday, a `free`call), great, but that isn't how `strcpy` is designed, so it isn't shocking it doesn't behave that way. You only get undefined behavior if you use the function wrong. That isn't a flaw of the function; it's a flaw of the engineer using it. – WhozCraig Dec 15 '16 at 05:44
1

I understand that it is efficient, since there no need to allocate.

That's an incorrect understanding. Neither memcpy nor strcat allocates memory. Both require that you pass pointers that point to sufficient amount of valid memory. If that is not the case, the program is subject to undefined behavior.

Your posted code is subject to undefined behavior for couple of reasons:

  1. str1 points to a string literal, which is in read-only portion of the program.

  2. str1 does not enough memory to hold the string "HelloWorld" and the terminating null character.

R Sahu
  • 204,454
  • 14
  • 159
  • 270