public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
	Shuah Khan <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 4/6] tools/nolibc: fix integer overflow in i{64,}toa_r() and
Date: Sat, 19 Apr 2025 11:40:08 +0200	[thread overview]
Message-ID: <20250419094008.GD31874@1wt.eu> (raw)
In-Reply-To: <20250416-nolibc-ubsan-v1-4-c4704bb23da7@weissschuh.net>

On Wed, Apr 16, 2025 at 08:40:19PM +0200, Thomas Weißschuh wrote:
> In twos complement the most negative number can not be negated.

well, if we're picky, that's not really an int overflow since it's only
used as an unsigned in the end, so -2^(N-1) == 2^(N-1) in twos complement.

> @@ -271,16 +271,12 @@ int utoa_r(unsigned long in, char *buffer)
>  static __attribute__((unused))
>  int itoa_r(long in, char *buffer)
>  {
> -	char *ptr = buffer;
> -	int len = 0;
> -
>  	if (in < 0) {
> -		in = -in;
> -		*(ptr++) = '-';
> -		len++;
> +		*(buffer++) = '-';
> +		return 1 + utoa_r(-(unsigned long)in, buffer);
>  	}
> -	len += utoa_r(in, ptr);
> -	return len;
> +
> +	return utoa_r(in, buffer);
>  }

At -Os it's OK but at -O2 it inflates it a little bit and at -O3 it
significantly inflates the function (175 -> 320 bytes) due to the two
calls that get inlined. Have you tried to check if ubsan is happy
with just this?

-		in = -in;
+		in = -(unsigned long)in;

Otherwise this variant doesn't inflate it for me and keeps the spirit
of the original one (i.e. single call):

  int itoa_r3(long in, char *buffer)
  {
        unsigned long uin = in;
        int len = 0;

        if ((long)uin < 0) {
                uin = -uin;
                *(buffer++) = '-';
                len++;
        }
        len += utoa_r(uin, buffer);
        return len;
  }

I'm also seeing a way to make it even smaller than the original by
changing utoa_r() so that it takes the original buffer in a 3rd
argument and emits the difference at the end as the length. This
allows to always perform a tail call, though I'm not sure we really
need it. 
 
Thanks,
Willy

  reply	other threads:[~2025-04-19  9:40 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-16 18:40 [PATCH 0/6] tools/nolibc: fix some undefined behaviour and enable UBSAN Thomas Weißschuh
2025-04-16 18:40 ` [PATCH 1/6] tools/nolibc: add __nolibc_has_feature() Thomas Weißschuh
2025-04-16 18:40 ` [PATCH 2/6] tools/nolibc: disable function sanitizer for _start_c() Thomas Weißschuh
2025-04-19  9:06   ` Willy Tarreau
2025-04-19 10:10     ` Thomas Weißschuh
2025-04-19 10:27       ` Willy Tarreau
2025-04-16 18:40 ` [PATCH 3/6] tools/nolibc: properly align dirent buffer Thomas Weißschuh
2025-04-19  9:11   ` Willy Tarreau
2025-04-19 10:11     ` Thomas Weißschuh
2025-04-16 18:40 ` [PATCH 4/6] tools/nolibc: fix integer overflow in i{64,}toa_r() and Thomas Weißschuh
2025-04-19  9:40   ` Willy Tarreau [this message]
2025-04-19 10:26     ` Thomas Weißschuh
2025-04-16 18:40 ` [PATCH 5/6] selftests/nolibc: disable ubsan for smash_stack() Thomas Weißschuh
2025-04-16 18:40 ` [PATCH 6/6] selftests/nolibc: enable UBSAN if available Thomas Weißschuh
2025-04-19  9:41   ` Willy Tarreau
2025-04-18 17:32 ` [PATCH 0/6] tools/nolibc: fix some undefined behaviour and enable UBSAN Paul E. McKenney
2025-04-18 21:20   ` Thomas Weißschuh
2025-04-18 21:29     ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250419094008.GD31874@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=paulmck@kernel.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox