Skip to content

Commit

Permalink
Fix a test failure in base62 and an out-of-bounds buffer access in th…
Browse files Browse the repository at this point in the history
…e test

Signed-off-by: Haru <[email protected]>
  • Loading branch information
MishimaHaruna committed Oct 16, 2023
1 parent 3ac3c97 commit 5266c62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/common/base62.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "common/nullpo.h"
#include "common/utils.h"

#include <string.h>

static struct base62_interface base62_s;
struct base62_interface *base62;

Expand Down Expand Up @@ -57,12 +59,13 @@ static char base62tbl[62] = {
static bool base62_encode_int_padded(int value, char *buf, int min_len, int buf_len)
{
nullpo_retr(false, buf);

// if caller ignores an error, at least it gets a NULL-terminated string
memset(buf, '\0', buf_len);

Assert_retr(false, min_len < buf_len);
Assert_retr(false, buf_len >= 2);

// if caller ignores an error, at least it gets a NULL-terminated string
buf[0] = '\0';

char temp_buf[BASE62_INT_BUFFER_LEN] = { 0 };
int max_idx = cap_value(buf_len - 2, 0, BASE62_INT_BUFFER_LEN - 2);

Expand All @@ -76,7 +79,7 @@ static bool base62_encode_int_padded(int value, char *buf, int min_len, int buf_
Assert_retr(false, (value == 0));

int final_buf_idx = 0;

if (idx < min_len) {
int pad_size = min_len - idx;
for (int i = 0; i < pad_size; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/test_base62.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
} while (false)

#define expect_int(message, actual, expected, ...) \
expect("%d", (actual == expected), message, actual, expected, ##__VA_ARGS__)
expect("%d", ((actual) == (expected)), message, (actual), (expected), ##__VA_ARGS__)
#define expect_str(message, actual, expected, ...) \
expect("%s", (strcmp(actual, expected) == 0), message, actual, expected, ##__VA_ARGS__)
expect("%s", (strcmp((actual), (expected)) == 0), message, (actual), (expected), ##__VA_ARGS__)

static bool test_base62_encode_int_padded(void)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ static bool test_base62_encode_int_padded(void)
// This will show an assert error to alert server owners that the used buffer is too small
bool res = base62->encode_int_padded(INT_MAX - 1, output, 5, sizeof(output));
expect_int("To fail the encoding", res, false);
expect_int("To have a NULL-terminated buffer", output[6], '\0');
expect_int("To have a NULL-terminated buffer", output[5], '\0');
}

return passed;
Expand Down

0 comments on commit 5266c62

Please sign in to comment.