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

From:
Omar Polo <op@omarpolo.com>
Subject:
Re: move got_pkt_readlen() to separate function
To:
Tobias Heider <tobias.heider@stusta.de>
Cc:
gameoftrees@openbsd.org
Date:
Wed, 17 Apr 2024 10:41:01 +0200

Download raw body.

Thread
> +const struct got_error *
> +got_pkt_readlen(int *len, const char *str, int chattygot)
> +{
> [...]
> +	errno = 0;
> +	*len = strtol(str, &e, 16);
> +	if (str[0] == '\0' || *e != '\0')
> +		return got_error(GOT_ERR_BAD_PACKET);
> +	if (errno == ERANGE || *len > INT_MAX || *len < INT_MIN)
> +		return got_error_msg(GOT_ERR_BAD_PACKET, "bad pkt-line length");

I'm not sure this bit is correct.  You're storing a long inside a int,
then check if it's bigger than INT_MAX or lesser than INT_MIN, which is
impossible.

The original was

> -	errno = 0;
> -	len = strtol(lenstr, &e, 16);
> -	if (lenstr[0] == '\0' || *e != '\0')
> -		return got_error(GOT_ERR_BAD_PACKET);
> -	if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
> -		return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
> -	if (len > INT_MAX || len < INT_MIN)
> -		return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");

which is closer to the example code in strtol(3) (actually more readable
than the EXAMPLE section.)