aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-01-02 15:16:13 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-01-02 15:16:13 +0200
commit496aee483a5ccfa9c396de84dc0cedd234930ddc (patch)
tree49de33d56b29dc9870f1b61f6b40d125d1034692
parent7855a05b15356731a0c2c8d951d2c1ffeb325311 (diff)
Fix undefined behavior (ubsan) bug
-rw-r--r--libbutl/base64.cxx8
1 files changed, 5 insertions, 3 deletions
diff --git a/libbutl/base64.cxx b/libbutl/base64.cxx
index a8f7757..bed8059 100644
--- a/libbutl/base64.cxx
+++ b/libbutl/base64.cxx
@@ -54,14 +54,16 @@ namespace butl
if (n && n % 19 == 0)
*o++ = '\n'; // Split into lines, like the base64 utility does.
- char c (*i++);
+ auto next = [&i] () {return static_cast<unsigned char> (*i++);};
+
+ unsigned char c (next ());
size_t i1 ((c >> 2) & 0x3F);
size_t i2 ((c << 4) & 0x30);
size_t i3 (un);
if (i != e)
{
- c = *i++;
+ c = next ();
i2 |= (c >> 4) & 0xF;
i3 = (c << 2) & 0x3C;
}
@@ -69,7 +71,7 @@ namespace butl
size_t i4 (un);
if (i != e)
{
- c = *i++;
+ c = next ();
i3 |= (c >> 6) & 0x3;
i4 = c & 0x3F;
}