Repository navigation
fix(sheet): tolerate a null rounding mode in the decimal format cache - #1270
Sean-Walker0 wants to merge 2 commits into
Conversation
89fa41f to
0683ed6
Compare
skytin1004
left a comment
There was a problem hiding this comment.
Hi @Sean-Walker0, thanks for working on this fix. I think there may still be one issue.
Because the same formatter is reused, a previous call with RoundingMode.UP can affect a later call with a null rounding mode. For example, 1.001 is formatted as 1.01 instead of the expected default result, 1.00.
Could you reset the rounding mode to the default when it is null? Please also add a test that first uses RoundingMode.UP and then uses a null rounding mode. Thank you!
0683ed6 to
b1ad771
Compare
|
Good catch — the cached format is shared per pattern, so a mode set by an earlier call did leak into later null-mode calls. Fixed and pushed as b1ad771: a null rounding mode now restores The requested regression test is in as |
Mikkey-f
left a comment
There was a problem hiding this comment.
Thanks @Sean-Walker0 — verified the cache-leak fix on Windows/JDK 17.
Verification
- PR head:
NumberUtilsTest36/36 green. - At 292c583 (first commit: null check but no reset):
test_format_nullRoundingModeResetsPreviousModeFromCachefails withexpected: <1.00> but was: <1.01>— the second commit is necessary. - At current main (no change): both new tests fail with
NullPointerExceptioningetCacheDecimalFormat.
The reset to RoundingMode.HALF_EVEN matches DecimalFormat's documented default, and the comparison avoids setting the mode when it is already correct. LGTM from my side.
b1ad771 to
42e1189
Compare
|
Thanks @Mikkey-f for the independent verification on Windows — glad the regression test pins exactly the leak skytin1004 described. |
76536da to
6223cca
Compare
NumberUtils.getCacheDecimalFormat compared the cached format's rounding mode against the requested one and called setRoundingMode whenever they differed. A NumberFormatProperty built without a rounding mode therefore always differed (the DecimalFormat default is HALF_EVEN), and DecimalFormat.setRoundingMode(null) answered with a NullPointerException - so NumberUtils.format and NumberUtils.parse blew up on any content property whose number format carries no explicit mode, while the annotation default (HALF_UP) worked. Skip the assignment when the requested mode is null, keeping the DecimalFormat default, exactly like the empty-format fallbacks the annotation path already handles. Verified with NumberUtilsTest.test_format_nullRoundingModeDoesNotThrow (NPE before the change, green after) plus the full fesod-sheet suite (1777 tests) and spotless:check.
The decimal format cache shares one DecimalFormat per pattern, so an explicit mode from an earlier call survived into later calls that request no mode: formatting 1.001 with RoundingMode.UP and then with a null mode yielded 1.01 instead of the HALF_EVEN default 1.00. A null rounding mode now restores RoundingMode.HALF_EVEN (the documented DecimalFormat default) before the format is returned. Verified with NumberUtilsTest.test_format_nullRoundingModeResetsPreviousModeFromCache (1.01 before the change, 1.00 after) plus the full fesod-sheet suite (1791 tests) and spotless:check.
6223cca to
0f3654a
Compare
Purpose of the pull request
NumberUtils.getCacheDecimalFormatcalledDecimalFormat.setRoundingMode(roundingMode)whenever the cached format's mode differed from the requested one.NumberFormatPropertyis a public class whose constructor accepts a nullRoundingMode, and theDecimalFormatdefault isHALF_EVEN— so a property built without an explicit mode always differed,setRoundingMode(null)threwNullPointerException, and bothNumberUtils.format(...)andNumberUtils.parse(...)failed:The annotation path was unaffected because
@NumberFormat.roundingMode()defaults toHALF_UP; programmaticNumberFormatPropertyuse (public constructors, public utility methods) hit the NPE.No issue exists for this yet; found while probing the number conversion utilities.
What's changed?
getCacheDecimalFormatskips the assignment when the requested mode is null, keeping theDecimalFormatdefault — the same shape as the existing empty-format fallbacks the annotation path already handles.Checklist
Verifying this change
NumberUtilsTest.test_format_nullRoundingModeDoesNotThrow— red before (NullPointerException), green after, and pins the HALF_EVEN rendering (1.005with0.00->1.00).NumberUtilsTestcases (explicit modes, cache locale tracking, range rejection) pass unchanged../mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 1777 tests, 0 failures, 0 errors../mvnw -pl fesod-sheet spotless:check: clean.Notes
NumberUtilsorNumberFormatProperty; the recently merged fix: reject partial formatted number parses #1162/ fix: reject out-of-range integral cell values #1101 changed different regions of the same file.