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

[#4209] chore replace hardcoded value with std definition #6354

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -353,11 +352,8 @@ DER get(int... tags) {
}

String getAsString() {
try {
return new String(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalCharsetNameException("UTF-8"); // won't happen.
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this code?

The String constructor used here does not declare that it throws an UnsupportedEncodingException because StandardCharsets.UTF_8 guarantees that the encoding is supported. However, an IndexOutOfBoundsException could occur in this context.

Copy link
Member

@justinmclean justinmclean Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems Ok, but I assume it was there for some reason for it originally, so it may not be best to remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems Ok, but I assume it was there for some reason for it originally, so it may not be best to remove it.

we can't do that, if we use:

try {
       return new String(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), StandardCharsets.UTF_8);
     } catch (UnsupportedEncodingException e) {
       throw new IllegalCharsetNameException("UTF-8"); // won't happen.
     }

the complier will report Exception java.io.UnsupportedEncodingException is never thrown in the corresponding try block like this:

Screenshot 2025-01-23 at 10 53 59

maybe we can use Exception replace UnsupportedEncodingException?

return new String(
bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), StandardCharsets.UTF_8);
}

@Override
Expand Down
Loading