0

I've browsed the questions asked, many of them helped to fix my code but I'm facing a last challenge and I've no idea how to pass it.

Every test made with "cat.jpg", "cat.html" or "hello.html" works perfectly. Things get messed up only when a word is entered in "hello.html", page fails to load and a segmentation fault is prompted in my terminal.

The bit of code related to the query extraction has been many times tested in another code sheet just to verify that my query array is filled with the part of request_target right after the question mark (nth position stored in int qmarkPos).

Here it is:

char query[] = {};

if (qLen == 0)
{
    query[0] = '\0';
}
else
{
    strncpy(query, &request_target[qmarkPos + 1], qLen);
    query[qLen] = '\0';
}

If I specify a length previously calculated and stored in int qLen, I can't load anymore my static contents and I'm notified about a segmentation fault here too.

I can feel that my code for extracting the query is incomplete. When I type a word in "hello.html", does the code run again and refresh my query array? When "hello.html" loads successfully, query = '\0'and if I type "Alice", I should see query being updated to query = "name=Alice". The only thing to me that can explain why I'm having a segmentation fault is that I don't overwrite properly query but after many tries, I still can't figure out how to do.

Am I guessing right? Any clue/tip would be greatly appreciated. Thanks!

2
  • First instinct is that since you don't specify a size, maybe the OS is only giving you enough space for one '\0' and then returning the pointer to that address into query. Then when you try to copy in your request target, maybe it's trying to write over other important stuff in that stack frame? Have you tried to use malloc() or alternatively specify a generous or dynamic length for your char array? – Sam Gerber Sep 26 '15 at 2:40
  • @Sam Gerber I've tried to specify a length using my int qLen but if I write char query[qLen]; instead of char query[] = {}; I get here again a seg fault. I've not tried a malloc() yet as I thought it wouldn't work when the user hit a CTRL+C to quit the program while running. Well, I'm still trying things, if anybody has another guess or tip to share, you're welcome. Meanwhile, I'm trying to debug it and if anything new occurs, I'll let you know. – DFATPUNK Sep 26 '15 at 17:05
0

Alright, following Sam Gerber's comment, I've tried to allocate space to my query array and turns out this works now perfectly.

I think I've not allocate enough space when I was using my int qLen to specify the space needed in my array but replacing char query[] = {}; by char query[qLen + 1]; fixed my code and everything's working perfectly now.

Now I've found what was going wrong, I'm very confused by the method.

Here's the bit of code for qLen

// save query's length
int qLen = 0;
if (qmarkPos != 0)
{
    qLen = rtLen - qmarkPos - 1; 
}     

// TODO: extract query from request-target
char query[qLen + 1];

In a previous loop, I've iterated through the entire request_target to check where the question mark is. If found, its index is stored in int qmarkPos while int rtLen saves the length of request_target

So qLen calculated as shown above equals the exact numbers of characters query has. The first error I notice now is quite stupid: I was indexing a '\0' at query[qLen] while query was qLen long.

The bizarre logic I don't get here is that I've tried to run my web server using this bit of code instead of the first:

// save query's length
int qLen = 0;
if (qmarkPos != 0)
{
    qLen = rtLen - qmarkPos; 
}    

// TODO: extract query from request-target
char query[qLen];

By removing the - 1 to qLen I have the exact amount of spaces required for my query, including '\0' for my query[qLen] but in this fashion, the code compiles but a seg fault is prompted in the terminal when trying to load any content.

Does someone understand why these details in this particular order is so important for running the web server?

1
  • For this request-target: /hello.php?q=A what values do you get for rtlen and qmarkPos? I'm going to assume you get 11 for qmarkPos and 14 for rtLen. So qLen would be 3, which is not enough for {'q', '=', 'A', '\0}. Are you sure you shouldn't be calculating qLen as the bottom method and declaring query with qLen+1? Have you used gdb or printf to check on all the values? To be sure, I don't understand how adding one to both expressions would change anything... – Sam Gerber Sep 26 '15 at 23:12

You must log in to answer this question.

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