fix: Validate value before writing deferred name in JsonWriter#2999
Open
daguimu wants to merge 1 commit intogoogle:mainfrom
Open
fix: Validate value before writing deferred name in JsonWriter#2999daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu wants to merge 1 commit intogoogle:mainfrom
Conversation
JsonWriter.value(float), value(double), and value(Number) called writeDeferredName() before validating whether the value is finite. This meant that if a NaN or Infinity value was rejected with an exception, the property name had already been written to the output, leaving the writer in an inconsistent state. Move the validation before writeDeferredName() so that invalid values are rejected without any side effects on the output. Fixes google#1736
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
JsonWriter.value(float),value(double), orvalue(Number)is called with a non-finite value (NaN, Infinity) in non-lenient mode, the deferred property name is written to the output before the value is validated. The validation then throws anIllegalArgumentException, but the name has already been emitted, leaving the writer in an inconsistent state.This is particularly problematic when
serializeNullsis set tofalse: a subsequentnullValue()call intended to suppress the property will find the name already written.Root Cause
In all three methods,
writeDeferredName()was called before the NaN/Infinity validation check. The deferred name is a side-effecting write to the output stream that cannot be undone after the exception.Fix
Move the validation checks before
writeDeferredName()in all three methods:value(float): moveFloat.isNaN/isInfinitecheck beforewriteDeferredName()value(double): moveDouble.isNaN/isInfinitecheck beforewriteDeferredName()value(Number): move the entire validation block (NaN/Infinity check and number format validation) beforewriteDeferredName()Tests Added
testNonFiniteValueDoesNotWriteDeferredName— verifies that whenserializeNulls=false:value(Float.NaN)throws without writing the name, allowingnullValue()to suppress itvalue(Double.NaN)throws without writing the namevalue(Double.POSITIVE_INFINITY)throws without writing the namevalue(Number)withDouble.NaNthrows without writing the namevalue(Number)with invalid number string throws without writing the name{}(empty object, all names suppressed)Impact
Only affects the error path for non-finite values in non-lenient mode. Normal finite value writing is unchanged. All 4587 existing tests pass.
Fixes #1736