[clang][analyzer] Model getline/getdelim preconditions and evaluation (#83027)
According to POSIX 2018. 1. lineptr, n and stream can not be NULL. 2. If *n is non-zero, *lineptr must point to a region of at least *n bytes, or be a NULL pointer. Additionally, if *lineptr is not NULL, *n must not be undefined.
This commit is contained in:
parent
a62441d4bb
commit
730ca47a0c
@ -15,7 +15,6 @@
|
||||
|
||||
#include "ProgramState_Fwd.h"
|
||||
#include "SVals.h"
|
||||
|
||||
#include "clang/AST/OperationKinds.h"
|
||||
#include "clang/AST/Stmt.h"
|
||||
#include "clang/Basic/OperatorKinds.h"
|
||||
@ -113,8 +112,7 @@ public:
|
||||
OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,
|
||||
bool IsBinary);
|
||||
|
||||
std::optional<DefinedSVal> getPointeeDefVal(SVal PtrSVal,
|
||||
ProgramStateRef State);
|
||||
std::optional<SVal> getPointeeVal(SVal PtrSVal, ProgramStateRef State);
|
||||
|
||||
} // namespace ento
|
||||
|
||||
|
@ -1441,7 +1441,7 @@ void MallocChecker::preGetdelim(const CallEvent &Call,
|
||||
return;
|
||||
|
||||
ProgramStateRef State = C.getState();
|
||||
const auto LinePtr = getPointeeDefVal(Call.getArgSVal(0), State);
|
||||
const auto LinePtr = getPointeeVal(Call.getArgSVal(0), State);
|
||||
if (!LinePtr)
|
||||
return;
|
||||
|
||||
@ -1470,8 +1470,10 @@ void MallocChecker::checkGetdelim(const CallEvent &Call,
|
||||
|
||||
SValBuilder &SVB = C.getSValBuilder();
|
||||
|
||||
const auto LinePtr = getPointeeDefVal(Call.getArgSVal(0), State);
|
||||
const auto Size = getPointeeDefVal(Call.getArgSVal(1), State);
|
||||
const auto LinePtr =
|
||||
getPointeeVal(Call.getArgSVal(0), State)->getAs<DefinedSVal>();
|
||||
const auto Size =
|
||||
getPointeeVal(Call.getArgSVal(1), State)->getAs<DefinedSVal>();
|
||||
if (!LinePtr || !Size || !LinePtr->getAsRegion())
|
||||
return;
|
||||
|
||||
|
@ -1200,10 +1200,25 @@ void StreamChecker::evalGetdelim(const FnDescription *Desc,
|
||||
|
||||
// Add transition for the successful state.
|
||||
NonLoc RetVal = makeRetVal(C, E.CE).castAs<NonLoc>();
|
||||
ProgramStateRef StateNotFailed =
|
||||
State->BindExpr(E.CE, C.getLocationContext(), RetVal);
|
||||
ProgramStateRef StateNotFailed = E.bindReturnValue(State, C, RetVal);
|
||||
StateNotFailed =
|
||||
E.assumeBinOpNN(StateNotFailed, BO_GE, RetVal, E.getZeroVal(Call));
|
||||
|
||||
// On success, a buffer is allocated.
|
||||
auto NewLinePtr = getPointeeVal(Call.getArgSVal(0), State);
|
||||
if (NewLinePtr && isa<DefinedOrUnknownSVal>(*NewLinePtr))
|
||||
StateNotFailed = StateNotFailed->assume(
|
||||
NewLinePtr->castAs<DefinedOrUnknownSVal>(), true);
|
||||
|
||||
// The buffer size `*n` must be enough to hold the whole line, and
|
||||
// greater than the return value, since it has to account for '\0'.
|
||||
SVal SizePtrSval = Call.getArgSVal(1);
|
||||
auto NVal = getPointeeVal(SizePtrSval, State);
|
||||
if (NVal && isa<NonLoc>(*NVal)) {
|
||||
StateNotFailed = E.assumeBinOpNN(StateNotFailed, BO_GT,
|
||||
NVal->castAs<NonLoc>(), RetVal);
|
||||
StateNotFailed = E.bindReturnValue(StateNotFailed, C, RetVal);
|
||||
}
|
||||
if (!StateNotFailed)
|
||||
return;
|
||||
C.addTransition(StateNotFailed);
|
||||
@ -1217,6 +1232,10 @@ void StreamChecker::evalGetdelim(const FnDescription *Desc,
|
||||
E.isStreamEof() ? ErrorFEof : ErrorFEof | ErrorFError;
|
||||
StateFailed = E.setStreamState(
|
||||
StateFailed, StreamState::getOpened(Desc, NewES, !NewES.isFEof()));
|
||||
// On failure, the content of the buffer is undefined.
|
||||
if (auto NewLinePtr = getPointeeVal(Call.getArgSVal(0), State))
|
||||
StateFailed = StateFailed->bindLoc(*NewLinePtr, UndefinedVal(),
|
||||
C.getLocationContext());
|
||||
C.addTransition(StateFailed, E.getFailureNoteTag(this, C));
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
||||
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
||||
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
|
||||
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
@ -44,10 +45,23 @@ namespace {
|
||||
class UnixAPIMisuseChecker
|
||||
: public Checker<check::PreCall, check::ASTDecl<TranslationUnitDecl>> {
|
||||
const BugType BT_open{this, "Improper use of 'open'", categories::UnixAPI};
|
||||
const BugType BT_getline{this, "Improper use of getdelim",
|
||||
categories::UnixAPI};
|
||||
const BugType BT_pthreadOnce{this, "Improper use of 'pthread_once'",
|
||||
categories::UnixAPI};
|
||||
const BugType BT_ArgumentNull{this, "NULL pointer", categories::UnixAPI};
|
||||
mutable std::optional<uint64_t> Val_O_CREAT;
|
||||
|
||||
ProgramStateRef
|
||||
EnsurePtrNotNull(SVal PtrVal, const Expr *PtrExpr, CheckerContext &C,
|
||||
ProgramStateRef State, const StringRef PtrDescr,
|
||||
std::optional<std::reference_wrapper<const BugType>> BT =
|
||||
std::nullopt) const;
|
||||
|
||||
ProgramStateRef EnsureGetdelimBufferAndSizeCorrect(
|
||||
SVal LinePtrPtrSVal, SVal SizePtrSVal, const Expr *LinePtrPtrExpr,
|
||||
const Expr *SizePtrExpr, CheckerContext &C, ProgramStateRef State) const;
|
||||
|
||||
public:
|
||||
void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager &Mgr,
|
||||
BugReporter &BR) const;
|
||||
@ -56,6 +70,7 @@ public:
|
||||
|
||||
void CheckOpen(CheckerContext &C, const CallEvent &Call) const;
|
||||
void CheckOpenAt(CheckerContext &C, const CallEvent &Call) const;
|
||||
void CheckGetDelim(CheckerContext &C, const CallEvent &Call) const;
|
||||
void CheckPthreadOnce(CheckerContext &C, const CallEvent &Call) const;
|
||||
|
||||
void CheckOpenVariant(CheckerContext &C, const CallEvent &Call,
|
||||
@ -95,6 +110,30 @@ private:
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
ProgramStateRef UnixAPIMisuseChecker::EnsurePtrNotNull(
|
||||
SVal PtrVal, const Expr *PtrExpr, CheckerContext &C, ProgramStateRef State,
|
||||
const StringRef PtrDescr,
|
||||
std::optional<std::reference_wrapper<const BugType>> BT) const {
|
||||
const auto Ptr = PtrVal.getAs<DefinedSVal>();
|
||||
if (!Ptr)
|
||||
return State;
|
||||
|
||||
const auto [PtrNotNull, PtrNull] = State->assume(*Ptr);
|
||||
if (!PtrNotNull && PtrNull) {
|
||||
if (ExplodedNode *N = C.generateErrorNode(PtrNull)) {
|
||||
auto R = std::make_unique<PathSensitiveBugReport>(
|
||||
BT.value_or(std::cref(BT_ArgumentNull)),
|
||||
(PtrDescr + " pointer might be NULL.").str(), N);
|
||||
if (PtrExpr)
|
||||
bugreporter::trackExpressionValue(N, PtrExpr, *R);
|
||||
C.emitReport(std::move(R));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return PtrNotNull;
|
||||
}
|
||||
|
||||
void UnixAPIMisuseChecker::checkASTDecl(const TranslationUnitDecl *TU,
|
||||
AnalysisManager &Mgr,
|
||||
BugReporter &) const {
|
||||
@ -137,6 +176,9 @@ void UnixAPIMisuseChecker::checkPreCall(const CallEvent &Call,
|
||||
|
||||
else if (FName == "pthread_once")
|
||||
CheckPthreadOnce(C, Call);
|
||||
|
||||
else if (is_contained({"getdelim", "getline"}, FName))
|
||||
CheckGetDelim(C, Call);
|
||||
}
|
||||
void UnixAPIMisuseChecker::ReportOpenBug(CheckerContext &C,
|
||||
ProgramStateRef State,
|
||||
@ -215,8 +257,7 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
|
||||
OS << "Call to '" << VariantName << "' with more than " << MaxArgCount
|
||||
<< " arguments";
|
||||
|
||||
ReportOpenBug(C, state,
|
||||
SBuf.c_str(),
|
||||
ReportOpenBug(C, state, SBuf.c_str(),
|
||||
Call.getArgExpr(MaxArgCount)->getSourceRange());
|
||||
return;
|
||||
}
|
||||
@ -266,6 +307,93 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// getdelim and getline
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
ProgramStateRef UnixAPIMisuseChecker::EnsureGetdelimBufferAndSizeCorrect(
|
||||
SVal LinePtrPtrSVal, SVal SizePtrSVal, const Expr *LinePtrPtrExpr,
|
||||
const Expr *SizePtrExpr, CheckerContext &C, ProgramStateRef State) const {
|
||||
static constexpr llvm::StringLiteral SizeGreaterThanBufferSize =
|
||||
"The buffer from the first argument is smaller than the size "
|
||||
"specified by the second parameter";
|
||||
static constexpr llvm::StringLiteral SizeUndef =
|
||||
"The buffer from the first argument is not NULL, but the size specified "
|
||||
"by the second parameter is undefined.";
|
||||
|
||||
auto EmitBugReport = [this, &C, SizePtrExpr, LinePtrPtrExpr](
|
||||
ProgramStateRef BugState, StringRef ErrMsg) {
|
||||
if (ExplodedNode *N = C.generateErrorNode(BugState)) {
|
||||
auto R = std::make_unique<PathSensitiveBugReport>(BT_getline, ErrMsg, N);
|
||||
bugreporter::trackExpressionValue(N, SizePtrExpr, *R);
|
||||
bugreporter::trackExpressionValue(N, LinePtrPtrExpr, *R);
|
||||
C.emitReport(std::move(R));
|
||||
}
|
||||
};
|
||||
|
||||
// We have a pointer to a pointer to the buffer, and a pointer to the size.
|
||||
// We want what they point at.
|
||||
auto LinePtrSVal = getPointeeVal(LinePtrPtrSVal, State)->getAs<DefinedSVal>();
|
||||
auto NSVal = getPointeeVal(SizePtrSVal, State);
|
||||
if (!LinePtrSVal || !NSVal || NSVal->isUnknown())
|
||||
return nullptr;
|
||||
|
||||
assert(LinePtrPtrExpr && SizePtrExpr);
|
||||
|
||||
const auto [LinePtrNotNull, LinePtrNull] = State->assume(*LinePtrSVal);
|
||||
if (LinePtrNotNull && !LinePtrNull) {
|
||||
// If `*lineptr` is not null, but `*n` is undefined, there is UB.
|
||||
if (NSVal->isUndef()) {
|
||||
EmitBugReport(LinePtrNotNull, SizeUndef);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If it is defined, and known, its size must be less than or equal to
|
||||
// the buffer size.
|
||||
auto NDefSVal = NSVal->getAs<DefinedSVal>();
|
||||
auto &SVB = C.getSValBuilder();
|
||||
auto LineBufSize =
|
||||
getDynamicExtent(LinePtrNotNull, LinePtrSVal->getAsRegion(), SVB);
|
||||
auto LineBufSizeGtN = SVB.evalBinOp(LinePtrNotNull, BO_GE, LineBufSize,
|
||||
*NDefSVal, SVB.getConditionType())
|
||||
.getAs<DefinedOrUnknownSVal>();
|
||||
if (!LineBufSizeGtN)
|
||||
return LinePtrNotNull;
|
||||
if (auto LineBufSizeOk = LinePtrNotNull->assume(*LineBufSizeGtN, true))
|
||||
return LineBufSizeOk;
|
||||
|
||||
EmitBugReport(LinePtrNotNull, SizeGreaterThanBufferSize);
|
||||
return nullptr;
|
||||
}
|
||||
return State;
|
||||
}
|
||||
|
||||
void UnixAPIMisuseChecker::CheckGetDelim(CheckerContext &C,
|
||||
const CallEvent &Call) const {
|
||||
ProgramStateRef State = C.getState();
|
||||
|
||||
// The parameter `n` must not be NULL.
|
||||
SVal SizePtrSval = Call.getArgSVal(1);
|
||||
State = EnsurePtrNotNull(SizePtrSval, Call.getArgExpr(1), C, State, "Size");
|
||||
if (!State)
|
||||
return;
|
||||
|
||||
// The parameter `lineptr` must not be NULL.
|
||||
SVal LinePtrPtrSVal = Call.getArgSVal(0);
|
||||
State =
|
||||
EnsurePtrNotNull(LinePtrPtrSVal, Call.getArgExpr(0), C, State, "Line");
|
||||
if (!State)
|
||||
return;
|
||||
|
||||
State = EnsureGetdelimBufferAndSizeCorrect(LinePtrPtrSVal, SizePtrSval,
|
||||
Call.getArgExpr(0),
|
||||
Call.getArgExpr(1), C, State);
|
||||
if (!State)
|
||||
return;
|
||||
|
||||
C.addTransition(State);
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// pthread_once
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
@ -183,10 +183,9 @@ OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<DefinedSVal> getPointeeDefVal(SVal PtrSVal,
|
||||
ProgramStateRef State) {
|
||||
std::optional<SVal> getPointeeVal(SVal PtrSVal, ProgramStateRef State) {
|
||||
if (const auto *Ptr = PtrSVal.getAsRegion()) {
|
||||
return State->getSVal(Ptr).getAs<DefinedSVal>();
|
||||
return State->getSVal(Ptr);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
322
clang/test/Analysis/getline-unixapi.c
Normal file
322
clang/test/Analysis/getline-unixapi.c
Normal file
@ -0,0 +1,322 @@
|
||||
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,debug.ExprInspection -verify %s
|
||||
|
||||
#include "Inputs/system-header-simulator.h"
|
||||
#include "Inputs/system-header-simulator-for-malloc.h"
|
||||
#include "Inputs/system-header-simulator-for-valist.h"
|
||||
|
||||
void clang_analyzer_eval(int);
|
||||
void clang_analyzer_dump_int(int);
|
||||
void clang_analyzer_dump_ptr(void*);
|
||||
void clang_analyzer_warnIfReached();
|
||||
|
||||
void test_getline_null_lineptr() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char **buffer = NULL;
|
||||
size_t n = 0;
|
||||
getline(buffer, &n, F1); // expected-warning {{Line pointer might be NULL}}
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getline_null_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
getline(&buffer, NULL, F1); // expected-warning {{Size pointer might be NULL}}
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getline_null_buffer_size_gt0() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
size_t n = 8;
|
||||
getline(&buffer, &n, F1); // ok since posix 2018
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getline_null_buffer_size_gt0_2(size_t n) {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
if (n > 0) {
|
||||
getline(&buffer, &n, F1); // ok since posix 2018
|
||||
}
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getline_null_buffer_unknown_size(size_t n) {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
|
||||
getline(&buffer, &n, F1); // ok
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_null_buffer_undef_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char *buffer = NULL;
|
||||
size_t n;
|
||||
|
||||
getline(&buffer, &n, F1); // ok since posix 2018
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_buffer_size_0() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char *buffer = malloc(10);
|
||||
size_t n = 0;
|
||||
if (buffer != NULL)
|
||||
getline(&buffer, &n, F1); // ok, the buffer is enough for 0 character
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_buffer_bad_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char *buffer = malloc(10);
|
||||
size_t n = 100;
|
||||
if (buffer != NULL)
|
||||
getline(&buffer, &n, F1); // expected-warning {{The buffer from the first argument is smaller than the size specified by the second parameter}}
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_buffer_smaller_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char *buffer = malloc(100);
|
||||
size_t n = 10;
|
||||
if (buffer != NULL)
|
||||
getline(&buffer, &n, F1); // ok, there is enough space for 10 characters
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_buffer_undef_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
|
||||
char *buffer = malloc(100);
|
||||
size_t n;
|
||||
if (buffer != NULL)
|
||||
getline(&buffer, &n, F1); // expected-warning {{The buffer from the first argument is not NULL, but the size specified by the second parameter is undefined}}
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
|
||||
void test_getline_null_buffer() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
size_t n = 0;
|
||||
ssize_t r = getline(&buffer, &n, F1);
|
||||
// getline returns -1 on failure, number of char reads on success (>= 0)
|
||||
if (r < -1) {
|
||||
clang_analyzer_warnIfReached(); // must not happen
|
||||
} else {
|
||||
// The buffer could be allocated both on failure and success
|
||||
clang_analyzer_dump_int(n); // expected-warning {{conj_$}}
|
||||
clang_analyzer_dump_ptr(buffer); // expected-warning {{conj_$}}
|
||||
}
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getdelim_null_size() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
getdelim(&buffer, NULL, ',', F1); // expected-warning {{Size pointer might be NULL}}
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getdelim_null_buffer_size_gt0() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
size_t n = 8;
|
||||
getdelim(&buffer, &n, ';', F1); // ok since posix 2018
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getdelim_null_buffer_size_gt0_2(size_t n) {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
if (n > 0) {
|
||||
getdelim(&buffer, &n, ' ', F1); // ok since posix 2018
|
||||
}
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getdelim_null_buffer_unknown_size(size_t n) {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
getdelim(&buffer, &n, '-', F1); // ok
|
||||
fclose(F1);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getdelim_null_buffer() {
|
||||
FILE *F1 = tmpfile();
|
||||
if (!F1)
|
||||
return;
|
||||
char *buffer = NULL;
|
||||
size_t n = 0;
|
||||
ssize_t r = getdelim(&buffer, &n, '\r', F1);
|
||||
// getdelim returns -1 on failure, number of char reads on success (>= 0)
|
||||
if (r < -1) {
|
||||
clang_analyzer_warnIfReached(); // must not happen
|
||||
}
|
||||
else {
|
||||
// The buffer could be allocated both on failure and success
|
||||
clang_analyzer_dump_int(n); // expected-warning {{conj_$}}
|
||||
clang_analyzer_dump_ptr(buffer); // expected-warning {{conj_$}}
|
||||
}
|
||||
free(buffer);
|
||||
fclose(F1);
|
||||
}
|
||||
|
||||
void test_getline_while() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
|
||||
while ((read = getline(&line, &len, file)) != -1) {
|
||||
printf("%s\n", line);
|
||||
}
|
||||
|
||||
free(line);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void test_getline_return_check() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t r = getline(&line, &len, file);
|
||||
|
||||
if (r != -1) {
|
||||
if (line[0] == '\0') {} // ok
|
||||
}
|
||||
free(line);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void test_getline_clear_eof() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t n = 10;
|
||||
char *buffer = malloc(n);
|
||||
ssize_t read = fread(buffer, n, 1, file);
|
||||
if (feof(file)) {
|
||||
clearerr(file);
|
||||
getline(&buffer, &n, file); // ok
|
||||
}
|
||||
fclose(file);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_not_null(char **buffer, size_t *size) {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
getline(buffer, size, file);
|
||||
fclose(file);
|
||||
|
||||
if (size == NULL || buffer == NULL) {
|
||||
clang_analyzer_warnIfReached(); // must not happen
|
||||
}
|
||||
}
|
||||
|
||||
void test_getline_size_constraint(size_t size) {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t old_size = size;
|
||||
char *buffer = malloc(10);
|
||||
if (buffer != NULL) {
|
||||
ssize_t r = getline(&buffer, &size, file);
|
||||
if (r >= 0) {
|
||||
// Since buffer has a size of 10, old_size must be less than or equal to 10.
|
||||
// Otherwise, there would be UB.
|
||||
clang_analyzer_eval(old_size <= 10); // expected-warning{{TRUE}}
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void test_getline_negative_buffer() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
char *buffer = NULL;
|
||||
size_t n = -1;
|
||||
getline(&buffer, &n, file); // ok since posix 2018
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void test_getline_negative_buffer_2(char *buffer) {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t n = -1;
|
||||
(void)getline(&buffer, &n, file); // ok
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
// RUN: %clang_analyze_cc1 -triple=hexagon -analyzer-checker=core,alpha.unix.Stream,debug.ExprInspection -verify %s
|
||||
|
||||
#include "Inputs/system-header-simulator.h"
|
||||
#include "Inputs/system-header-simulator-for-malloc.h"
|
||||
#include "Inputs/system-header-simulator-for-valist.h"
|
||||
|
||||
void clang_analyzer_eval(int);
|
||||
@ -376,3 +377,75 @@ void fflush_on_open_failed_stream(void) {
|
||||
}
|
||||
fclose(F);
|
||||
}
|
||||
|
||||
void getline_null_file() {
|
||||
char *buffer = NULL;
|
||||
size_t n = 0;
|
||||
getline(&buffer, &n, NULL); // expected-warning {{Stream pointer might be NULL}}
|
||||
}
|
||||
|
||||
void getdelim_null_file() {
|
||||
char *buffer = NULL;
|
||||
size_t n = 0;
|
||||
getdelim(&buffer, &n, '\n', NULL); // expected-warning {{Stream pointer might be NULL}}
|
||||
}
|
||||
|
||||
void getline_buffer_on_error() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
if (getline(&line, &len, file) == -1) {
|
||||
if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
|
||||
} else {
|
||||
if (line[0] == '\0') {} // no warning
|
||||
}
|
||||
|
||||
free(line);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void getline_ret_value() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t n = 0;
|
||||
char *buffer = NULL;
|
||||
ssize_t r = getline(&buffer, &n, file);
|
||||
|
||||
if (r > -1) {
|
||||
// The return value does *not* include the terminating null byte.
|
||||
// The buffer must be large enough to include it.
|
||||
clang_analyzer_eval(n > r); // expected-warning{{TRUE}}
|
||||
clang_analyzer_eval(buffer != NULL); // expected-warning{{TRUE}}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
|
||||
void getline_buffer_size_negative() {
|
||||
FILE *file = fopen("file.txt", "r");
|
||||
if (file == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t n = -1;
|
||||
clang_analyzer_eval((ssize_t)n >= 0); // expected-warning{{FALSE}}
|
||||
char *buffer = NULL;
|
||||
ssize_t r = getline(&buffer, &n, file);
|
||||
|
||||
if (r > -1) {
|
||||
clang_analyzer_eval((ssize_t)n > r); // expected-warning{{TRUE}}
|
||||
clang_analyzer_eval(buffer != NULL); // expected-warning{{TRUE}}
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user