-
-
Notifications
You must be signed in to change notification settings - Fork 815
Fixes Implicit narrowing conversion in compound assignment FloatToDecimal #1450
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
base: 2.x
Are you sure you want to change the base?
Conversation
@odaysec can you provide a test case that demos that this is a fix? A test case that fails with the existing code and that is fixed by your change. |
@@ -405,14 +405,14 @@ private static int rop(long g, long cp) { | |||
/* | |||
Formats the decimal f 10^e. | |||
*/ | |||
private int toChars(int f, int e) { | |||
private int toChars(long f, int e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find any calls to this method where the input f is long - they are all int. I think there will be a performance penalty here as there will need to be an implicit cast from int to long on all calls to this method after this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @pjfanning
Thank you for the respond.
The reason I changed the parameter type from int
to long
was to address a potential integer overflow issue when handling large float values, particularly during conversion to a decimal representation. However, I understand the concern regarding the potential performance impact due to implicit casting from int
to long
on all existing calls to this method. If all current usages only involve int
, I’m open to alternative approaches such as:
- Introducing an overloaded method
toChars(long f, int e)
without removing the existingint
version, to preserve performance for current usage patterns. - Re-evaluating whether
long
is truly necessary, or if overflow can be handled with validation before the call.
I’m happy to adjust the fix based on what would be best for both performance and maintainability. Please let me know your preferred direction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the start is to provide a broken test case. I'd prefer not to deal with theory and more interested in actual test cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code change doesn't even compile
Error: COMPILATION ERROR :
Error: /home/runner/work/jackson-core/jackson-core/src/main/java/com/fasterxml/jackson/core/io/schubfach/FloatToDecimal.java:[415,63] incompatible types: possible lossy conversion from long to int
Yeah we definitely need something to show a problem to solve, not a speculative fix without one. This has bit of a smell of tool/scanner reported concern. Also worth noting that code for "schubfach" is an outside contribution so if there's an issue, should probably be fixed there (but I don't remember specifically where it's from, @pjfanning may know). |
Comes from here: https://github.com/c4f7fcce9cb06515/Schubfach |
jackson-core/src/main/java/com/fasterxml/jackson/core/io/schubfach/FloatToDecimal.java
Line 408 in 6affde8
jackson-core/src/main/java/com/fasterxml/jackson/core/io/schubfach/FloatToDecimal.java
Line 415 in 6affde8
jackson-core/src/main/java/com/fasterxml/jackson/core/io/schubfach/FloatToDecimal.java
Line 426 in 6affde8
jackson-core/src/main/java/com/fasterxml/jackson/core/io/schubfach/FloatToDecimal.java
Line 440 in 6affde8
Fix the issue need to ensure that the type of the left-hand side (
f
) is at least as wide as the type of the right-hand side (pow10(H - len)
). Sincepow10
likely returns along
, the simplest and safest fix is to change the type off
fromint
tolong
. This avoids the implicit narrowing conversion and ensures that the computation can handle larger values without overflow.Steps to implement the fix:
f
fromint
tolong
in the methodtoChars
.f
to ensure compatibility with the new type.Compound assignment statements of the form
x += y
orx *= y
perform an implicit narrowing conversion if the type ofx
is narrower than the type ofy
.x += y
is equivalent tox = (T)(x + y)
, whereT
is the type ofx
. This can result in information loss and numeric errors such as overflows.References
Compound Assignment Operators, Narrowing Primitive Conversion
NUM00-J. Detect or prevent integer overflow