Skip to content

Commit c462c65

Browse files
committed
Fix base64 decoding issue with unpack m
1 parent d772ae7 commit c462c65

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bug fixes:
2323
* Fix handling of `break`, `next` and `redo` in `define_method(name, &block)` methods (#2418).
2424
* Fix handling of incompatible types in `Float#<=>` (#2432, @chrisseaton).
2525
* Fix issue with escaping curly braces for `Dir.glob` (#2425).
26+
* Fix `base64` decoding issue with missing output (#2435).
2627

2728
Compatibility:
2829

spec/ruby/library/base64/decode64_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
it "returns a binary encoded string" do
2323
Base64.decode64("SEk=").encoding.should == Encoding::BINARY
2424
end
25+
26+
it "decodes without padding suffix ==" do
27+
Base64.decode64("eyJrZXkiOnsibiI6InR0dCJ9fQ").should == "{\"key\":{\"n\":\"ttt\"}}"
28+
end
2529
end

spec/tags/library/base64/decode64_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/java/org/truffleruby/core/format/read/bytes/ReadBase64StringNode.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ private byte[] read(ByteBuffer encode) {
144144
}
145145

146146
if (a != -1) {
147-
if (c == -1) {
147+
if (c == -1 && s == '=') {
148148
if ((b & 15) != 0) {
149149
throw new InvalidFormatException("invalid base64");
150150
}
151151
lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
152-
} else if (s == '=') {
152+
} else if (c != -1 && s == '=') {
153153
if ((c & 3) != 0) {
154154
throw new InvalidFormatException("invalid base64");
155155
}
@@ -160,7 +160,7 @@ private byte[] read(ByteBuffer encode) {
160160
} else {
161161

162162
while (encode.hasRemaining()) {
163-
b = c = -1;
163+
a = b = c = d = -1;
164164

165165
// obtain a
166166
s = safeGet(encode);
@@ -214,12 +214,13 @@ private byte[] read(ByteBuffer encode) {
214214
lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
215215
lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
216216
lElem[index++] = (byte) ((c << 6 | d) & 255);
217+
a = -1;
217218
}
218219

219220
if (a != -1 && b != -1) {
220-
if (c == -1 && s == '=') {
221+
if (c == -1) {
221222
lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
222-
} else if (c != -1 && s == '=') {
223+
} else if (c != -1) {
223224
lElem[index++] = (byte) ((a << 2 | b >> 4) & 255);
224225
lElem[index++] = (byte) ((b << 4 | c >> 2) & 255);
225226
}

0 commit comments

Comments
 (0)