[CP] [parser][asan]: avoid heap-buffer-overflow while parser hex with odd number(e.g., 0xaaa);

This commit is contained in:
Monk-Liu
2023-02-06 13:20:00 +08:00
committed by ob-robot
parent a09570cef7
commit 73b10be4ad

View File

@ -505,17 +505,24 @@ void ob_parse_binary(const char *src, int64_t len, char *dest)
if (OB_UNLIKELY(NULL == src || len <= 0 || NULL == dest)) { if (OB_UNLIKELY(NULL == src || len <= 0 || NULL == dest)) {
//do nothing //do nothing
} else { } else {
bool is_odd = false;
if (len > 0 && len % 2 != 0) if (len > 0 && len % 2 != 0)
{ {
*dest = char_int(src[0]); *dest = char_int(src[0]);
++src; ++src;
++dest; ++dest;
is_odd = true;
} }
const char *end = src + len -1; if (len == 1) {
for (; src <= end; src += 2) //do nothing.
{ } else {
*dest = (char)(16*char_int(src[0]) + char_int(src[1])); //for odd number, we have copy the first char, so we should minus 2;
++dest; const char *end = src + len - (is_odd ? 2 : 1);
for (; src <= end; src += 2)
{
*dest = (char)(16*char_int(src[0]) + char_int(src[1]));
++dest;
}
} }
} }
} }