All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH v2] utf8: fixed bit shift larger than type size
@ 2022-02-24  0:05 Denis Kenzior
  0 siblings, 0 replies; 2+ messages in thread
From: Denis Kenzior @ 2022-02-24  0:05 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 809 bytes --]

Hi James,

On 2/23/22 17:51, James Prestwood wrote:
> The C standard requires the bit shift result value width to be greater
> than or equal to the number of bits being shifted, otherwise the behavior
> is undefined. In this case str[0] was being cast to an unsigned char
> (1 byte) but being shifted 24 bits. This resulted, on some compilers,
> with a runtime error:
> 
> runtime error: left shift of 255 by 24 places cannot be represented in type 'int'
> 
> Casting str[0] to an unsigned int satisfies the width requirements
> for this bit shift.
> 
> Fixes: 9843abcd84bc ("utf8: cast to avoid left shift of negative value (ubsan)")
> ---
>   ell/utf8.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> v2:
>   * Added Fixes tag
> 

Applied, thanks.

Regards,
-Denis

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH v2] utf8: fixed bit shift larger than type size
@ 2022-02-23 23:51 James Prestwood
  0 siblings, 0 replies; 2+ messages in thread
From: James Prestwood @ 2022-02-23 23:51 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1103 bytes --]

The C standard requires the bit shift result value width to be greater
than or equal to the number of bits being shifted, otherwise the behavior
is undefined. In this case str[0] was being cast to an unsigned char
(1 byte) but being shifted 24 bits. This resulted, on some compilers,
with a runtime error:

runtime error: left shift of 255 by 24 places cannot be represented in type 'int'

Casting str[0] to an unsigned int satisfies the width requirements
for this bit shift.

Fixes: 9843abcd84bc ("utf8: cast to avoid left shift of negative value (ubsan)")
---
 ell/utf8.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

v2:
 * Added Fixes tag

diff --git a/ell/utf8.c b/ell/utf8.c
index 84ab774..ceda1de 100644
--- a/ell/utf8.c
+++ b/ell/utf8.c
@@ -98,7 +98,7 @@ LIB_EXPORT int l_utf8_get_codepoint(const char *str, size_t len, wchar_t *cp)
 		return 1;
 	}
 
-	expect_bytes = __builtin_clz(~((unsigned char)str[0] << 24));
+	expect_bytes = __builtin_clz(~((unsigned int)str[0] << 24));
 
 	if (expect_bytes < 2 || expect_bytes > 4)
 		goto error;
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-24  0:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-24  0:05 [PATCH v2] utf8: fixed bit shift larger than type size Denis Kenzior
  -- strict thread matches above, loose matches on Subject: below --
2022-02-23 23:51 James Prestwood

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.