Skip to content

To validate Base16 strings with null and odd length handling - #8786

Open
NithinU2802 wants to merge 2 commits into
open-telemetry:mainfrom
NithinU2802:fix/validate-base16-string
Open

To validate Base16 strings with null and odd length handling#8786
NithinU2802 wants to merge 2 commits into
open-telemetry:mainfrom
NithinU2802:fix/validate-base16-string

Conversation

@NithinU2802

@NithinU2802 NithinU2802 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

This PR fixes an issue in OtelEncodingUtils.isValidBase16String(), which currently accepts strings with odd lengths even though the encoding utilities in this class operate on byte pairs, where each byte must be represented by exactly two hexadecimal characters.

The fix ensures that the method rejects odd length Base16 strings and properly handles null and empty CharSequence inputs.

Regression tests have also been added to cover these cases and verify the expected validation behavior.

@NithinU2802
NithinU2802 requested a review from a team as a code owner September 9, 2026 16:35
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.30%. Comparing base (600cce0) to head (9750607).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8786      +/-   ##
============================================
+ Coverage     91.26%   91.30%   +0.03%     
- Complexity    10503    10508       +5     
============================================
  Files          1007     1007              
  Lines         28373    28375       +2     
  Branches       3586     3586              
============================================
+ Hits          25896    25909      +13     
+ Misses         1685     1673      -12     
- Partials        792      793       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jack-berg

Copy link
Copy Markdown
Member

What problem does this solve? Did you actually encounter an issue that led to this?

See https://github.com/open-telemetry/opentelemetry-java/blob/main/docs/knowledge/api-design.md#null-guards for repo philosophy on null checks given our use of nullaway.

@NithinU2802

Copy link
Copy Markdown
Contributor Author

CharSequence

Yes, the null guard is already present, and I also added a check for an empty CharSequence value. I initially raised this PR with the goal of improving Base16String validation mainly with odd length strings. However, I later noticed that with the padding, the value is handled on the other side. The method isValidBase16String is only checking the hexa characters, I feel empty CharSequence value check is required if not I’m fine with closing this PR.


/** Returns whether the {@link CharSequence} is a valid hex string. */
public static boolean isValidBase16String(CharSequence value) {
if (value == null || value.length() == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Realized this is an internal method so no null check is required. As described here, we only add null guards at public API entry points.

Also, is a CharSequence with length 0 an invalid base16 string? I don't think so, so I think we should close this PR.

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