-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(CodeQL): resolve warning for implicit narrowing conversion #6465
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
Closed
halibobo1205
wants to merge
1
commit into
tronprotocol:release_v4.8.1
from
halibobo1205:fix/reactor_implicit_narrowing_conversion
+79
−4
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
75 changes: 75 additions & 0 deletions
75
framework/src/test/java/org/tron/common/math/ImplicitNarrowingConversionTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.tron.common.math; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * @see <a | ||
| * href="https://codeql.github.com/codeql-query-help/java/java-implicit-cast-in-compound-assignment" | ||
| * >Implicit narrowing conversion in compound assignment</a> | ||
| * | ||
| */ | ||
| public class ImplicitNarrowingConversionTest { | ||
|
|
||
| @Test | ||
| public void test() { | ||
| long l = 36714; | ||
| double d = (double) 50 / 64400 * 2210208; | ||
| long l1 = method1(l,d); | ||
| long l2 = method2(l,d); | ||
| long l3 = method3(l,d); | ||
| // l1 = 38429 | ||
| // l2 = l3 = 38430 | ||
| // d = 1715.9999999999998 | ||
| Assert.assertEquals(l2, l3); | ||
| Assert.assertNotEquals(l1, l2); | ||
| Assert.assertNotEquals(l1, l3); | ||
| } | ||
|
|
||
| /** | ||
| * code: | ||
| * <pre>{@code | ||
| * 0: lload_0 // load long l1 | ||
| * 1: dload_2 // load double d | ||
| * 2: d2l // convert double d to long ((truncates decimal)) | ||
| * 3: ladd // long + long integer addition | ||
| * 4: lreturn // return the result | ||
| * }</pre> | ||
| */ | ||
| private long method1(long l1, double d) { | ||
| return l1 + (long) (d); | ||
| } | ||
|
|
||
| /** | ||
| * code: | ||
| * <pre>{@code | ||
| * 0: lload_0 // load long l2 | ||
| * 1: l2d // promote long l2 to double | ||
| * 2: dload_2 // load double d | ||
| * 3: dadd // double + double floating-point addition | ||
| * 4: d2l // convert the result to long | ||
| * 5: lstore_0 // store the result back to long l2 (local variable) | ||
| * 6: lload_0 // reload l2 (for return) | ||
| * 7: lreturn // return the result | ||
| * }</pre> | ||
| */ | ||
| private long method2(long l2, double d) { | ||
| l2 += d; | ||
| return l2; | ||
| } | ||
|
|
||
| /** | ||
| * code: | ||
| * <pre>{@code | ||
| * 0: lload_0 // load long l3 | ||
| * 1: l2d // promote long l3 to double | ||
| * 2: dload_2 // load double d | ||
| * 3: dadd // double + double floating-point addition | ||
| * 4: d2l // convert the result to long | ||
| * 5: lreturn // return the result | ||
| * }</pre> | ||
| */ | ||
| private long method3(long l3, double d) { | ||
| return (long) (l3 + d); | ||
| } | ||
| } |
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.
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 understand it logically equals to previous
reward += voteRate * totalReward;, just wonder why you not revert to previous one?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.
Fix: Code scanning alerts java/implicit-cast-in-compound-assignment