"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Theo Buehler <tb@theobuehler.org>
Subject:
Re: gotwebd: fix cmdline_symset
To:
Omar Polo <op@omarpolo.com>
Cc:
gameoftrees@openbsd.org
Date:
Mon, 5 Sep 2022 19:53:19 +0200

Download raw body.

Thread
On Mon, Sep 05, 2022 at 07:49:37PM +0200, Omar Polo wrote:
> Thomas reported on IRC that -DFOO=bar crashes gotwebd.  There's a typo
> in cmdline_symset where we mangle a pointer instead of writing to what
> it points to.  However, also the computed value is wrong (symset gets
> called with `sym' being "FOO=" instead of "FOO").
> 
> This brings us closer to the usual cmdline_symset.

Looks like a job for strndup().

> 
> diff /home/op/w/got
> commit - 3d8e0c5ede1a5654397b63a9f483d875543527d5
> path + /home/op/w/got
> blob - 342886d6f2c8b12ed3548eb39f4ce4d40be487d1
> file + gotwebd/parse.y
> --- gotwebd/parse.y
> +++ gotwebd/parse.y
> @@ -918,19 +918,16 @@ cmdline_symset(char *s)
>  {
>  	char *sym, *val;
>  	int ret;
> -	size_t len;
>  
>  	val = strrchr(s, '=');
>  	if (val == NULL)
>  		return (-1);
>  
> -	len = strlen(s) - strlen(val) + 1;
> -	sym = malloc(len);
> +	sym = calloc(1, val - s + 1);
>  	if (sym == NULL)
> -		fatal("%s: malloc", __func__);
> +		fatal("%s: calloc", __func__);
>  
> -	memcpy(&sym, s, len);
> -
> +	memcpy(sym, s, val - s);
>  	ret = symset(sym, val + 1, 1);
>  	free(sym);
>  
>