httpd: memory hole in function addEnv() and more

Dirk Clemens develop at cle-mens.de
Sun Sep 4 08:06:49 UTC 2005


Rob Landley wrote:
> On Saturday 03 September 2005 19:00, Dirk Clemens wrote:
>
>>Them memory alloced by asprintf() is never free'd.
>>Solution: insert free(s) behind putenv(s).
>
>
> And since this could be used repeatedly from a long-running script,
it's not
> even a FEATURE_CLEAN_UP free, either.

The problem is a long running http-server (if not startet via inetd).

>
> Too bad there's no obvious way to apply alloca() here.  Hmmm...

We know the maximum needed size of the temp buffer within one
call of sendCgi():

 needed_size = max_env_var_name + maximum( stdlen(uri), MAX_PATH) + 2;

At the very beginning of sendCgi() we could alloca the needed memory:

  const int url_len = strlen(url);
  int envbuf_size = 50 + ( url_len > MAX_PATH ? url_len : MAX_PATH  );
  char * envbuf = alloca(envbuf_size);

And for every env-var:

  snprintf(envbuf,envbuf_size,"NAME=%s",value);
  putenv(envbuf);



> Okay, this function just seems unnecessary.  Just inline the following
at the
> two actual call sites:
>
> if(ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV)
> {
>  char buf[32];
>  sprintf("REMOTE=%u",config->port);
>  putenv(buf);
> }
>

You mean:
  sprintf(buf,"REMOTE_PORT=%u",config->port);
          ^^^        ^^^^^


>>I think that we can save memory and cpu time if we change such lines into
>>    putenv("SERVER_PROTOCOL=HTTP/1.0");
>
>
> I heartily support this cleanup. :)

If you give your ok I will do all this work in this week.
I'll take the httpd.c file from the trunk as startup?

Dirk




More information about the busybox mailing list