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

Handle empty string input for parseBase64Binary instead of throwing IllegalArgumentException #309

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/main/java/jakarta/xml/bind/DatatypeConverterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,10 @@ private static int guessLength(String text) {
* because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast)
*/
public static byte[] _parseBase64Binary(String text) {
final int buflen = guessLength(text);
if (buflen < 3) {
if (null == text || text.length() % 4 != 0) {
throw new IllegalArgumentException("base64 text invalid.");
}
final int buflen = guessLength(text);
final byte[] out = new byte[buflen];
int o = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public void testPrint() {
@Test
public void testBase64() {
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("Qxx=="));

Assert.assertEquals("", new String(DatatypeConverter.parseBase64Binary("")));
Assert.assertEquals("f", new String(DatatypeConverter.parseBase64Binary("Zg==")));
Assert.assertEquals("fo", new String(DatatypeConverter.parseBase64Binary("Zm8=")));
Assert.assertEquals("foo", new String(DatatypeConverter.parseBase64Binary("Zm9v")));
Assert.assertEquals("foob", new String(DatatypeConverter.parseBase64Binary("Zm9vYg==")));
Assert.assertEquals("fooba", new String(DatatypeConverter.parseBase64Binary("Zm9vYmE=")));
Assert.assertEquals("foobar", new String(DatatypeConverter.parseBase64Binary("Zm9vYmFy")));

Assert.assertNotEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQ==")));

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