]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] Fix the null char in truncated value returned by switch_b64_decode
authorwindy-wang <xiaofengcanyuexp@163.com>
Tue, 1 Sep 2020 17:04:49 +0000 (01:04 +0800)
committerGitHub <noreply@github.com>
Tue, 1 Sep 2020 17:04:49 +0000 (21:04 +0400)
src/switch_utils.c
tests/unit/switch_utils.c

index e6e4917b1eedb51592934d85d1a4368c16a020ae..e4e26a6eef95337cccd6151453826c470b57aecd 100644 (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;
                        }
                }
index 72c5817b71c5a8342f95803b185a04222a62d4b0..7b7fa3729a51c0417e34eb144866318bf8038a10 100644 (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()