Skip to content

Commit 3cb177a

Browse files
SanderHestvikSander Hestvikantoniosanct
authored
Handle empty string input for parseBase64Binary instead of throwing IllegalArgumentException (#309)
* Handle empty string input for parseBase64Binary * Fix input validation in _parseBase64Binary and add test cases from https://www.rfc-editor.org/rfc/rfc4648#section-10 Co-authored-by: Antonio Santos Izaguirre <[email protected]> --------- Co-authored-by: Sander Hestvik <[email protected]> Co-authored-by: Antonio Santos Izaguirre <[email protected]>
1 parent 748c50b commit 3cb177a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

api/src/main/java/jakarta/xml/bind/DatatypeConverterImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,10 @@ private static int guessLength(String text) {
711711
* because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast)
712712
*/
713713
public static byte[] _parseBase64Binary(String text) {
714-
final int buflen = guessLength(text);
715-
if (buflen < 3) {
714+
if (null == text || text.length() % 4 != 0) {
716715
throw new IllegalArgumentException("base64 text invalid.");
717716
}
717+
final int buflen = guessLength(text);
718718
final byte[] out = new byte[buflen];
719719
int o = 0;
720720

api/src/test/java/org/eclipse/jaxb/api/DatatypeConverterTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public void testPrint() {
8484
@Test
8585
public void testBase64() {
8686
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("Qxx=="));
87+
88+
Assert.assertEquals("", new String(DatatypeConverter.parseBase64Binary("")));
89+
Assert.assertEquals("f", new String(DatatypeConverter.parseBase64Binary("Zg==")));
90+
Assert.assertEquals("fo", new String(DatatypeConverter.parseBase64Binary("Zm8=")));
91+
Assert.assertEquals("foo", new String(DatatypeConverter.parseBase64Binary("Zm9v")));
92+
Assert.assertEquals("foob", new String(DatatypeConverter.parseBase64Binary("Zm9vYg==")));
93+
Assert.assertEquals("fooba", new String(DatatypeConverter.parseBase64Binary("Zm9vYmE=")));
94+
Assert.assertEquals("foobar", new String(DatatypeConverter.parseBase64Binary("Zm9vYmFy")));
95+
8796
Assert.assertNotEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQ==")));
8897

8998
Assert.assertEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIHdvcmxkIQ==")));

0 commit comments

Comments
 (0)