Skip to content

Commit ba19d73

Browse files
committed
Address the remaining comments
1 parent 5c21678 commit ba19d73

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,6 @@ static const TypedValueRegion *getOriginRegion(const ElementRegion *ER) {
439439
return nullptr;
440440
Ret = sym2->getOriginRegion();
441441
}
442-
if (const auto *Elem = MR->getAs<ElementRegion>()) {
443-
Ret = Elem->getBaseRegion();
444-
}
445442
return dyn_cast_or_null<TypedValueRegion>(Ret);
446443
}
447444

@@ -460,10 +457,7 @@ ProgramStateRef CStringChecker::checkInit(CheckerContext &C,
460457
return nullptr;
461458

462459
const MemRegion *R = Element.getAsRegion();
463-
if (!R)
464-
return State;
465-
466-
const auto *ER = dyn_cast<ElementRegion>(R);
460+
const auto *ER = dyn_cast_or_null<ElementRegion>(R);
467461
if (!ER)
468462
return State;
469463

@@ -552,11 +546,11 @@ ProgramStateRef CStringChecker::checkInit(CheckerContext &C,
552546
}
553547
llvm::SmallString<258> Buf;
554548
llvm::raw_svector_ostream OS(Buf);
555-
OS << "The last element of the ";
556-
printIdxWithOrdinalSuffix(OS, Buffer.ArgumentIndex + 1);
557-
OS << " argument to access (the ";
549+
OS << "The last (";
558550
printIdxWithOrdinalSuffix(OS, IdxInt->getExtValue() + 1);
559-
OS << ") is undefined";
551+
OS << ") element to access in the ";
552+
printIdxWithOrdinalSuffix(OS, Buffer.ArgumentIndex + 1);
553+
OS << " argument is undefined";
560554
emitUninitializedReadBug(C, State, Buffer.Expression, OS.str());
561555
return nullptr;
562556
}

clang/test/Analysis/bstring_UninitRead.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void memcpy_array_fully_uninit(char *dst) {
2323
void memcpy_array_partially_uninit(char *dst) {
2424
char buf[10];
2525
buf[0] = 'i';
26-
memcpy(dst, buf, 10); // expected-warning{{The last element of the 2nd argument to access (the 10th) is undefined}}
26+
memcpy(dst, buf, 10); // expected-warning{{The last (10th) element to access in the 2nd argument is undefined}}
2727
// expected-note@-1{{Other elements might also be undefined}}
2828
(void)buf;
2929
}
@@ -38,8 +38,23 @@ void memcpy_array_only_init_portion(char *dst) {
3838
void memcpy_array_partially_init_error(char *dst) {
3939
char buf[10];
4040
buf[0] = 'i';
41-
memcpy(dst, buf, 2); // expected-warning{{The last element of the 2nd argument to access (the 2nd) is undefined}}
42-
// expected-note@-1{{Other elements might also be undefined}}
41+
memcpy(dst, buf, 2); // expected-warning{{The last (2nd) element to access in the 2nd argument is undefined}}
42+
// expected-note@-1{{Other elements might also be undefined}}
43+
(void)buf;
44+
}
45+
46+
// The interesting case here is that the portion we're copying is initialized,
47+
// but not the whole matrix. We need to be careful to extract buf[1], and not
48+
// buf when trying to peel region layers off from the source argument.
49+
void memcpy_array_from_matrix(char *dst) {
50+
char buf[2][2];
51+
buf[1][0] = 'i';
52+
buf[1][1] = 'j';
53+
// FIXME: This is a FP -- we mistakenly retrieve the first element of buf,
54+
// instead of the first element of buf[1]. getLValueElement simply peels off
55+
// another ElementRegion layer, when in this case it really shouldn't.
56+
memcpy(dst, buf[1], 2); // expected-warning{{The first element of the 2nd argument is undefined}}
57+
// expected-note@-1{{Other elements might also be undefined}}
4358
(void)buf;
4459
}
4560

@@ -96,8 +111,8 @@ void mempcpy_struct_fully_uninit() {
96111
mempcpy(&s2, &s1, sizeof(struct st));
97112
}
98113

99-
// Creduced crash.
100-
114+
// Creduced crash. In this case, an symbolicregion is wrapped in an
115+
// elementregion for the src argument.
101116
void *ga_copy_strings_from_0;
102117
void *memmove();
103118
void alloc();

0 commit comments

Comments
 (0)