Skip to content

Commit

Permalink
Forbid initializing string arrays with literals that are too long.
Browse files Browse the repository at this point in the history
Bug: issue #971
Test: new test case, modified corpus
  • Loading branch information
dvander committed Jun 6, 2024
1 parent a30c0d9 commit 944d5fc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/array-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,12 @@ bool ArrayValidator::ValidateRank(ArrayType* rank, Expr* init) {
return false;
}

auto cells = char_array_cells(str->text()->length() + 1);
auto bytes = str->text()->length() + 1;
auto cells = char_array_cells(bytes);
if (!AddCells(cells))
return false;

if (rank->size() && cells > rank->size()) {
if (rank->size() && bytes > rank->size()) {
report(str->pos(), 47);
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/compile-only/fail-string-too-big-for-array.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
char str[10] = "asdasdsadassadadsdas";

public main() {}
1 change: 1 addition & 0 deletions tests/compile-only/fail-string-too-big-for-array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(1) : error 047: array sizes do not match, or destination array is too small

0 comments on commit 944d5fc

Please sign in to comment.