LuciferYang opened a new pull request, #56655:
URL: https://github.com/apache/spark/pull/56655

   ### What changes were proposed in this pull request?
   
   `ByteUnit.toBytes` checked for a negative argument and then returned `d * 
multiplier` directly, with no guard against the multiplication overflowing a 
`long`. For the larger units this can wrap silently: `PiB.toBytes(8192)` 
returns `Long.MIN_VALUE` instead of reporting an out-of-range value. The 
sibling `convertTo` already guards the same multiplication with `Long.MAX_VALUE 
/ ratio < d`, so this PR adds the equivalent check to `toBytes`: an 
out-of-range conversion now throws `IllegalArgumentException` rather than 
returning a wrapped result.
   
   It also adds `ByteUnitSuite`, which the module did not have, covering the 
per-unit conversions (including zero), negative input, the overflow case, and 
the largest value that still fits in a `long`.
   
   ### Why are the changes needed?
   
   A size conversion that silently wraps to a negative number is worse than one 
that fails loudly, and `toBytes` was the only conversion method on this enum 
without the overflow guard that `convertTo` already has. Making the two 
consistent removes a latent correctness gap on a public utility.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Values that fit in a `long` convert exactly as before; only an input 
large enough to overflow now throws `IllegalArgumentException` instead of 
returning a wrapped value.
   
   ### How was this patch tested?
   
   Added `ByteUnitSuite`. `build/sbt 'common-utils-java/testOnly 
*ByteUnitSuite'` passes.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 4.8)
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to