Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 21707 - std.base64: Faulty input creates range error instead of Base64Exception #7871

Merged
merged 1 commit into from Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions std/base64.d
Expand Up @@ -1468,6 +1468,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
while (data.length % 4 != 0)
{
range_.popFront();
enforce(!range_.empty, new Base64Exception("Invalid length of encoded data"));
data ~= cast(const(char)[])range_.front;
}
}
Expand Down Expand Up @@ -1652,9 +1653,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
*
* If $(D_PARAM range) is a range of ranges of characters, a `Decoder`
* that iterates over the decoded strings corresponding to each element of
* the range. In this case, the length of each subrange must be a multiple
* of 4; the returned _decoder does not keep track of Base64 decoding
* state across subrange boundaries.
* the range.
*
* In both cases, the returned `Decoder` will be a
* $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) if the
Expand Down Expand Up @@ -2158,3 +2157,15 @@ class Base64Exception : Exception
char[][] input = [['e', 'y'], ['0', '=']];
assert(Base64.decoder(input).array == [[123, 45]]);
}

// https://issues.dlang.org/show_bug.cgi?id=21707
@safe unittest
{
import std.exception : assertThrown;

char[][] t1 = [[ 'Z', 'g', '=' ]];
assertThrown!Base64Exception(Base64.decoder(t1));

char[][] t2 = [[ 'e', 'y', '0' ], ['=', '=']];
assertThrown!Base64Exception(Base64.decoder(t2));
}