[PATCH 1/1] strings patch for bug 33360

Andrew C Aitchison github@aitchison.me.uk
Sat Sep 6 04:23:58 GMT 2025


On Sat, 6 Sep 2025, Alice Carlotti wrote:

> On Thu, Sep 04, 2025 at 08:27:50AM +0930, Alan Modra wrote:
>> On Wed, Sep 03, 2025 at 11:02:12PM +0100, Alice Carlotti wrote:
>>> On Wed, Sep 03, 2025 at 03:08:33PM +0100, Andrew C Aitchison wrote:
>>>> ---
>>>>  binutils/strings.c | 4 ++--
>>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/binutils/strings.c b/binutils/strings.c
>>>> index f5c022b50a1..38da6381edf 100644
>>>> --- a/binutils/strings.c
>>>> +++ b/binutils/strings.c
>>>> @@ -757,8 +757,8 @@ display_utf8_char (const unsigned char * buffer)
>>>>
>>>>  	case 4:
>>>>  	  printf ("\\u%02x%02x%02x",
>>>
>>> Should 5 digit outputs be allowed?
>>>
>>>> -		  ((buffer[0] & 0x07) << 6) | ((buffer[1] & 0x3c) >> 2),
>>>> -		  ((buffer[1] & 0x03) << 6) | ((buffer[2] & 0x3c) >> 2),
>>>> +		  ((buffer[0] & 0x07) << 2) | ((buffer[1] & 0x30) >> 4),
>>>> +		  ((buffer[1] & 0x0f) << 4) | ((buffer[2] & 0x3c) >> 2),
>>>
>>> Your updated bit arithmetic looks correct to me, but I do wonder whether we
>>> should simplify things by building the whole value into a 32 bit integer and
>>> printing that.  Is there any reason we can't do that?
>>
>> Thanks for reviewing.  I'm going to accept the patch as-is.
>> Improvements, if necessary, can come later.
>
> I've discovered some more bugs.  Firstly, there's exactly the same bug with
> 4-byte encodings in objdump.c and nm.c.

    also readelf.c

>   Secondly, we currently accept various
> invalid encodings (overlong encodings, out-of-range codepoints, and UTF-16
> surrogate pair values).  There's also other bits of code that are unnecessarily
> complicated that I can simplify, including one typo that only happens to not be
> a bug because swapping two bytes at that point doesn't affect output.

Now that I see these new files with the bug and recognise
that \uhhhh and \Uhhhhhhhh come from the C99 standard
I think we must change case 4 to uppercase U and should
print all eight digits, ie \\U%08x or !\\U00%06x

> I'm currently working on a patch (series) to fix and simplify the utf-8 code in
> these files.
>
> Alice
>
>>
>>> Alice
>>>
>>>>  		  ((buffer[2] & 0x03) << 6) | ((buffer[3] & 0x3f)));
>>>>  	  break;
>>>>  	default:
>>>> --
>>>> 2.48.1
>>>>
>>
>> --
>> Alan Modra
>

-- 
Andrew C. Aitchison                      Kendal, UK
                    andrew@aitchison.me.uk


More information about the Binutils mailing list