Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move p.isNaN() check inside try block, post #4195 #4234

Closed
wants to merge 1 commit into from

Conversation

JooHyukKim
Copy link
Member

@JooHyukKim JooHyukKim commented Nov 29, 2023

part of #4194

Motivation

This PR addresses possible p.isNan() failure by adding a fix proposed by https://github.com/FasterXML/jackson-databind/pull/4195/files#r1408831213

Changes

  • Moved the p.isNaN() check inside try block
  • Added more comment about why it is there

if (p.isNaN() && ctxt.isEnabled(JsonNodeFeature.FAIL_ON_NAN_TO_BIG_DECIMAL_COERCION)) {
ctxt.handleWeirdNumberValue(handledType(), p.getDoubleValue(),
"Cannot convert NaN into BigDecimal");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not what I meant - p.isNan is unreliable and should not be used

this code code can be changed to remove this and put this in the catch

if (ctxt.isEnabled(JsonNodeFeature.FAIL_ON_NAN_TO_BIG_DECIMAL_COERCION)) {
                    ctxt.handleWeirdNumberValue(handledType(), p.getDoubleValue(),
                            "Cannot convert NaN into BigDecimal");
          return; //??
} else {
   // fall through
}

If you give me a week or 2, I can get back to this and try to fix it myself.

Copy link
Member

@cowtowncoder cowtowncoder Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pjfanning First thing would be to show how JsonNode serialization would be compromised by this change. I do not know why this would be, based on my reading of code.

Is there a case where BigDecimal value would now be incorrect?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JooHyukKim As usual, it'd be necessary to have a failing case first before trying to solve a problem. Otherwise we have no verification of the fix, or check against regression.

And specifically, as far as I can see, p.isNaN() never throws an exception; at least for JSON parser. So enclosing that in try-catch block would serve no purpose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmh. Ok, I think I know what is likely problematic... Double value overflow becoming +INFINITY, in case of JsonNodeFeature.FAIL_ON_NAN_TO_BIG_DECIMAL_COERCION being true.
So this would be actual bug for this handling.

But we do need jackson-core test reproduction. Maybe:

FasterXML/jackson-core#1135

does that, I'll have another look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. So one issue relevant here is this:

  1. JsonParser.isNaN() immediately after parser.nextToken() is accurate; only considering non-standard values (if allowed)
  2. JsonParser.getNumberType() coerces all fp values into NumberType.DOUBLE and thus Double value overflow can induce NaNs
  3. JsonNodeDeserializer should not call getNumberType() for JSON -- but! It should call it for binary formats that DO provide this information

so it's combo of (2) and (3) that are problematic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we need a reproduction. I will close this, because I think I missed lots of information here thus need to catch up on what's missing.

My mistake 🙏🏼 Thank you for explaining things @pjfanning @cowtowncoder !

Copy link
Member

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a good change - it still uses the unreliable p.isNaN call

@JooHyukKim JooHyukKim closed this Nov 30, 2023
@JooHyukKim JooHyukKim deleted the Fix-post-4195 branch January 20, 2024 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants