All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] utf8: fixed bit shift larger than type size
@ 2022-02-23 23:30 James Prestwood
  0 siblings, 0 replies; only message in thread
From: James Prestwood @ 2022-02-23 23:30 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 992 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.
---
 ell/utf8.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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] only message in thread

only message in thread, other threads:[~2022-02-23 23:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-23 23:30 [PATCH] utf8: fixed bit shift larger than type size 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.