This is my code to extract the filename requested from the server from the request message:

char* buffer2 = strchr(buffer1, '/');
char* buffer3 = strchr(buffer2, ' ');

char* buffer4 = malloc(strlen(buffer2) - strlen(buffer3));
if (!buffer4)
    return false;
strncpy(buffer4, buffer2, (strlen(buffer2) - strlen(buffer3)));

Later on, buffer4 is copied to abs_path.

This code works perfectly the first or second time the image is requested, but after that buffer4 becomes things like " /cat.jpgjpg�]r" instead of just "/cat.jpg".

I always free buffer4 later after copying it to abs_path. Any suggestions on why this is happening? many thanks in advance


While this may not be the only problem, the math is "backwards". Remember, buffer3 is greater than buffer2 (found later in the string). So, buffer2 - buffer3 would not be the result you are after. Also, make sure to allocate enough memory for the null terminator. The reason it works sometimes: memory is fickle. :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .