Skip to content

Commit

Permalink
Fix base38 decoding for long payloads. (#21037)
Browse files Browse the repository at this point in the history
We were casting indices into the string to uint8_t, so would fail for
inputs that were longer than 255 chars.
  • Loading branch information
bzbarsky-apple authored Jul 21, 2022
1 parent 6d54807 commit 94f8974
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/setup_payload/Base38Decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ CHIP_ERROR base38Decode(std::string base38, std::vector<uint8_t> & result)
{
result.clear();

int base38CharactersNumber = static_cast<int>(base38.length());
int decodedBase38Characters = 0;
size_t base38CharactersNumber = base38.length();
size_t decodedBase38Characters = 0;
while (base38CharactersNumber > 0)
{
uint8_t base38CharactersInChunk;
Expand Down Expand Up @@ -128,10 +128,10 @@ CHIP_ERROR base38Decode(std::string base38, std::vector<uint8_t> & result)

uint32_t value = 0;

for (int i = (base38CharactersInChunk - 1); i >= 0; i--)
for (size_t i = base38CharactersInChunk; i > 0; i--)
{
uint8_t v = 0;
CHIP_ERROR err = decodeChar(base38[static_cast<uint8_t>(decodedBase38Characters + i)], v);
CHIP_ERROR err = decodeChar(base38[decodedBase38Characters + i - 1], v);

if (err != CHIP_NO_ERROR)
{
Expand All @@ -143,7 +143,7 @@ CHIP_ERROR base38Decode(std::string base38, std::vector<uint8_t> & result)
decodedBase38Characters += base38CharactersInChunk;
base38CharactersNumber -= base38CharactersInChunk;

for (int i = 0; i < bytesInDecodedChunk; i++)
for (size_t i = 0; i < bytesInDecodedChunk; i++)
{
result.push_back(static_cast<uint8_t>(value));
value >>= 8;
Expand Down

0 comments on commit 94f8974

Please sign in to comment.