Commit 8430930
Fix %d/%o/%x overflow for large doubles (#605)
This PR fixes an overflow bug when large doubles are used in jsonnet
string formatting:
- `%d`, `%o`, and `%x` format specs used `Double.toLong` to convert to
an integer, which silently clamps to `Long.MAX_VALUE` (~9.2e18) for
large double values like `1e19` or `1e20`
- Add a `truncateToInteger` helper (mirroring the idiom in
[`RenderUtils.renderDouble`](https://github.com/databricks/sjsonnet/blame/9bd52b5ea0564cbb532d72bc345a65f54233c2d3/sjsonnet/src/sjsonnet/Renderer.scala#L256-L264))
that uses `Long` as a fast path and falls back to `BigDecimal.toBigInt`
for values exceeding `Long` range
- Sign and digits both derive from the truncated `BigInt`, so edge cases
like `-0.3` (which truncates to `0`) are handled naturally.
**Test plan**
- Added unit tests for `%d`, `%o`, `%x` with values exceeding Long range
(`1e19`, `1e20`, `-1e19`)
- Added edge cases for fractional negatives (`-0.3`, `-0.9`) that
truncate to zero
- Expected values verified against official C and Go jsonnet
implementations
- Existing `format.jsonnet` test suite covers format strings that
produce outputs with leading negative signs.
---
All code written by Claude Opus 4.6.
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 77af254 commit 8430930
2 files changed
Lines changed: 42 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
240 | 243 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
246 | 253 | | |
247 | 254 | | |
248 | 255 | | |
249 | 256 | | |
250 | 257 | | |
251 | 258 | | |
252 | 259 | | |
253 | | - | |
| 260 | + | |
254 | 261 | | |
255 | 262 | | |
256 | 263 | | |
| |||
275 | 282 | | |
276 | 283 | | |
277 | 284 | | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
283 | 289 | | |
284 | 290 | | |
285 | 291 | | |
286 | 292 | | |
287 | 293 | | |
288 | 294 | | |
289 | 295 | | |
290 | | - | |
| 296 | + | |
291 | 297 | | |
292 | 298 | | |
293 | 299 | | |
294 | 300 | | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
300 | 305 | | |
301 | 306 | | |
302 | 307 | | |
303 | 308 | | |
304 | 309 | | |
305 | 310 | | |
306 | 311 | | |
307 | | - | |
| 312 | + | |
308 | 313 | | |
309 | 314 | | |
310 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
572 | 572 | | |
573 | 573 | | |
574 | 574 | | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
575 | 593 | | |
576 | 594 | | |
577 | 595 | | |
| |||
0 commit comments