Skip to content

Commit aa31d42

Browse files
committed
Check declared array length for IndexAccess in TypeChecker.
1 parent d28d552 commit aa31d42

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

libsolidity/analysis/TypeChecker.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
*/
2323

2424
#include <libsolidity/analysis/TypeChecker.h>
25+
26+
#include "DeclarationTypeChecker.h"
27+
2528
#include <libsolidity/ast/AST.h>
2629
#include <libsolidity/ast/ASTUtils.h>
27-
#include <libsolidity/ast/UserDefinableOperators.h>
2830
#include <libsolidity/ast/TypeProvider.h>
31+
#include <libsolidity/ast/UserDefinableOperators.h>
2932

3033
#include <libyul/AsmAnalysis.h>
3134
#include <libyul/AsmAnalysisInfo.h>
@@ -3481,7 +3484,11 @@ bool TypeChecker::visit(IndexAccess const& _access)
34813484
if (expectType(*index, *TypeProvider::uint256()))
34823485
{
34833486
if (auto indexValue = dynamic_cast<RationalNumberType const*>(type(*index)))
3487+
{
34843488
length = indexValue->literalValue(nullptr);
3489+
if (length < 1)
3490+
m_errorReporter.typeError(7015_error, _access.location(), "Array with zero length specified.");
3491+
}
34853492
else
34863493
m_errorReporter.fatalTypeError(3940_error, index->location(), "Integer constant expected.");
34873494
}

test/libsolidity/syntaxTests/array/contract_index_access.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
contract C {
22
function f() view public {
3-
C[0];
3+
C[1];
44
}
55
}
66
// ----

test/libsolidity/syntaxTests/array/invalid/library_index_access.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library C {
22
function f() view public {
3-
C[0];
3+
C[1];
44
}
55
}
66
// ----
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
contract C {
2+
function f0() public {
3+
abi.decode("", (int[0]));
4+
}
5+
6+
function f1() public {
7+
abi.decode("", (int[0][]));
8+
}
9+
10+
function f2() public {
11+
abi.decode("", (int[][0]));
12+
}
13+
14+
function f3() public {
15+
abi.decode("", (int[][0][]));
16+
}
17+
18+
function f4() public {
19+
abi.decode("", (int[][][0]));
20+
}
21+
22+
struct S { uint t; }
23+
function f5() public {
24+
abi.decode("", (S[][][0]));
25+
}
26+
27+
function f6() public {
28+
C[0];
29+
}
30+
}
31+
// ----
32+
// TypeError 7015: (65-71): Array with zero length specified.
33+
// TypeError 7015: (134-140): Array with zero length specified.
34+
// TypeError 7015: (205-213): Array with zero length specified.
35+
// TypeError 7015: (276-284): Array with zero length specified.
36+
// TypeError 7015: (349-359): Array with zero length specified.
37+
// TypeError 7015: (447-455): Array with zero length specified.
38+
// TypeError 7015: (501-505): Array with zero length specified.

0 commit comments

Comments
 (0)