@@ -1392,6 +1392,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
1392
1392
SValBuilder &SVB = C.getSValBuilder ();
1393
1393
BasicValueFactory &BVF = SVB.getBasicValueFactory ();
1394
1394
const ASTContext &ACtx = BVF.getContext ();
1395
+ Preprocessor &PP = C.getPreprocessor ();
1395
1396
1396
1397
// Helper class to lookup a type by its name.
1397
1398
class LookupType {
@@ -1527,14 +1528,11 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
1527
1528
const RangeInt UCharRangeMax =
1528
1529
std::min (BVF.getMaxValue (ACtx.UnsignedCharTy ).getLimitedValue (), IntMax);
1529
1530
1530
- // The platform dependent value of EOF.
1531
- // Try our best to parse this from the Preprocessor, otherwise fallback to -1.
1532
- const auto EOFv = [&C]() -> RangeInt {
1533
- if (const std::optional<int > OptInt =
1534
- tryExpandAsInteger (" EOF" , C.getPreprocessor ()))
1535
- return *OptInt;
1536
- return -1 ;
1537
- }();
1531
+ // Get platform dependent values of some macros.
1532
+ // Try our best to parse this from the Preprocessor, otherwise fallback to a
1533
+ // default value (what is found in a library header).
1534
+ const auto EOFv = tryExpandAsInteger (" EOF" , PP).value_or (-1 );
1535
+ const auto AT_FDCWDv = tryExpandAsInteger (" AT_FDCWD" , PP).value_or (-100 );
1538
1536
1539
1537
// Auxiliary class to aid adding summaries to the summary map.
1540
1538
struct AddToFunctionSummaryMap {
@@ -1979,6 +1977,12 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
1979
1977
ConstraintSet{ReturnValueCondition (WithinRange, Range (-1 , IntMax))};
1980
1978
const auto &ReturnsValidFileDescriptor = ReturnsNonnegative;
1981
1979
1980
+ auto ValidFileDescriptorOrAtFdcwd = [&](ArgNo ArgN) {
1981
+ return std::make_shared<RangeConstraint>(
1982
+ ArgN, WithinRange, Range ({AT_FDCWDv, AT_FDCWDv}, {0 , IntMax}),
1983
+ " a valid file descriptor or AT_FDCWD" );
1984
+ };
1985
+
1982
1986
// FILE *fopen(const char *restrict pathname, const char *restrict mode);
1983
1987
addToFunctionSummaryMap (
1984
1988
" fopen" ,
@@ -2130,6 +2134,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2130
2134
Summary (NoEvalCall)
2131
2135
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2132
2136
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2137
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd (ArgNo (0 )))
2133
2138
.ArgConstraint (NotNull (ArgNo (1 ))));
2134
2139
2135
2140
// int dup(int fildes);
@@ -2207,7 +2212,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2207
2212
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2208
2213
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2209
2214
.ArgConstraint (NotNull (ArgNo (0 )))
2210
- .ArgConstraint (ArgumentCondition ( 1 , WithinRange, Range ( 0 , IntMax )))
2215
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 1 )))
2211
2216
.ArgConstraint (NotNull (ArgNo (2 ))));
2212
2217
2213
2218
// int lockf(int fd, int cmd, off_t len);
@@ -2318,6 +2323,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2318
2323
Summary (NoEvalCall)
2319
2324
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2320
2325
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2326
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd (ArgNo (0 )))
2321
2327
.ArgConstraint (NotNull (ArgNo (1 ))));
2322
2328
2323
2329
std::optional<QualType> Dev_tTy = lookupTy (" dev_t" );
@@ -2339,6 +2345,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2339
2345
Summary (NoEvalCall)
2340
2346
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2341
2347
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2348
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd (ArgNo (0 )))
2342
2349
.ArgConstraint (NotNull (ArgNo (1 ))));
2343
2350
2344
2351
// int chmod(const char *path, mode_t mode);
@@ -2357,7 +2364,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2357
2364
Summary (NoEvalCall)
2358
2365
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2359
2366
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2360
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2367
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2361
2368
.ArgConstraint (NotNull (ArgNo (1 ))));
2362
2369
2363
2370
// int fchmod(int fildes, mode_t mode);
@@ -2381,7 +2388,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2381
2388
Summary (NoEvalCall)
2382
2389
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2383
2390
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2384
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2391
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2385
2392
.ArgConstraint (NotNull (ArgNo (1 ))));
2386
2393
2387
2394
// int chown(const char *path, uid_t owner, gid_t group);
@@ -2446,9 +2453,9 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2446
2453
Summary (NoEvalCall)
2447
2454
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2448
2455
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2449
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2456
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2450
2457
.ArgConstraint (NotNull (ArgNo (1 )))
2451
- .ArgConstraint (ArgumentCondition ( 2 , WithinRange, Range ( 0 , IntMax )))
2458
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 2 )))
2452
2459
.ArgConstraint (NotNull (ArgNo (3 ))));
2453
2460
2454
2461
// int unlink(const char *pathname);
@@ -2466,7 +2473,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2466
2473
Summary (NoEvalCall)
2467
2474
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2468
2475
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2469
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2476
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2470
2477
.ArgConstraint (NotNull (ArgNo (1 ))));
2471
2478
2472
2479
std::optional<QualType> StructStatTy = lookupTy (" stat" );
@@ -2515,7 +2522,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2515
2522
Summary (NoEvalCall)
2516
2523
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2517
2524
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2518
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2525
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2519
2526
.ArgConstraint (NotNull (ArgNo (1 )))
2520
2527
.ArgConstraint (NotNull (ArgNo (2 ))));
2521
2528
@@ -2690,7 +2697,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2690
2697
ReturnValueCondition (WithinRange, Range (0 , Ssize_tMax))},
2691
2698
ErrnoMustNotBeChecked)
2692
2699
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2693
- .ArgConstraint (ArgumentCondition ( 0 , WithinRange, Range ( 0 , IntMax )))
2700
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd ( ArgNo ( 0 )))
2694
2701
.ArgConstraint (NotNull (ArgNo (1 )))
2695
2702
.ArgConstraint (NotNull (ArgNo (2 )))
2696
2703
.ArgConstraint (BufferSize (/* Buffer=*/ ArgNo (2 ),
@@ -2707,7 +2714,9 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
2707
2714
Summary (NoEvalCall)
2708
2715
.Case (ReturnsZero, ErrnoMustNotBeChecked)
2709
2716
.Case (ReturnsMinusOne, ErrnoNEZeroIrrelevant)
2717
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd (ArgNo (0 )))
2710
2718
.ArgConstraint (NotNull (ArgNo (1 )))
2719
+ .ArgConstraint (ValidFileDescriptorOrAtFdcwd (ArgNo (2 )))
2711
2720
.ArgConstraint (NotNull (ArgNo (3 ))));
2712
2721
2713
2722
// char *realpath(const char *restrict file_name,
0 commit comments