Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230106092010.GA2032@voyager>
Date: Fri, 6 Jan 2023 10:20:10 +0100
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] fix return value of wcs{,n}cmp for near-limits
 signed wchar_t values

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.