libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] Can I define variable of char array for a page in th


From: Viktor Paramonov
Subject: Re: [libmicrohttpd] Can I define variable of char array for a page in the function containing MHD_create_response_from_buffer with the flag of MHD_RESPMEM_PERSISTENT
Date: Fri, 18 Nov 2022 07:03:42 +0100

Dear Christian 

Could you, please, remove my e-Mail address from this distribution list?

Thanks in advance
Viktor

On Thu, 17 Nov 2022 at 17:10, Christian Grothoff <grothoff@gnunet.org> wrote:
On 11/17/22 14:24, lingweicai via libmicrohttpd wrote:
> Hello Experts,
>
>
> I am developing a web tool, Can I define variable of char array for a page in
> the function containing MHD_create_response_from_buffer, with flag of
> MHD_RESPMEM_PERSISTENT? or I must use the flag of MUST_COPY ?
>
>
> for example:
>
>
> send_page ( char * str )
>
> {
>
>       char page[1024];
>
>       strcpy ( page, str);
>
>       response = MHD_create_response_from_buffer ( strlen(page), (void*) page,
> MHD_RESPMEM_PERSISTENT ) ;
>
>    ...
>
> }

This is NOT ok, as 'page' is on the stack and could change after you
'return', so if you do this, MHD may send random value from your stack
out on the Internet. Not good.

>
> or use this :
>
> static char page[1024];
>
>
>    in multiple threads pool mode ?

Only if you can guarantee that all threads will send exactly the same
data ;-).

What might work is

static __thread char page[1024];

but this is still dangerous if you use MHD in a mode where one thread
handles more than one connection.

So unless you have a very, very good reason to do any of the above, I
would strongly recommend to simply

char *page = malloc();

and use neither MHD_RESPMEM_MUST_COPY nor MHD_RESPMEM_PERSISTENT but
rather MHD_RESPMEM_MUST_FREE -- that way, there is no extra copying
*and* you don't have concurrency / use-after-free issues.

Happy hacking!

Christian


reply via email to

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