0

The error function defines phrase as follows:

const char* phrase = NULL;
switch (code)
{
    case 400: phrase = "Bad Request"; break;
    case 403: phrase = "Forbidden"; break;
    case 404: phrase = "Not Found"; break;
    case 405: phrase = "Method Not Allowed"; break;
    case 413: phrase = "Request Entity Too Large"; break;
    case 414: phrase = "Request-URI Too Long"; break;
    case 418: phrase = "I'm a teapot"; break;
    case 500: phrase = "Internal Server Error"; break;
    case 501: phrase = "Not Implemented"; break;
    case 505: phrase = "HTTP Version Not Supported"; break;
}

There is no malloc'ing here, but the string hasn't been given space on the stack has it? I mean, how does this not trigger a segmentation fault?

1

The string hasn't been given space on the stack. What is on the stack is a pointer called phrase. In the switch statement, they are setting this pointer to point to one of the string constants (like "Bad Request"). These string constants are stored as part of the executable file when you compile. (If you were to look at the server executable using a hex viewer (or by using xxd and piping the results to a text file), you would see all of those constant strings.

| improve this answer | |
  • Ahh, thank you! That makes a lot of sense. – Michael F Jan 25 '15 at 21:50

You must log in to answer this question.

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