0

I'm a newbie of C. Here I write a function to parse http post from browser. Here is my code:

char* HTTP_POST_GET_BODY(char* HttpPost){

     char* HttpPostBody = strstr(HttpPost,"\r\n\r\n");
     HttpPostBody = HttpPostBody + 4;
     if(strcmp(HttpPostBody,"\r\n\r\n") != 0 && strcmp(HttpPostBody,"") != 0){
         return HttpPostBody;
     }
     else{
        char* HttpPostBody_IE;
        HttpPostBody = strstr(HttpPost,"::");
        char* HttpPostBodyEnd = strstr(HttpPost,"HTTP/1.1");
        int body_length = HttpPostBodyEnd - HttpPostBody;
        strncpy(HttpPostBody_IE,HttpPostBody+2,body_length-2);
        return HttpPostBody_IE;
     }

}

So basically, if the procedure goes in the "else" it should return a char pointer to caller. I check the debugger. HttpPostBody_IE has a value but when it return it is a null string.

char* http_body = HTTP_POST_GET_BODY(recieve_buffer);

Anyone has an idea about it?

Pwan
  • 153
  • 13
  • Try to think about `char* HttpPostBody_IE;` What is pointing this pointer....? Which is the scope of the pointer? – LPs Aug 26 '16 at 07:13
  • What do you mean? Is it because I didnt allocate space for it? – Pwan Aug 26 '16 at 07:14
  • I try to malloc a space to it but If I free it then it cant be return. If I just do the return and the memory is not freeing – Pwan Aug 26 '16 at 07:16
  • Why do you want to free a memory that you want to use later?.... Non sense. `malloc` it and free it as far as `http_body` is not more required to be used – LPs Aug 26 '16 at 07:18
  • Braces '{' and '}' define "scope". Variables declared within a "scope" will be "out of scope" outside the braces. Typically such variables reside in stack or registers, the contents of which may be discarded when you leave the "scope". – S.C. Madsen Aug 26 '16 at 07:18

1 Answers1

2

You declare the pointer-variable HttpPostBody_IE but never allocate memory for it.
The call to strncpy(....) should create a core dump. Try this:

int body_length = HttpPostBodyEnd - HttpPostBody;
HttpPostBody_IE = (char*)malloc(body_length+1);
strncpy(HttpPostBody_IE,HttpPostBody+2,body_length-2);
return HttpPostBody_IE;

Of course, make sure the caller of this functions releases the allocated memory afterwards.
You have a problem in case the function returns from within the if statement. This because no memory is allocated in that case.

You might resolve it this way:

static char HttpPostBody_IE[BIG_ENOUGH_FOR_ANY_SOURCE];
if (....)
{
  ...
}
else
{
  ...
  strncpy(HttpPostBody_IE, ...);
  return (HttpPostBody_IE);
}

Please notice that in this way the variable needs to be static.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • ...and check `malloc` return....and do not cast `malloc` return in [tag:c] – LPs Aug 26 '16 at 07:19
  • ye but how i gonna free this space? – Pwan Aug 26 '16 at 07:20
  • @LPs What do you mean do not cast malloc return in c? – Pwan Aug 26 '16 at 07:20
  • free(...); It may cause problems; I just edited my answer – Robert Kock Aug 26 '16 at 07:21
  • ye. I know free(...). but What can i do if i dont wanna free the space outside the function. – Pwan Aug 26 '16 at 07:22
  • @Pwan `malloc` return `void *` that is fine for whatever pointer using [tag:c]. Take a look [at this SO post](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LPs Aug 26 '16 at 07:23
  • @Pwan So do not free it. Free it at the end of program. What's the problem. – LPs Aug 26 '16 at 07:28
  • @LPs: what if the program is running for ever in background? You get an out-of-memory error. – Robert Kock Aug 26 '16 at 07:29
  • If the function is called more that 1 time, just use realloc since the first call amd that's all. BTW I meant: if you want a global scoped variable declare a global scoped variable in module. Declaring a local static variable to be exported is non-sense and source of future misunderstanding. – LPs Aug 26 '16 at 07:32