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

Conversation

SanderHestvik
Copy link

@SanderHestvik SanderHestvik commented Apr 25, 2024

Hi, after #282, parseBase64Binary throws IllegalArgumentException when called with an empty string

Reading through https://www.w3.org/TR/xmlschema-2 it seems like this is valid input
There is even a test vector BASE64("") = "" in https://www.rfc-editor.org/rfc/rfc4648#section-10
https://github.com/jakartaee/jaxb-api/pull/282/files#diff-2eef69682cb2debd3c0bb837aceb2da7d800a9145e133c709f9e9fb6ef0c3391L707-L710

Let me know if you would like me to create another PR adding some more test coverage

@SanderHestvik SanderHestvik changed the title Handle empty string input for parseBase64Binary Handle empty string input for parseBase64Binary instead of throwing IllegalArgumentException Apr 25, 2024
@antoniosanct
Copy link
Contributor

@SanderHestvik @lukasj
That's correct! Maybe a better solution would be:

public static byte[] _parseBase64Binary(String text) {
  if (null == text || text.length() % 4 != 0) {
    throw new IllegalArgumentException("base64 text invalid.");
  }

Then you could modify your PR changing with this snippet and delete the next if condition.

In addition to, you could add new asserts including RFC references:

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")));

Regards,
Antonio.

@SanderHestvik
Copy link
Author

@lukasj Could you merge this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants