libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Strange char 0x7f added after last POST notification


From: Imóveis Nacionais
Subject: Re: [libmicrohttpd] Strange char 0x7f added after last POST notification
Date: Mon, 13 Apr 2020 11:25:35 +0100

Ops... :-(
Thank you very much!

On Mon, Apr 13, 2020 at 10:38 AM Christian Grothoff <address@hidden> wrote:
You're using strncpy(), which does NOT add the 0-terminator at the end
that printf() expects.

On 4/13/20 11:27 AM, Imóveis Nacionais wrote:
> But I am just handling arrived data when *upload_data_size > 0. Here is
> the code:
>
> else if(strcmp(method, "POST") == 0)
> {
>         ST_PER_THREAD_DATA* pstThreadData = *ptr;
>         if( pstThreadData == NULL)
>         {                  
>             ST_PER_THREAD_DATA * cThreadData = (ST_PER_THREAD_DATA*)
> malloc(sizeof(ST_PER_THREAD_DATA));
>             cThreadData->nThreadID = nThID;
>             cThreadData->strData[0]='\0';
>
>             *ptr = cThreadData;
>             return MHD_YES;  
>         }
>
>         if(*upload_data_size > 0)
>         {
>             printf("Before strcpy (%s) (%s)
> (%d)\n",pstThreadData->strData,upload_data,*upload_data_size);
>             strncpy(pstThreadData->strData,upload_data,*upload_data_size);
>             printf("After strcpy (%s) (%s)
> (%d)\n",pstThreadData->strData,upload_data,*upload_data_size);
>
>             *upload_data_size = 0;
>             return MHD_YES;
>
>             //After strcpy ({"key98"}) ({"key98"}) (9)
>         }
>         else
>         {
>             //END OF POST DATA GATHERING            
>             printf("In the end of post we have (%s) (%d)
> \n",pstThreadData->strData, strlen(pstThreadData->strData));
>             HandleClientPost(pstThreadData->strData))
>             //In the end of post we have ({"key98"}) (10)
> ...
>
> Please, is there anything wrong with the (testing) code?
>
> Thank you
>
>
>
> On Mon, Apr 13, 2020 at 10:00 AM Christian Grothoff <address@hidden
> <mailto:address@hidden>> wrote:
>
>     On 4/13/20 2:11 AM, Imóveis Nacionais wrote:
>     > Then when in the "*upload_data_size = 0" situation, using a printf
>     > sometimes I can see that the thread context data has the 0x7f char
>     appended
>     > and of course the strlen now is one char extra. This happens
>     between the
>     > last post call with *upload_data_size > 0 and *upload_data_size = 0.
>     >
>     > Does this char has any meaning in the libmicrohttpd context?
>     > Why is this char being added?
>
>     You're seeing undefined behavior. When '*upload_data_size' is zero, you
>     are not allowed to look into '*upload_data'. It is NEVER warranted to be
>     a 0-terminated char* in the first place, and right now you're just
>     reading data out of bounds.  Do not rely on getting 0x7f or anything
>     meaningful, in fact, expect reading more than '*upload_data_size' bytes
>     from '*upload_data' to be deadly.
>
>     Happy hacking!
>
>     Christian
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]