From 6919d11ecaa3eafe9a9e1621b6e258068a6064e3 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 25 Jul 2020 04:22:42 +0300 Subject: [PATCH] utils: fix hexEncode / hexDecode out-of-bounds index Using generic size type instead of unsigned char --- code/espurna/utils.cpp | 8 ++++---- code/espurna/utils.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/code/espurna/utils.cpp b/code/espurna/utils.cpp index d7a83382..c32f24b6 100644 --- a/code/espurna/utils.cpp +++ b/code/espurna/utils.cpp @@ -796,7 +796,7 @@ size_t hexEncode(uint8_t * in, size_t in_size, char * out, size_t out_size) { if ((2 * in_size + 1) > (out_size)) return 0; static const char base16[] = "0123456789ABCDEF"; - unsigned char index = 0; + size_t index = 0; while (index < in_size) { out[(index*2)] = base16[(in[index] & 0xf0) >> 4]; @@ -811,11 +811,11 @@ size_t hexEncode(uint8_t * in, size_t in_size, char * out, size_t out_size) { // From an hexa char array ("A220EE...") to a byte array (half the size) -size_t hexDecode(const char* in, size_t in_size, uint8_t* out, uint8_t out_size) { +size_t hexDecode(const char* in, size_t in_size, uint8_t* out, size_t out_size) { if (out_size < (in_size / 2)) return 0; - unsigned char index = 0; - unsigned char out_index = 0; + size_t index = 0; + size_t out_index = 0; auto char2byte = [](char ch) -> uint8_t { if ((ch >= '0') && (ch <= '9')) { diff --git a/code/espurna/utils.h b/code/espurna/utils.h index c3d4ffc9..e287b446 100644 --- a/code/espurna/utils.h +++ b/code/espurna/utils.h @@ -70,4 +70,4 @@ void nice_delay(unsigned long ms); double roundTo(double num, unsigned char positions); size_t hexEncode(uint8_t* in, size_t in_size, char* out, size_t out_size); -size_t hexDecode(const char* in, size_t in_size, uint8_t* out, uint8_t out_size); +size_t hexDecode(const char* in, size_t in_size, uint8_t* out, size_t out_size);