Skip to content

Commit c639a5d

Browse files
committed
M68kNumberExpression: catch NumberFormatException (out of range)
1 parent 61d841d commit c639a5d

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

plugin-resources/messages/M68kBundle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ documentation.destination=Destination:
126126
documentation.section.cpu=CPU:
127127
documentation.section.privileged=Privileged:
128128

129+
documentation.number.expression.invalid=Invalid number value
130+
129131
cpu.group.GROUP_68000_UP=MC68000 Family
130132
cpu.group.GROUP_68010_UP=MC68010+
131133
cpu.group.GROUP_68020_UP=MC68020+

src/com/yanncebron/m68kplugin/documentation/M68kNumberExpressionDocumentationProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.intellij.psi.PsiFile;
2323
import com.intellij.psi.util.PsiTreeUtil;
2424
import com.intellij.util.ObjectUtils;
25+
import com.yanncebron.m68kplugin.M68kBundle;
2526
import com.yanncebron.m68kplugin.lang.psi.M68kTokenGroups;
2627
import com.yanncebron.m68kplugin.lang.psi.expression.M68kNumberExpression;
2728
import org.jetbrains.annotations.NotNull;
@@ -34,7 +35,9 @@ final class M68kNumberExpressionDocumentationProvider extends AbstractDocumentat
3435
if (!(element instanceof M68kNumberExpression numberExpression)) return null;
3536

3637
Integer originalValue = ObjectUtils.tryCast(numberExpression.getValue(), Integer.class);
37-
assert originalValue != null : numberExpression.getText();
38+
if (originalValue == null) {
39+
return M68kBundle.message("documentation.number.expression.invalid");
40+
}
3841

3942
return "<code>" +
4043
doGetValueText(element, Integer.toString(originalValue)) + "<br>" +

src/com/yanncebron/m68kplugin/lang/psi/expression/impl/M68kNumberExpressionMixIn.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
import com.yanncebron.m68kplugin.lang.psi.expression.M68kNumberExpression;
2424
import com.yanncebron.m68kplugin.lang.psi.expression.M68kUnaryMinusExpression;
2525
import org.jetbrains.annotations.NotNull;
26+
import org.jetbrains.annotations.Nullable;
2627

2728
abstract class M68kNumberExpressionMixIn extends ASTWrapperPsiElement implements M68kNumberExpression {
2829

2930
protected M68kNumberExpressionMixIn(@NotNull ASTNode node) {
3031
super(node);
3132
}
3233

34+
/**
35+
* @return {@code null} for invalid number (out of range)
36+
*/
3337
@Override
3438
public Object getValue() {
3539
String text = getText();
@@ -54,7 +58,12 @@ public Object getValue() {
5458
throw new IllegalArgumentException("could not determine getValue() for " + elementType + ", '" + text + "'");
5559
}
5660

61+
@Nullable
5762
private static Integer parseNumber(String text, boolean isNegative, int radix) {
58-
return Integer.parseInt(isNegative ? "-" + text : text, radix);
63+
try {
64+
return Integer.parseInt(isNegative ? "-" + text : text, radix);
65+
} catch (NumberFormatException e) {
66+
return null;
67+
}
5968
}
6069
}

tests/com/yanncebron/m68kplugin/documentation/M68kNumberExpressionDocumentationProviderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 The Authors
2+
* Copyright 2026 The Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,10 @@ public void testGenerateHoverDoc() {
2828
"<code><span style=\"color:#0000ff;\">42</span><br><span style=\"color:#0000ff;\">$2a</span><br><span style=\"color:#0000ff;\">@52</span><br><span style=\"color:#0000ff;\">%101010</span><br></code>");
2929
}
3030

31+
public void testGenerateHoverDocInvalidValue() {
32+
doTestGenerateDoc(" dc.b -214748<caret>3649","Invalid number value");
33+
}
34+
3135
private void doTestGenerateDoc(String source, String docText) {
3236
myFixture.configureByText("a.s", source);
3337

tests/com/yanncebron/m68kplugin/lang/psi/expression/NumberExpressionPsiTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.intellij.util.containers.ContainerUtil;
2121
import com.yanncebron.m68kplugin.lang.psi.M68kPsiTestCase;
2222
import com.yanncebron.m68kplugin.lang.psi.directive.M68kDcDirective;
23+
import org.jetbrains.annotations.Nullable;
2324

2425
public class NumberExpressionPsiTest extends M68kPsiTestCase<M68kDcDirective> {
2526

@@ -47,7 +48,15 @@ public void testGetValueBinary() {
4748
doTestGetValue("-%01011", -11);
4849
}
4950

50-
private void doTestGetValue(String numberValue, Integer expectedValue) {
51+
public void testGetValueFailsForOverflow() {
52+
doTestGetValue("" + Integer.MAX_VALUE , Integer.MAX_VALUE);
53+
doTestGetValue("" + Integer.MAX_VALUE + 1, null);
54+
55+
doTestGetValue("-2147483648", Integer.MIN_VALUE);
56+
doTestGetValue("-2147483649", null);
57+
}
58+
59+
private void doTestGetValue(String numberValue, @Nullable Integer expectedValue) {
5160
final M68kExpression expression = parseNumber(numberValue);
5261
final M68kNumberExpression numberExpression = PsiTreeUtil.findChildOfType(expression, M68kNumberExpression.class, false);
5362
assertNotNull(numberExpression);

0 commit comments

Comments
 (0)