U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <ravi@prevas.dk>
To: Jerome Forissier <jerome.forissier@linaro.org>
Cc: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>,
	 Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	 u-boot@lists.denx.de
Subject: Re: [PATCH 1/3] lib: implement strnstr()
Date: Mon, 06 Jan 2025 11:14:13 +0100	[thread overview]
Message-ID: <87msg41c7e.fsf@prevas.dk> (raw)
In-Reply-To: <196cc091-5031-426e-b5ea-923065b2b026@linaro.org> (Jerome Forissier's message of "Mon, 6 Jan 2025 08:44:24 +0100")

On Mon, Jan 06 2025, Jerome Forissier <jerome.forissier@linaro.org> wrote:

>> + *
>> + * Return:	pointer to the first occurrence or NULL
>>   */
>> -char * strstr(const char * s1,const char * s2)
>> +char *strnstr(const char *s1, const char *s2, size_t len)
>>  {
>> -	int l1, l2;
>> +	size_t l1, l2;
>>  
>> +	l1 = strnlen(s1, len);
>>  	l2 = strlen(s2);
>> -	if (!l2)
>> -		return (char *) s1;
>> -	l1 = strlen(s1);
>> -	while (l1 >= l2) {
>> -		l1--;
>> -		if (!memcmp(s1,s2,l2))
>> +
>> +	for (; l1 >= l2; --l1, ++s1) {
>> +		if (!memcmp(s1, s2, l2))
>>  			return (char *) s1;
>> -		s1++;
>>  	}
>> +
>>  	return NULL;
>>  }
>>  #endif
>
> This won't return s1 when s2 == NULL, will it?

Why should it? That's a broken caller. You can't call str* functions
with NULL pointers. It's ok for s2 to point at an empty string, in which
case l2 is 0, so the very first memcmp() is guaranteed to succeed and
yes, this will return s1 in that case.

As passing "" as s2 is likely quite rare, the current short-circuiting
of that case is pointless.

> Why not use a known good implementation such as [1]?
>
> [1] https://github.com/freebsd/freebsd-src/blob/main/lib/libc/string/strnstr.c

Licensing? Also, that's quite unreadable, so please don't.

Rasmus

  reply	other threads:[~2025-01-06 10:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-03 23:21 [PATCH 0/3] net: use strnstr() for lwip_strnstr() Heinrich Schuchardt
2025-01-03 23:21 ` [PATCH 1/3] lib: implement strnstr() Heinrich Schuchardt
2025-01-06  7:44   ` Jerome Forissier
2025-01-06 10:14     ` Rasmus Villemoes [this message]
2025-01-06 10:22       ` Jerome Forissier
2025-01-03 23:21 ` [PATCH 2/3] test: unit tests for strstr() and strnstr() Heinrich Schuchardt
2025-01-09  7:53   ` Ilias Apalodimas
2025-01-03 23:21 ` [PATCH 3/3] net: use strnstr() for lwip_strnstr() Heinrich Schuchardt
2025-01-06  7:50   ` Jerome Forissier
2025-01-21  1:05   ` Tom Rini
2025-01-21  2:18     ` Heinrich Schuchardt
2025-01-21  2:33       ` Tom Rini
2025-01-20 20:52 ` [PATCH 0/3] " Tom Rini
2025-01-20 20:54   ` Tom Rini

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=87msg41c7e.fsf@prevas.dk \
    --to=ravi@prevas.dk \
    --cc=heinrich.schuchardt@canonical.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jerome.forissier@linaro.org \
    --cc=u-boot@lists.denx.de \
    /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