From 9ceb9a7228cd6d5796a78e4cced9d5fac358e3a9 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 17 Sep 2014 16:24:13 -0400 Subject: [PATCH] don't write beyond out buffer --- base64.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base64.cpp b/base64.cpp index 62ef7892..4c72c0c6 100644 --- a/base64.cpp +++ b/base64.cpp @@ -166,18 +166,21 @@ namespace data ps = (unsigned char *)InBuffer; if (outCount > len) return -1; - pd = (unsigned char *)OutBuffer; + pd = OutBuffer; + auto endOfOutBuffer = OutBuffer + outCount; for ( i = 0; i < n; i++ ){ acc_1 = iT64[*ps++]; acc_2 = iT64[*ps++]; acc_1 <<= 2; acc_1 |= acc_2>>4; *pd++ = acc_1; + if (pd >= endOfOutBuffer) break; acc_2 <<= 4; acc_1 = iT64[*ps++]; acc_2 |= acc_1 >> 2; *pd++ = acc_2; + if (pd >= endOfOutBuffer) break; acc_2 = iT64[*ps++]; acc_2 |= acc_1 << 6;