Skip to content

base64_enc_len should return a bigger size #22

Open
@petrbrouzda

Description

@petrbrouzda

There is a commentary in sample:

// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);

There is no consumption of "input" in base64_encode, is it?
But the input gets consumed.
So problem lies somewhere else.

base64_enc_len( "Hello world" ) returns 16.
And base64 encode result is "SGVsbG8gd29ybGQ=", which is 16 characters, OK?
But this doesn't include terminating \0.
So memory gets overwritten.

  • input* is placed to address 3FFEFBE0
  • encoded* is placed to address 3FFEFBD0
  • so nul-terminator of "encoded" overwrites the first byte of input
  • so input seems to be consumed.

Fix is easy:

int base64_enc_len(int plainLen) {
return ((plainLen + 2 - ((plainLen + 2) % 3)) / 3 * 4)+1;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions