Skip to contentSkip to conversation

lib/base64: fix -Wconstant-conversion warning in INIT_1()

PATCH In review Rosalie Fang 3 participants 5 messages Nov 3 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/base64: fix -Wconstant-conversion warning in INIT_1()

When expanding BASE64_REV_INIT() for v in 0x7c..0x7f (124..127), the unselected ternary branch (v) - '0' + 52 evaluates to 128..131, which exceeds SCHAR_MAX (127). Recent Clang versions diagnose implicit integer constant conversions in unevaluated ternary arms when initializing the s8 lookup tables:

lib/base64.c:58:18: error: implicit conversion from 'int' to 's8' (aka 'signed char') changes value from 131 to -125 [-Werror,-Wconstant-conversion]

     58 |         [BASE64_IMAP] = BASE64_REV_INIT('+', ',')
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
  lib/base64.c:34:42: note: expanded from macro 'INIT_1'
     34 |                 : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
        |                                              ~~~~~~~~~~^~~~

Explicitly cast the digit-branch expression to s8 to silence the warning.

Fixes c4eb7ad32eab ("lib/base64: optimize base64_decode() with reverse lookup tables")
Assisted-by LLM
---
 lib/base64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/base64.c +1 -1
index 325c7332b049..1489c048b1fd 100644
@@ -31,7 +31,7 @@ static const char base64_tables[][65] = {
 #define INIT_1(v, ch_62, ch_63) \
 	[v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \
 		: (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \
-		: (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
+		: (v) >= '0' && (v) <= '9' ? (s8)((v) - '0' + 52) \
 		: (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1
 
 /*
Signature · 1 line
2.56.0.385.gac671c819a-goog
#1 lore raw

Hi Rosalie,

Quoted 41 lines · Rosalie Fang

When expanding BASE64_REV_INIT() for v in 0x7c..0x7f (124..127), the unselected ternary branch (v) - '0' + 52 evaluates to 128..131, which exceeds SCHAR_MAX (127). Recent Clang versions diagnose implicit integer constant conversions in unevaluated ternary arms when initializing the s8 lookup tables:

lib/base64.c:58:18: error: implicit conversion from 'int' to 's8' (aka 'signed char') changes value from 131 to -125 [-Werror,-Wconstant-conversion]

     58 |         [BASE64_IMAP] = BASE64_REV_INIT('+', ',')
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
  lib/base64.c:34:42: note: expanded from macro 'INIT_1'
     34 |                 : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
        |                                              ~~~~~~~~~~^~~~

Explicitly cast the digit-branch expression to s8 to silence the warning.

Fixes c4eb7ad32eab ("lib/base64: optimize base64_decode() with reverse lookup tables")
Assisted-by LLM
---
 lib/base64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/base64.c +1 -1
index 325c7332b049..1489c048b1fd 100644
@@ -31,7 +31,7 @@ static const char base64_tables[][65] = {
 #define INIT_1(v, ch_62, ch_63) \
 	[v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \
 		: (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \
-		: (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
+		: (v) >= '0' && (v) <= '9' ? (s8)((v) - '0' + 52) \
 		: (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1
 
 /*
Signature · 1 line
2.56.0.385.gac671c819a-goog

Thanks for the change. I avoided this in a different way, which has recently been merged into a tree that should be merged into mainline relatively soon:

https://git.kernel.org/akpm/mm/c/3af83c11cf5990fda0bfe3959aa6391ff903ffdd

For the record, I do hope that

https://github.com/llvm/llvm-project/pull/226775

eventually gets merged so that these workarounds will be unnecessary but I am not sure how that will play out yet.

Signature · 2 lines
Cheers,
Nathan
#2 lore raw
Quoted 4 lines · Nathan Chancellor

Thanks for the change. I avoided this in a different way, which has recently been merged into a tree that should be merged into mainline relatively soon:

Quoted 8 lines

https://git.kernel.org/akpm/mm/c/3af83c11cf5990fda0bfe3959aa6391ff903ffdd

For the record, I do hope that

https://github.com/llvm/llvm-project/pull/226775

eventually gets merged so that these workarounds will be unnecessary but I am not sure how that will play out yet.

Thanks for the pointer, Nathan! Please disregard this patch in favor of 3af83c11cf59 ("lib/base64: silence clang-24 -Wconstant-conversion with diag pragmas").

Thanks,
Rosalie

#3 lore raw
In reply to Nathan Chancellor
Quoted 49 lines · On Tue, 3 Nov 2026 12:00:52 -0700 Nathan Chancellor <[email protected]> wrote:

Hi Rosalie,

Quoted 41 lines · Rosalie Fang

When expanding BASE64_REV_INIT() for v in 0x7c..0x7f (124..127), the unselected ternary branch (v) - '0' + 52 evaluates to 128..131, which exceeds SCHAR_MAX (127). Recent Clang versions diagnose implicit integer constant conversions in unevaluated ternary arms when initializing the s8 lookup tables:

lib/base64.c:58:18: error: implicit conversion from 'int' to 's8' (aka 'signed char') changes value from 131 to -125 [-Werror,-Wconstant-conversion]

     58 |         [BASE64_IMAP] = BASE64_REV_INIT('+', ',')
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
  lib/base64.c:34:42: note: expanded from macro 'INIT_1'
     34 |                 : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
        |                                              ~~~~~~~~~~^~~~

Explicitly cast the digit-branch expression to s8 to silence the warning.

Fixes c4eb7ad32eab ("lib/base64: optimize base64_decode() with reverse lookup tables")
Assisted-by LLM
---
 lib/base64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/base64.c +1 -1
index 325c7332b049..1489c048b1fd 100644
@@ -31,7 +31,7 @@ static const char base64_tables[][65] = {
 #define INIT_1(v, ch_62, ch_63) \
 	[v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \
 		: (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \
-		: (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
+		: (v) >= '0' && (v) <= '9' ? (s8)((v) - '0' + 52) \
 		: (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1
 
 /*
Signature · 1 line
2.56.0.385.gac671c819a-goog

Thanks for the change. I avoided this in a different way, which has recently been merged into a tree that should be merged into mainline relatively soon:

Quoted 1 line

https://git.kernel.org/akpm/mm/c/3af83c11cf5990fda0bfe3959aa6391ff903ffdd

#define INIT_1(v, ch_62, ch_63) \
	[v] = (s8)((v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \
		 : (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \
		 : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
		 : (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1)

probably looks best?

Quoted 6 lines

For the record, I do hope that

https://github.com/llvm/llvm-project/pull/226775

eventually gets merged so that these workarounds will be unnecessary but I am not sure how that will play out yet.

Someone really doesn't like kernel code!
There are other warnings like 'cast of pointer to/from integer of a different size' that it would be nice not to have reported in code that will be optimised away.
At least the kernel gave up on some of the 'type limits' warnings, they had similar problems (even inside __builtin_choose_expr()).

David

(I'm reading these unthreaded...)

#4 lore raw
In reply to Rosalie Fang
Quoted 5 lines · On Tue, 3 Nov 2026 16:34:04 +0000 Rosalie Fang <[email protected]> wrote:

When expanding BASE64_REV_INIT() for v in 0x7c..0x7f (124..127), the unselected ternary branch (v) - '0' + 52 evaluates to 128..131, which exceeds SCHAR_MAX (127). Recent Clang versions diagnose implicit integer constant conversions in unevaluated ternary arms when initializing the s8 lookup tables:

Has that been reported as a compiler bug?

Quoted 25 lines

lib/base64.c:58:18: error: implicit conversion from 'int' to 's8' (aka 'signed char') changes value from 131 to -125 [-Werror,-Wconstant-conversion]

     58 |         [BASE64_IMAP] = BASE64_REV_INIT('+', ',')
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~
  lib/base64.c:34:42: note: expanded from macro 'INIT_1'
     34 |                 : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
        |                                              ~~~~~~~~~~^~~~

Explicitly cast the digit-branch expression to s8 to silence the warning.

Fixes c4eb7ad32eab ("lib/base64: optimize base64_decode() with reverse lookup tables")
Assisted-by LLM
---
 lib/base64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/base64.c +0 -0
index 325c7332b049..1489c048b1fd 100644

lib/base64.c static const char base64_tables[][65] = { · line 35

Quoted 6 lines
lib/base64.c +1 -1
@@ -31,7 +31,7 @@ static const char base64_tables[][65] = {
 #define INIT_1(v, ch_62, ch_63) \
 	[v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \
 		: (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \
-		: (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \
+		: (v) >= '0' && (v) <= '9' ? (s8)((v) - '0' + 52) \
 		: (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1

It might save further trouble to put the (s8) cast on the entire RHS expression.

David

Quoted 2 lines
lib/base64.c +0 -0
 /*