diff options
author | Florent <florent@napalu.ch> | 2017-03-02 08:54:45 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-03-02 08:54:45 +0100 |
commit | 99651230985b61a4471c3b0df168ec10a1524222 (patch) | |
tree | d1170aabdc1aa7b2df40461677f7001075cec392 /lib | |
parent | 37abcf18f88f9f0bfd7d1b721359149d84cb67d6 (diff) | |
download | Nim-99651230985b61a4471c3b0df168ec10a1524222.tar.gz |
Fixes #5457 - StdLib base64 encodeInternal crashes with out of bound exception (#5464)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/base64.nim | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim index 2fc0b1c5e..eee03d7ae 100644 --- a/lib/pure/base64.nim +++ b/lib/pure/base64.nim @@ -49,7 +49,7 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat ## `newline` is added. var total = ((len(s) + 2) div 3) * 4 var numLines = (total + lineLen - 1) div lineLen - if numLines > 0: inc(total, (numLines-1) * newLine.len) + if numLines > 0: inc(total, (numLines - 1) * newLine.len) result = newString(total) var i = 0 @@ -66,7 +66,8 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat inc(r, 4) inc(i, 3) inc(currLine, 4) - if currLine >= lineLen and i != s.len-2: + # avoid index out of bounds when lineLen == encoded length + if currLine >= lineLen and i != s.len-2 and r < total: for x in items(newLine): result[r] = x inc(r) @@ -155,12 +156,17 @@ when isMainModule: assert encode("asure.") == "YXN1cmUu" assert encode("sure.") == "c3VyZS4=" + const testInputExpandsTo76 = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + const testInputExpands = "++++++++++++++++++++++++++++++" const longText = """Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.""" const tests = ["", "abc", "xyz", "man", "leasure.", "sure.", "easure.", - "asure.", longText] + "asure.", longText, testInputExpandsTo76, testInputExpands] + for t in items(tests): assert decode(encode(t)) == t + assert decode(encode(t, lineLen=40)) == t + assert decode(encode(t, lineLen=76)) == t \ No newline at end of file |