Skip to content

Commit

Permalink
Merge pull request #12392 from aabadie/pr/sys/base64_fix_empty
Browse files Browse the repository at this point in the history
sys/base64: fix handling of empty buffers
  • Loading branch information
aabadie authored Oct 8, 2019
2 parents d749e2d + 4c8b628 commit c20199c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 8 additions & 2 deletions sys/base64/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ int base64_encode(const void *data_in, size_t data_in_size,
return BASE64_ERROR_DATA_IN;
}

if (data_in_size < 1) {
return BASE64_ERROR_DATA_IN_SIZE;
if (data_in_size == 0) {
*base64_out_size = 0;
return BASE64_SUCCESS;
}

if (*base64_out_size < required_size) {
Expand Down Expand Up @@ -170,6 +171,11 @@ int base64_decode(const unsigned char *base64_in, size_t base64_in_size,
return BASE64_ERROR_DATA_IN;
}

if (base64_in_size == 0) {
*data_out_size = 0;
return BASE64_SUCCESS;
}

if (base64_in_size < 4) {
return BASE64_ERROR_DATA_IN_SIZE;
}
Expand Down
5 changes: 2 additions & 3 deletions sys/include/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static inline size_t base64_estimate_encode_size(size_t data_in_size)
BASE64_ERROR_BUFFER_OUT_SIZE on insufficient size for encoding to `base64_out`,
BASE64_ERROR_BUFFER_OUT if `base64_out` equals NULL
but the `base64_out_size` is sufficient,
BASE64_ERROR_DATA_IN if `data_in` equals NULL,
BASE64_ERROR_DATA_IN_SIZE if `data_in_size` is less than 1.
BASE64_ERROR_DATA_IN if `data_in` equals NULL.
*/
int base64_encode(const void *data_in, size_t data_in_size,
unsigned char *base64_out, size_t *base64_out_size);
Expand All @@ -93,7 +92,7 @@ int base64_encode(const void *data_in, size_t data_in_size,
BASE64_ERROR_BUFFER_OUT if `data_out` equals NULL
but the size for `data_out_size` is sufficient,
BASE64_ERROR_DATA_IN if `base64_in` equals NULL,
BASE64_ERROR_DATA_IN_SIZE if `base64_in_size` is less than 4.
BASE64_ERROR_DATA_IN_SIZE if `base64_in_size` is between 1 and 4.
*/
int base64_decode(const unsigned char *base64_in, size_t base64_in_size,
void *data_out, size_t *data_out_size);
Expand Down
29 changes: 29 additions & 0 deletions tests/unittests/tests-base64/tests-base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,33 @@ static void test_base64_09_encode_size_determination(void)
TEST_ASSERT_EQUAL_INT(required_out_size, expected_out_size);
}

static void test_base64_10_encode_empty(void)
{
unsigned char data_in[] = "";

size_t base64_out_size = 8;
unsigned char base64_out[8];

int ret = base64_encode(data_in, 0, base64_out, &base64_out_size);

TEST_ASSERT_EQUAL_INT(BASE64_SUCCESS, ret);
TEST_ASSERT_EQUAL_INT(0, base64_out_size);
}

static void test_base64_10_decode_empty(void)
{
unsigned char data_in[] = "";

size_t base64_out_size = 8;
unsigned char base64_out[8];

int ret = base64_decode(data_in, 0, base64_out, &base64_out_size);

TEST_ASSERT_EQUAL_INT(BASE64_SUCCESS, ret);
TEST_ASSERT_EQUAL_INT(0, base64_out_size);
}


Test *tests_base64_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand All @@ -395,6 +422,8 @@ Test *tests_base64_tests(void)
new_TestFixture(test_base64_07_stream_decode),
new_TestFixture(test_base64_08_encode_16_bytes),
new_TestFixture(test_base64_09_encode_size_determination),
new_TestFixture(test_base64_10_encode_empty),
new_TestFixture(test_base64_10_decode_empty),
};

EMB_UNIT_TESTCALLER(base64_tests, NULL, NULL, fixtures);
Expand Down

0 comments on commit c20199c

Please sign in to comment.