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

[java] Fix AvoidUsingOctalValues false-positive #5020

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public boolean isIntegral() {
* for the literal {@code 0} (which can really be any base).
*/
public int getBase() {
return getBase(getLiteralText());
return getBase(getLiteralText(), isIntegral());
}

static int getBase(Chars image) {
static int getBase(Chars image, boolean isIntegral) {
if (image.length() > 1 && image.charAt(0) == '0') {
switch (image.charAt(1)) {
case 'x':
Expand All @@ -132,10 +132,8 @@ static int getBase(Chars image) {
case 'b':
case 'B':
return 2;
case '.':
return 10;
default:
return 8;
return isIntegral ? 8 : 10;
}
}
return 10;
Expand Down Expand Up @@ -172,7 +170,7 @@ public double getValueAsDouble() {
* <p>Invalid literals or overflows result in {@code 0L}.
*/
static long parseIntegralValue(Chars image) {
final int base = getBase(image);
final int base = getBase(image, true);
if (base == 8) {
image = image.subSequence(1); // 0
} else if (base != 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ public class Foo {
]]></code>
</test-code>

<test-code>
<description>ok, float literal in hexadecimal and exponent</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
float f = 0x1.0000_ffff_eeeep+2f;
}
]]></code>
</test-code>

<test-code>
<description>ok, Lengthy numeric literal with variable name as serialVersionUID</description>
<expected-problems>0</expected-problems>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ public class Foo {
]]></code>
</test-code>

<test-code>
<description>OK, double value</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d = 012.0;
}
]]></code>
</test-code>

<test-code>
<description>OK, double suffix</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d1 = 012d;
double d2 = 012D;
}
]]></code>
</test-code>

<test-code>
<description>OK, float suffix</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
float f = 012f;
float f = 012F;
}
]]></code>
</test-code>

<test-code>
<description>OK, double value with exponent</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d = 012e0;
}
]]></code>
</test-code>

<test-code>
<description>bad, 012L</description>
<expected-problems>1</expected-problems>
Expand Down