Both of the code snippet invoke undefined behavior. In the first snippet s1 has not enough space to hold any other character than six characters.
In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.
Read strcat man page:
Answer from haccks on Stack Overflow[...] The strings may not overlap, and the
deststring must have enough space for the result. Ifdestis not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.
Videos
Both of the code snippet invoke undefined behavior. In the first snippet s1 has not enough space to hold any other character than six characters.
In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.
Read strcat man page:
[...] The strings may not overlap, and the
deststring must have enough space for the result. Ifdestis not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.
In C the function strcat does not create a new character array containing concatenated strings. It appends characters from the second string to the first string of the first character array provided that it has enough elements to store the new characters. Otherwise the function will try to overwrite the memory beyond the character array that results in undefined behavior.
So a valid use of the function in the first program can look the following way
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char s1[11] = "12345";
char s2[] = "abcde";
strcat(s1, s2);
puts(s1);
puts(s2);
return 0;
}
In this program the character array is declared as having 11 elements. Thus it is able to accommodate the appended string "abcde".
In the second program there is an attempt to modify the string literal pointed to by the pointer s1. String literals in C and C++ are immutable. Any attempt to change a string literal results in undefined behavior even though in C opposite to C++ string literals have types of non-constant character arrays.
From the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
So in the second program you again need to use a character array with enough elements. For example
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char s1[11] = "12345";
char* s2 = "abcde";
strcat(s1, s2);
puts(s1);
puts(s2);
return 0;
}
Or you could use either a Variable Length Array (VLA) if the compiler supports them or dynamically allocate an array. For example
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
char *s1 = "12345";
char* s2 = "abcde";
char s3[strlen( s1 ) + strlen( s2 ) + 1];
strcpy( s3, s1 );
strcat( s3, s2 );
puts(s1);
puts(s2);
puts(s3);
return 0;
}
Or
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[]) {
char *s1 = "12345";
char* s2 = "abcde";
char *s3 = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
if ( s3 != NULL )
{
strcpy( s3, s1 );
strcat( s3, s2 );
puts(s1);
puts(s2);
puts(s3);
}
free( s3 );
return 0;
}
Change
char *message = "\n\nHTTP/1.1 ";
to
char message[1024];
strcpy(message,"\n\nHTTP/1.1 ");
and you should be ok, up to a total message length of 1023.
Edit: (as per mjy's comment). Using strcat in this fashion is a great way of getting buffer overflows. You could readily write a small function that checks the size of the buffer and length of incoming string addition to overcome this, or use realloc on a dynamic buffer. IMO, the onus is on the programmer to check correct buffer sizes where they are used, as with sprintfs and other C strings functions. I assume that C is being used over C++ for performance reasons, and hence STL is not an option.
Edit: As per request from Filip's comment, a simple strcat implementation based on a fixed size char buffer:
char buffer[MAXSIZE] = "";
int mystrcat(char *addition)
{
if (strlen(buffer) + strlen(addition) + sizeof(char) >= MaxSize)
return(FAILED);
strcat(buffer,addition);
return(OK);
}
Using dynamic allocation:
char *buffer = NULL;
int mystrcat(char *addition)
{
buffer = realloc(buffer, strlen(buffer) + strlen(addition) + sizeof(char));
if (!buffer)
return(FAIL);
strcat(buffer, addition);
return(OK);
}
In this case you have to free your buffer manually when you are finished with it. (Handled by destructors in C++ equivalents)
Addendum (Pax):
Okay, since you didn't actually explain why you had to create message[1024], here it is.
With char *x = "hello", the actual bytes ('h','e','l','l','o',0) (null on the end) are stored in an area of memory separate from the variables (and quite possibly read-only) and the variable x is set to point to it. After the null, there's probably something else very important. So you can't append to that at all.
With char x[1024]; strcpy(x,"hello");, you first allocate 1K om memory which is totally dedicated to x. Then you copy "hello" into it, and still leave quite a bit of space at the end for appending more strings. You won't get into trouble until you append more than the 1K-odd allowed.
End addendum (Pax):
I wonder why no one mentioned snprintf() from stdio.h yet. That's the C way to output multiple values and you won't even have to convert your primitives to strings beforehand.
The following example uses a stack allocated fixed-sized buffer. Otherwise, you have to malloc() the buffer (and store its size), which would make it possible to realloc() on overflow...
char buffer[1024];
int len = snprintf(buffer, sizeof(buffer), "%s %i", "a string", 5);
if(len < 0 || len >= sizeof(buffer))
{
// buffer too small or error
}
Edit: You might also consider using the asprintf() function. It's a widely available GNU extension and part of TR 24731-2 (which means it might make it into the next C standard). The example from above would read
char * buffer;
if(asprintf(&buffer, "%s %i", "a string", 5) < 0)
{
// (allocation?) error
}
Remember to free() the buffer when done using it!
I'm trying to make my own strcat method in C, but it's not working. Will someone help me out here?
char *strcat(char *s1, char *s2)
{
int size=strlen(s1)+strlen(s2);
char str[size];
int i=0;
for (int x=0; x<strlen(s1); x++)
{
str[x]=s1[x];
}
for (int y=strlen(s1); y<size; y++)
{
str[y]=s2[i];
i++;
}
str[size]='\0';
return str;
}