From: Dmitry Volyntsev Date: Fri, 23 Feb 2024 01:38:58 +0000 (-0800) Subject: Fixed atob() with non-padded base64 strings. X-Git-Tag: 0.8.4~26 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=822e4501c1ca3804a729fe9d0288513bfd5aed6d;p=njs.git Fixed atob() with non-padded base64 strings. This fixes #695 issue on Github. --- diff --git a/src/njs_string.c b/src/njs_string.c index 3a944ecd..f22ab43a 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -4298,7 +4298,14 @@ njs_string_atob(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } } - len = njs_base64_decoded_length(str.length, pad); + len = str.length; + + if (len % 4 != 0) { + pad = 4 - (len % 4); + len += pad; + } + + len = njs_base64_decoded_length(len, pad); njs_chb_init(&chain, vm->mem_pool); diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 4e54c712..63615770 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -10093,6 +10093,18 @@ static njs_unit_test_t njs_test[] = "].every(v => c(atob(v)).toString() == '8,52,86')"), njs_str("true")}, + { njs_str("atob('aGVsbG8=')"), + njs_str("hello") }, + + { njs_str("atob('aGVsbG8')"), + njs_str("hello") }, + + { njs_str("atob('TQ==')"), + njs_str("M") }, + + { njs_str("atob('TQ')"), + njs_str("M") }, + /* Functions. */ { njs_str("return"),