Skip to content

Commit f4768db

Browse files
Constrain expression variables in unwritable files to not change. (#467)
1 parent 52fafae commit f4768db

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

clang/lib/3C/ProgramInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ void ProgramInfo::storePersistentConstraints(Expr *E, const CVarSet &Vars,
700700
// visited before. To avoid this, the expression is not cached and instead is
701701
// recomputed each time it's needed.
702702
if (PSL.valid() && Rewriter::isRewritable(E->getBeginLoc())) {
703+
if (!canWrite(PSL.getFileName())) {
704+
for (ConstraintVariable *CVar : Vars)
705+
CVar->constrainToWild(CS, "Expression in non-writable file", &PSL);
706+
}
703707
auto &ExprMap = isa<ImplicitCastExpr>(E) ? ImplicitCastConstraintVars
704708
: ExprConstraintVars;
705709
ExprMap[PSL].insert(Vars.begin(), Vars.end());

clang/test/3C/canwrite_constraints.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ inline void foo(int *p) {}
1414
int *foo_var = ((void *)0);
1515
// CHECK_HIGHER: _Ptr<int> foo_var = ((void *)0);
1616

17+
// Make sure we don't allow the types of casts in non-writable files to be
18+
// changed. This problem was seen with the inline definition of `atol(p)` as
19+
// `strtol(p, (char **) NULL, 10)` in the system headers.
20+
void strtol_like(int *p : itype(_Ptr<int>));
21+
void atol_like() {
22+
// expected-warning@+1 {{Expression in non-writable file}}
23+
strtol_like((int *) 0);
24+
}
25+
1726
// Make sure we do not add checked regions in non-writable files. This happened
1827
// incidentally in system headers in some other regression tests, but this is a
1928
// dedicated test for it.

0 commit comments

Comments
 (0)