Skip to content

Commit

Permalink
Detect integer overflows in list size
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed May 8, 2024
1 parent c2d3620 commit ce11b9f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ private SemType toSemType(String typeExpr) {
int leftBracketPos = typeExpr.indexOf('[');
final Map<String, SemType> typeNameSemTypeMap = semtypeEnv.getTypeNameSemTypeMap();
if (leftBracketPos == -1) {
return typeNameSemTypeMap.get(typeExpr);
SemType referredType = typeNameSemTypeMap.get(typeExpr);
if (referredType == null) {
throw new IllegalArgumentException("No such type: " + typeExpr);
}
return referredType;
}
int rightBracketPos = typeExpr.indexOf(']');
String typeRef = typeExpr.substring(0, leftBracketPos);
Expand Down Expand Up @@ -198,6 +202,13 @@ public void visit(ModulePartNode modulePartNode) {
*/
record TypeAssertion(Context context, String fileName, SemType lhs, SemType rhs, RelKind kind, String text) {

TypeAssertion {
if (kind != null) {
assert lhs != null;
assert rhs != null;
}
}

@Override
public String toString() {
return Paths.get(fileName).getFileName().toString() + ": " + text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,23 @@ private SemType resolveTypeDesc(Context cx, Map<String, BLangNode> mod, BLangTyp

private static int from(Map<String, BLangNode> mod, BLangNode expr) {
if (expr instanceof BLangLiteral literal) {
return (int) literal.value;
return listSize((Number) literal.value);
} else if (expr instanceof BLangSimpleVarRef varRef) {
String varName = varRef.variableName.value;
return from(mod, mod.get(varName));
} else if (expr instanceof BLangConstant constant) {
Number val = (Number) constant.symbol.value.value;
return val.intValue();
return listSize((Number) constant.symbol.value.value);
}
throw new UnsupportedOperationException("Unsupported expr kind " + expr.getKind());
}

private static int listSize(Number size) {
if (size.longValue() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("list sizes greater than " + Integer.MAX_VALUE + " not yet supported");
}
return size.intValue();
}

private SemType resolveListInner(Context cx, int size, SemType eType) {
ListDefinition ld = new ListDefinition();
return resolveListInner(cx, ld, size, eType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ public void shouldFailForIncorrectTestStructure() {
testSemTypeAssertions(typeAssertions.get(0));
}

@Test(expectedExceptions = AssertionError.class)
public void shouldFailForTooLargeLists() {
File wrongAssertionFile = resolvePath("test-src/fixed-length-array-too-large-te.bal").toFile();
List<SemTypeAssertionTransformer.TypeAssertion> typeAssertions = getTypeAssertions(wrongAssertionFile);
testSemTypeAssertions(typeAssertions.get(0));
}

@Test(dataProvider = "type-rel-provider")
public void testSemTypeAssertions(SemTypeAssertionTransformer.TypeAssertion typeAssertion) {
if (typeAssertion.kind() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public const int MAX_VALUE = 9223372036854775807;
type LargeArray int[MAX_VALUE]; // @error
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@

type IntArray int[];
type Int5 int[5];
type ISTArray (1|2|3)[];

public const int MAX_VALUE = 9223372036854775807;

public const int MAX_VALUE = 2147483637;
public const int MAX_VALUE_M_1 = MAX_VALUE - 1;

// -@type LargeArray < IntArray
// @type LargeArray < IntArray
type LargeArray int[MAX_VALUE];

// @type LargeArray2 < IntArray
// -@type LargeArray <> LargeArray2
// @type LargeArray <> LargeArray2
type LargeArray2 int[MAX_VALUE_M_1];

// -@type Int5Intersection = Int5
Expand All @@ -21,4 +19,3 @@ type Int10000 int[100000];

// -@type ISTArray < I10000A
type I10000A Int10000|(!Int10000 & IntArray);

0 comments on commit ce11b9f

Please sign in to comment.