mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] fix return value of wcs{,n}cmp for near-limits signed wchar_t values
Date: Fri, 6 Jan 2023 10:20:10 +0100	[thread overview]
Message-ID: <20230106092010.GA2032@voyager> (raw)
In-Reply-To: <20230104150719.252185-1-gabravier@gmail.com>

On Wed, Jan 04, 2023 at 04:07:19PM +0100, Gabriel Ravier wrote:
>  int wcscmp(const wchar_t *l, const wchar_t *r)
>  {
>  	for (; *l==*r && *l && *r; l++, r++);

I just noticed this line. Isn't the "&& *r" part superfluous? If r is a
prefix of l, then *r and *l will be unequal, and the loop will terminate
because of the first condition alone. (If l is a prefix of r, we need
the second condition to terminate the loop.)

> -	return *l - *r;
> +	return *l < *r ? -1 : *l > *r;
>  }

Ah, that old bug. The problem is that the difference between two 32-bit
values takes up 33 bits to save. I wonder if it would be worth it to
just cast the values to 64 bits, then dividing the result by two. You
know, like

return ((int64_t)*l - *r) >> 1;

Although that does presuppose that wchar_t is smaller than 64 bits,
which the proposed change does not require.

Ciao,
Markus

  reply	other threads:[~2023-01-06  9:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-04 15:07 Gabriel Ravier
2023-01-06  9:20 ` Markus Wichmann [this message]
2023-01-06  9:31   ` Markus Wichmann
2023-01-06 11:17   ` Rich Felker
2023-01-06 16:57     ` Markus Wichmann
2023-01-07  1:51       ` Gabriel Ravier

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=20230106092010.GA2032@voyager \
    --to=nullplan@gmx.net \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).