[clang] [clang][analyzer] Add C standard streams to the internal memory space (PR #147766)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 08:55:35 PDT 2025
================
@@ -1054,10 +1054,24 @@ const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
assert(!Ty.isNull());
if (Ty.isConstQualified()) {
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
- } else if (Ctx.getSourceManager().isInSystemHeader(D->getLocation())) {
- sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
} else {
- sReg = getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind);
+ StringRef N = D->getNameAsString();
+ QualType FILETy = D->getASTContext().getFILEType();
+ if (!FILETy.isNull())
+ FILETy = FILETy.getCanonicalType();
+ Ty = Ty.getCanonicalType();
+ bool IsStdStreamVar = Ty->isPointerType() &&
+ Ty->getPointeeType() == FILETy &&
+ (N == "stdin" || N == "stdout" || N == "stderr");
+ // Pointer value of C standard streams is usually not modified by system
+ // calls. This means they should not get invalidated at system calls and
+ // can not belong to the system memory space.
----------------
NagyDonat wrote:
```suggestion
// Pointer value of C standard streams is usually not modified by calls
// to functions declared in system headers. This means that they should
// not get invalidated by calls to functions declared in system headers,
// so they are placed in the global internal space, which is not
// invalidated by calls to functions declared in system headers.
```
I would prefer avoiding the phrase "system call" because it has a [very specific meaning](https://en.wikipedia.org/wiki/System_call) which is not what you speak about here.
Also, I extended the comment to describe the reason why is the global internal space a "better" place for these streams, because I felt that this makes it easier to quickly understand the goals of this logic.
https://github.com/llvm/llvm-project/pull/147766
More information about the cfe-commits
mailing list