pax: fix signed-char left-shift UB in base64 encoder
Fix base64 shifts by casting input bytes to unsigned.
This commit is contained in:
1 parent
342d2728d3
commit
27c56a53d5
1 file changed
+7
-7
@@ -2005,7 +2005,7 @@ base64_encode(const char *s, size_t len)
|
||||
'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
|
||||
't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
|
||||
'8','9','+','/' };
|
||||
int v;
|
||||
uint32_t v;
|
||||
char *d, *out;
|
||||
|
||||
/* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
|
||||
@@ -2016,9 +2016,9 @@ base64_encode(const char *s, size_t len)
|
||||
|
||||
/* Convert each group of 3 bytes into 4 characters. */
|
||||
while (len >= 3) {
|
||||
v = (((int)s[0] << 16) & 0xff0000)
|
||||
| (((int)s[1] << 8) & 0xff00)
|
||||
| (((int)s[2]) & 0x00ff);
|
||||
v = ((uint32_t)(unsigned char)s[0] << 16)
|
||||
| ((uint32_t)(unsigned char)s[1] << 8)
|
||||
| (uint32_t)(unsigned char)s[2];
|
||||
s += 3;
|
||||
len -= 3;
|
||||
*d++ = digits[(v >> 18) & 0x3f];
|
||||
@@ -2030,13 +2030,13 @@ base64_encode(const char *s, size_t len)
|
||||
switch (len) {
|
||||
case 0: break;
|
||||
case 1:
|
||||
v = (((int)s[0] << 16) & 0xff0000);
|
||||
v = ((uint32_t)(unsigned char)s[0] << 16);
|
||||
*d++ = digits[(v >> 18) & 0x3f];
|
||||
*d++ = digits[(v >> 12) & 0x3f];
|
||||
break;
|
||||
case 2:
|
||||
v = (((int)s[0] << 16) & 0xff0000)
|
||||
| (((int)s[1] << 8) & 0xff00);
|
||||
v = ((uint32_t)(unsigned char)s[0] << 16)
|
||||
| ((uint32_t)(unsigned char)s[1] << 8);
|
||||
*d++ = digits[(v >> 18) & 0x3f];
|
||||
*d++ = digits[(v >> 12) & 0x3f];
|
||||
*d++ = digits[(v >> 6) & 0x3f];
|
||||
|
||||
Reference in new issue
Block a user