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: Shuah Khan <skhan@linuxfoundation.org>,
	Shuah Khan <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: add support for [v]sscanf()
Date: Sat, 3 Aug 2024 12:16:25 +0200	[thread overview]
Message-ID: <20240803101625.GH29127@1wt.eu> (raw)
In-Reply-To: <5db920e0-51e8-48d9-b0ae-95479e875fad@t-8ch.de>

On Fri, Aug 02, 2024 at 05:48:13PM +0200, Thomas Weißschuh wrote:
> > With all these libc functionality added, it isn't nolibc looks like :)
> 
> Well :-)
> 
> The main motivation is to provide kselftests compatibility.
> Maybe Willy disagrees.

No no I'm perfectly fine with adding the functions that developers use
or need to write their test or init tools. I don't have any strong
opinion on scanf(). Just like strtok(), I stopped using it 25 years ago
when I noticed that it never survives code evolutions, lacks a lot of
flexibility and is often strongly tied to your types (more than printf
where you can cast). But I perfectly understand that others are used to
it and would appreciate to have it, for example if it helps with command
line arguments.

> > > +static int test_scanf(void)
> > > +{
> > > +	unsigned long long ull;
> > > +	unsigned long ul;
> > > +	unsigned int u;
> > > +	long long ll;
> > > +	long l;
> > > +	void *p;
> > > +	int i;
> > > +
> > > +	if (sscanf("", "foo") != EOF)
> > > +		return 1;
> > > +
> > > +	if (sscanf("foo", "foo") != 0)
> > > +		return 2;
> > > +
> > > +	if (sscanf("123", "%d", &i) != 1)
> > > +		return 3;
> > > +
> > > +	if (i != 123)
> > > +		return 4;
> > > +
> > > +	if (sscanf("a123b456c0x90", "a%db%uc%p", &i, &u, &p) != 3)
> > > +		return 5;
> > > +
> > > +	if (i != 123)
> > > +		return 6;
> > > +
> > > +	if (u != 456)
> > > +		return 7;
> > > +
> > > +	if (p != (void *)0x90)
> > > +		return 8;
> > > +
> > > +	if (sscanf("a    b1", "a b%d", &i) != 1)
> > > +		return 9;
> > > +
> > > +	if (i != 1)
> > > +		return 10;
> > > +
> > > +	if (sscanf("a%1", "a%%%d", &i) != 1)
> > > +		return 11;
> > > +
> > > +	if (i != 1)
> > > +		return 12;
> > > +
> > > +	if (sscanf("1|2|3|4|5|6",
> > > +		   "%d|%ld|%lld|%u|%lu|%llu",
> > > +		   &i, &l, &ll, &u, &ul, &ull) != 6)
> > > +		return 13;
> > > +
> > > +	if (i != 1 || l != 2 || ll != 3 ||
> > > +	    u != 4 || ul != 5 || ull != 6)
> > > +		return 14;
> > > +
> > > +	return 0;
> > 
> > Can we simplify this code? It is hard to read code with too
> > many conditions. Maybe defining an array test conditions
> > instead of a series ifs.
> 
> I tried that and didn't find a way.
> Any pointers are welcome.

I think it would be difficult by nature of varargs.

However, since you grouped some expressions, maybe a one-liner comment
between each scanf() to explain the intent of the test would make it
easier to follow. E.g:
    /* test multiple naked numbers */
    ...
    /* test numbers delimited with a character */
    ...
    /* test multiple integer types at once */

etc. This allows the reviewer to more easly re-focus on the test they
were reading.

Willy

  parent reply	other threads:[~2024-08-03 10:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-31 18:32 [PATCH 0/2] tools/nolibc: add support for [v]sscanf() Thomas Weißschuh
2024-07-31 18:32 ` [PATCH 1/2] " Thomas Weißschuh
2024-07-31 23:01   ` Shuah Khan
2024-08-02 15:48     ` Thomas Weißschuh
2024-08-02 21:20       ` Shuah Khan
2024-08-03 10:16       ` Willy Tarreau [this message]
2024-07-31 18:32 ` [PATCH 2/2] Revert "selftests: kselftest: Fix build failure with NOLIBC" Thomas Weißschuh
2024-07-31 22:56 ` [PATCH 0/2] tools/nolibc: add support for [v]sscanf() Shuah Khan

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=20240803101625.GH29127@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.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