Fixes #5457 - StdLib base64 encodeInternal crashes with out of bound exception (#5464)

This commit is contained in:
Florent 2017-03-02 08:54:45 +01:00 • committed by Andreas Rumpf
commit 9965123098

View file

@ -66,7 +66,8 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat
inc(r, 4) inc(r, 4)
inc(i, 3) inc(i, 3)
inc(currLine, 4) 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): for x in items(newLine):
result[r] = x result[r] = x
inc(r) inc(r)
@ -155,12 +156,17 @@ when isMainModule:
assert encode("asure.") == "YXN1cmUu" assert encode("asure.") == "YXN1cmUu"
assert encode("sure.") == "c3VyZS4=" assert encode("sure.") == "c3VyZS4="
const testInputExpandsTo76 = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
const testInputExpands = "++++++++++++++++++++++++++++++"
const longText = """Man is distinguished, not only by his reason, but by this 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, singular passion from other animals, which is a lust of the mind,
that by a perseverance of delight in the continued and indefatigable that by a perseverance of delight in the continued and indefatigable
generation of knowledge, exceeds the short vehemence of any carnal generation of knowledge, exceeds the short vehemence of any carnal
pleasure.""" pleasure."""
const tests = ["", "abc", "xyz", "man", "leasure.", "sure.", "easure.", const tests = ["", "abc", "xyz", "man", "leasure.", "sure.", "easure.",
"asure.", longText] "asure.", longText, testInputExpandsTo76, testInputExpands]
for t in items(tests): for t in items(tests):
assert decode(encode(t)) == t assert decode(encode(t)) == t
assert decode(encode(t, lineLen=40)) == t
assert decode(encode(t, lineLen=76)) == t