C, atoi() generating Segmentation fault

2.4k Views Asked by At

I'm using atoi() to get the status code from a header, but it's not working with the following input:

" 404 Not Found\r\nContent-type: text/html\r\nDate: Thu, 12 Dec 2013 20:53:22 GMT\r\nConnection: close\r\n\r\n"

Shouldn't it stop reading at the first non-numerical character? As described on: http://www-control.eng.cam.ac.uk/~pcr20/www.cppreference.com/stdstring_details.html

atoi() will stop reading from str as soon as a non-numerical character has been read

According to the debugger, the code where the segmentation fault happens:

__NTH (atoi (const char *__nptr))
{
   return (int) strtol (__nptr, (char **) NULL, 10);
}

It's line 280 from stdlib.h, and the value of __nptr is:

__nptr  " 404 Not Found\r\nContent-type: text/html\r\nDate: Thu, 12 Dec 2013 20:53:22 GMT\r\nConnection: close\r\n\r\n" char *

I would like to point out that the following inputs work fine (no segmentation fault):

__nptr  " 404 Not Found\r\nContent-Type: text/html; charset=UTF-8\r\nX-Content-Type-Options: nosniff\r\nDate: Thu, 12 Dec 2013 21:13:24 GMT\r\nServer: sffe\r\nContent-Length: 943\r\nX-XSS-Protection: 1; mode=block\r\nAlternate-Protocol: 80:quic\r\n\r\n"   char *

__nptr  " 302 Found\r\nCache-Control: no-cache, no-store, must-revalidate\r\nPragma: no-cache\r\nExpires: 0\r\nLocation: http://br.godaddy.com/\r\nServer: Microsoft-IIS/7.0\r\nSet-Cookie: MemBotChk=false; path=/\r\nSet-Cookie: countrysite1=www; domain=godaddy.com; expires=Fri, 12-Dec-2014 21:15:09 GMT; path=/\r\nSet-Cookie: language1=pt-BR; domain=godaddy.com; expires=Fri, 12-Dec-2014 21:15:09 GMT; path=/\r\nP3P: policyref="/w3c/p3p.xml", CP="COM CNT DEM FIN GOV INT NAV ONL PHY PRE PUR STA UNI IDC CAO OTI DSP COR C..."    char *

Actually, all inputs so far worked all right except for the one I mentioned in the beginning. What could be causing segmentation fault?

Removing the leading space doesn't make a difference. I also tried to add a null terminator after the response code, same thing happened. So I believe it's not atoi(), but something else. How to identify the problem?


Valgrind results:

Invalid read of size 1 in main in main.c:23

Address 0xf is not stack'd, malloc'd or (recently) free'd

  • 1: __strtol_l_internal in /build/eglibc-hkB3nk/eglibc-2.17/stdlib/../stdlib/strtol_l.c:298
  • 2: get_web_content in /usr/include/stdlib.h:280
  • 3: main in main.c:23

main.c:23 is just a call to get_web_content()


The problem was that atoi() was called with a null pointer later.

1

There are 1 best solutions below

1
On

It's a beginner's mistake really. in order to allocate memory for the response body, I called strcasestr to find the Content-Length: field. Only I didn't check whether or not the field had been found. The thing I don't get is why the debugger was showing the previous call to atoi().

In case anyone with the same problem happens to stumble upon this question here's what I was doing wrong:

fill_this->content_length = atoi(strcasestr(header_string + i, "Content-Length:") + 15);   

And the solution:

char *temp = strcasestr(header_string + i, "Content-Length:");
if(temp == NULL)
   return;
fill_this->content_length = atoi(temp + 15);