[Core] Fix the null char in truncated value returned by switch_b64_decode

This commit is contained in:
windy-wang 2020-09-02 01:04:49 +08:00 committed by GitHub
parent 9c01bd4c78
commit 1d2a1057f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -26,7 +26,7 @@
* Anthony Minessale II <anthm@freeswitch.org>
* Juan Jose Comellas <juanjo@comellas.org>
* Seven Du <dujinfang@gmail.com>
*
* Windy Wang <xiaofengcanyuexp@163.com>
*
* switch_utils.c -- Compatibility and Helper Code
*
@ -1081,7 +1081,7 @@ SWITCH_DECLARE(switch_size_t) switch_b64_decode(const char *in, char *out, switc
while (l >= 8) {
op[ol++] = (char) ((b >> (l -= 8)) % 256);
if (ol >= olen - 2) {
if (ol >= olen - 1) {
goto end;
}
}

View File

@ -23,7 +23,7 @@
*
* Contributor(s):
* Seven Du <seven@signalwire.com>
*
* Windy Wang <xiaofengcanyuexp@163.com>
*
* switch_utils.c -- tests switch_utils
*
@ -63,6 +63,24 @@ FST_TEST_BEGIN(benchmark)
}
FST_TEST_END()
FST_TEST_BEGIN(b64)
{
char *str = "ABC";
unsigned char b64_str[6];
char decoded_str[4];
switch_status_t status = switch_b64_encode((unsigned char *)str, strlen(str), b64_str, sizeof(b64_str));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "b64_str: %s\n", b64_str);
fst_check(status == SWITCH_STATUS_SUCCESS);
fst_check_string_equals((const char *)b64_str, "QUJD");
switch_size_t size = switch_b64_decode((const char *)b64_str, decoded_str, sizeof(decoded_str));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "decoded_str: %s\n", decoded_str);
fst_check_string_equals(decoded_str, str);
fst_check(size == 4);
}
FST_TEST_END()
FST_SUITE_END()
FST_MINCORE_END()