Skip to content

Commit

Permalink
utils: fix hexEncode / hexDecode out-of-bounds index
Browse files Browse the repository at this point in the history
Using generic size type instead of unsigned char
  • Loading branch information
mcspr committed Jul 25, 2020
1 parent 5e5d0d0 commit 6919d11
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions code/espurna/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 6919d11

Please sign in to comment.