I'm working my way through the web server and am getting behavior I don't understand. I am consistently receiving a 500 error whatever I try to load in Chrome. I've added printf's (yes, I'm one of those) to output certain variables as I try loading different pages.

The first go through, all seems to look good - no odd characters being displayed - but then I get the 500 error. It then tries to get /favicon.ico and my variables begin displaying bizarre content.

For instance, my request_target is sized to be the number of bytes between the spaces in (i.e., line variable)

char request_target[strrchr(line, ' ') - strchr(line, ' ')]

but when I the value of request_target is displayed as (the value is actually changing each time, this is just the latest run).

I am building request_target as follows:

strncpy(request_target, (strchr(line, ' ') + 1), (strrchr(line, ' ') - strchr(line, ' '));

I get similar behavior in my absolute_path and concatenated path variables because the junk in request_target appears to be carried over.

What fundamental error have I made but missed?


Found the answer to this issue. I was using strncpy, which doesn't place nul character at the end unless it is present in source. Since I was copying a section from the middle of a string, my request_target had no nul, which was messing up subsequent copies and concatenations.

I fixed it by adding '\0' to the last byte in request_target after the copy.

You must log in to answer this question.

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