-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Validate hexadecimal byte input #1273
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,9 +156,17 @@ public static byte[] hexStringToByteArray(final String s) { | |
| return null; | ||
| } | ||
| final int len = s.length(); | ||
| if ((len & 1) != 0) { | ||
| throw new IllegalArgumentException("Hex string must contain an even number of characters"); | ||
| } | ||
| final byte[] bytes = new byte[len / 2]; | ||
| for (int i = 0; i < len; i += 2) { | ||
| bytes[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); | ||
| final int high = Character.digit(s.charAt(i), 16); | ||
| final int low = Character.digit(s.charAt(i + 1), 16); | ||
| if (high < 0 || low < 0) { | ||
| throw new IllegalArgumentException("Hex string contains a non-hexadecimal character"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here — |
||
| } | ||
| bytes[i / 2] = (byte) ((high << 4) + low); | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the project convention for argument checks is
Requires.requireTrue(cond, msg)fromcom.alipay.sofa.jraft.util.Requires(this class already usesRequires.requireNonNullinnextBytes). So this could be: