Skip to content

Commit b2da8ef

Browse files
camccamc
andauthored
[clang][bytecode] Fix crash when array index is past end of array in C (#165186)
Fixes #165090 Make sure to reject invalid array pointer offsets in C. Co-authored-by: camc <[email protected]>
1 parent 48cc443 commit b2da8ef

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,7 @@ std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
22832283
}
22842284
}
22852285

2286-
if (Invalid && S.getLangOpts().CPlusPlus)
2286+
if (Invalid && (S.getLangOpts().CPlusPlus || Ptr.inArray()))
22872287
return std::nullopt;
22882288

22892289
// Offset is valid - compute it on unsigned.

clang/test/AST/ByteCode/c.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,9 @@ static char foo_(a) // all-warning {{definition without a prototype}}
381381
static void bar_(void) {
382382
foo_(foo_(1));
383383
}
384+
385+
void foo2(void*);
386+
void bar2(void) {
387+
int a[2][3][4][5]; // all-note {{array 'a' declared here}}
388+
foo2(&a[0][4]); // all-warning {{array index 4 is past the end of the array}}
389+
}

0 commit comments

Comments
 (0)