Skip to content

base64.decode on whitespace prefixed string can lead to index defect #25530

@miki725

Description

@miki725

Nim Version

2.2.6

Description

(example using fish shell)

➜ nim c -r (echo '
  import base64
  echo decode("   a")
  ' | psub -s '.nim')
/tmp/.psub.PVFMMD/psub.nim(3) psub
/home/root/.choosenim/toolchains/nim-2.2.6/lib/pure/base64.nim(230) decode
/home/root/.choosenim/toolchains/nim-2.2.6/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: index 4 not in 0 .. 3 [IndexDefect]

if a string to be decoded is prefixed with whitespace due to:

Nim/lib/pure/base64.nim

Lines 252 to 259 in ab00c56

inputEnds = inputLen - 4
while inputIndex <= inputEnds:
while s[inputIndex] in {'\n', '\r', ' '}:
inc inputIndex
inputChar(a)
inputChar(b)
inputChar(c)
inputChar(d)

as it increments inputIndex it can increment it outside of the outer loop bound inputLen - 4 hence inputChar blows up with out of bounds error

Current Output

`IndexDefect` is raised

Expected Output

base64 should decode as normal

Known Workarounds

call strip before using decode:

decode(s.strip())

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions