ChristianRiesen / base32

Base32 Encoder/Decoder according to RFC 4648
MIT License
132 stars 29 forks source link

Error decoding an entirely non-base32 string #4

Closed enkrates closed 9 years ago

enkrates commented 9 years ago

In the decode() method, the first step is to check for an empty string:

if (strlen($base32String) == 0) {
    // Gives an empty string
    return '';
}

However, the second step is to strip out any non-base32 characters. If the string to decode is entirely non-base32 characters (granted, a strange thing to pass into a base32 decode method), the string will be left as an empty string. The empty string would then be split into an array here:

$base32Array = str_split($base32String); // $base32String is empty at this point

which sets $base32Array to be an array with a single item, an empty string (see here for the description of this "bug"). This causes the subsequent foreach loop to iterate once, on an empty string. That causes an error in the first line of the foreach loop:

foreach ($base32Array as $str) {
    $char = self::$decode[$str];

as $str here is an empty string and there is no such key in self::$decode.

It seems as though the approach in this library is to silently remove non-base32 characters (unlike the Python standard library, which throws a TypeError if a non Base32 string is passed to decode), so I will submit a Pull Request that should silently handle this problem.

ChristianRiesen commented 9 years ago

Fixed with merged PR