Skip to content

Commit d37f0cb

Browse files
committed
Fix a crash
1 parent 59dadad commit d37f0cb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,14 @@ ProgramStateRef CStringChecker::checkInit(CheckerContext &C,
461461
const QualType ElemTy = Ctx.getBaseElementType(SuperR->getValueType());
462462
const NonLoc Zero = SVB.makeZeroArrayIndex();
463463

464-
Loc FirstElementVal =
465-
State->getLValue(ElemTy, Zero, loc::MemRegionVal(SuperR)).castAs<Loc>();
464+
std::optional<Loc> FirstElementVal =
465+
State->getLValue(ElemTy, Zero, loc::MemRegionVal(SuperR)).getAs<Loc>();
466+
if (!FirstElementVal)
467+
return State;
466468

467469
// Ensure that we wouldn't read uninitialized value.
468470
if (Filter.CheckCStringUninitializedRead &&
469-
State->getSVal(FirstElementVal.castAs<Loc>()).isUndef()) {
471+
State->getSVal(*FirstElementVal).isUndef()) {
470472
llvm::SmallString<258> Buf;
471473
llvm::raw_svector_ostream OS(Buf);
472474
OS << "The first element of the ";

clang/test/Analysis/bstring_UninitRead.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,11 @@ void ga_copy_strings() {
122122
memmove(alloc, ((char **)ga_copy_strings_from_0)[i], 1);
123123
}
124124

125+
// Creduced crash. In this case, retrieving the Loc for the first element failed.
126+
char mov_mdhd_language_map[][4] = {};
127+
int ff_mov_lang_to_iso639_code;
128+
char *ff_mov_lang_to_iso639_to;
129+
void ff_mov_lang_to_iso639() {
130+
memcpy(ff_mov_lang_to_iso639_to,
131+
mov_mdhd_language_map[ff_mov_lang_to_iso639_code], 4);
132+
}

0 commit comments

Comments
 (0)