You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "nikolauspschuetz (via GitHub)" <gi...@apache.org> on 2026/07/24 05:23:52 UTC

[PR] MINOR: Fix formatBytes to return "0 B" for zero bytes [kafka]

nikolauspschuetz opened a new pull request, #22921:
URL: https://github.com/apache/kafka/pull/22921

   `Utils.formatBytes(0)` returns `"0.0"` instead of `"0 B"`.
   
   For `bytes == 0`, `Math.log(0)` is `-Infinity`, so the scale `ordinal` underflows to `Integer.MIN_VALUE`, the `BYTE_SCALE_SUFFIXES[ordinal]` lookup throws `ArrayIndexOutOfBoundsException`, and the `catch` falls back to `String.valueOf(0.0)`. Zero is neither negative nor "too large", so it is an unintended path. `formatBytes` is used by `SimpleMemoryPool`/`GarbageCollectedMemoryPool` `toString()`, so an empty pool renders `0.0` in operator-facing output.
   
   Structured red→green, verified in a JDK17 container (`./gradlew clients:test --tests UtilsTest.testFormatBytes`):
   - **1st commit** adds the regression test — **FAILED**: `expected: <0 B> but was: <0.0>`
   - **2nd commit** applies the fix — **PASSED**
   
   Trivial one-value correction, no public API change → `MINOR` (no JIRA/KIP).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Fix formatBytes to return "0 B" for zero bytes [kafka]

Posted by "nikolauspschuetz (via GitHub)" <gi...@apache.org>.
nikolauspschuetz commented on PR #22921:
URL: https://github.com/apache/kafka/pull/22921#issuecomment-5084648730

   Thanks Sean. Tightened the description to work as the squash commit message and trimmed the over-explanation. The regression test (`UtilsTest.testFormatBytes`) fails without the fix and passes with it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Fix formatBytes to return "0 B" for zero bytes [kafka]

Posted by "squah-confluent (via GitHub)" <gi...@apache.org>.
squah-confluent merged PR #22921:
URL: https://github.com/apache/kafka/pull/22921


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org