This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pack(): avoid << of negative values
authorDavid Mitchell <davem@iabyn.com>
Mon, 22 Dec 2014 20:12:22 +0000 (20:12 +0000)
committerDavid Mitchell <davem@iabyn.com>
Wed, 31 Dec 2014 11:28:52 +0000 (11:28 +0000)
Treat the string as U8* rather than char* when doing all the
bit shifts for uuencode. That stops these warnings under ASan:

    pp_pack.c:1890:34: runtime error: left shift of negative value -127
    pp_pack.c:1891:34: runtime error: left shift of negative value -126
    pp_pack.c:1899:34: runtime error: left shift of negative value -1
    pp_pack.c:1900:30: runtime error: left shift of negative value -31

pp_pack.c

index 9ed8c59..60462eb 100644 (file)
--- a/pp_pack.c
+++ b/pp_pack.c
@@ -1882,7 +1882,7 @@ PP(pp_unpack)
 }
 
 STATIC U8 *
-doencodes(U8 *h, const char *s, I32 len)
+doencodes(U8 *h, const U8 *s, I32 len)
 {
     *h++ = PL_uuemap[len];
     while (len > 2) {
@@ -1894,7 +1894,7 @@ doencodes(U8 *h, const char *s, I32 len)
        len -= 3;
     }
     if (len > 0) {
-        const char r = (len > 1 ? s[1] : '\0');
+        const U8 r = (len > 1 ? s[1] : '\0');
        *h++ = PL_uuemap[(077 & (s[0] >> 2))];
        *h++ = PL_uuemap[(077 & (((s[0] << 4) & 060) | ((r >> 4) & 017)))];
        *h++ = PL_uuemap[(077 & ((r << 2) & 074))];
@@ -3110,9 +3110,9 @@ S_pack_rec(pTHX_ SV *cat, tempsym_t* symptr, SV **beglist, SV **endlist )
                                   "aptr=%p, aend=%p, buffer=%p, todo=%ld",
                                   aptr, aend, buffer, (long) todo);
                    }
-                   end = doencodes(hunk, buffer, todo);
+                   end = doencodes(hunk, (const U8 *)buffer, todo);
                } else {
-                   end = doencodes(hunk, aptr, todo);
+                   end = doencodes(hunk, (const U8 *)aptr, todo);
                    aptr += todo;
                }
                PUSH_BYTES(utf8, cur, hunk, end-hunk, 0);