Skip to content

Commit 01661b9

Browse files
committed
FIO34-C: qcc support
library can access `stdin` or `_Stdin` by reference
1 parent c5e6c00 commit 01661b9

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

cpp/common/src/codingstandards/cpp/ReadErrorsAndEOF.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import codingstandards.cpp.standardlibrary.FileAccess
55
/**
66
* any call to function `feof()` or `ferror()`
77
*/
8-
abstract class FeofFerrorCall extends FunctionCall { }
8+
abstract class FeofFerrorCall extends FileAccess {
9+
override VariableAccess getFileExpr() {
10+
result = [this.getArgument(0), this.getArgument(0).(AddressOfExpr).getAnOperand()]
11+
}
12+
}
913

1014
class FeofCall extends FeofFerrorCall {
1115
FeofCall() { this.getTarget().hasGlobalName("feof") }
@@ -15,11 +19,6 @@ class FerrorCall extends FeofFerrorCall {
1519
FerrorCall() { this.getTarget().hasGlobalName("ferror") }
1620
}
1721

18-
pragma[inline]
19-
predicate accessSameTarget(VariableAccess va1, VariableAccess va2) {
20-
va1.getTarget() = va2.getTarget()
21-
}
22-
2322
predicate isShortCircuitedEdge(ControlFlowNode fst, ControlFlowNode snd) {
2423
fst = any(LogicalAndExpr andOp).getLeftOperand() and snd = fst.getAFalseSuccessor()
2524
or
@@ -36,7 +35,7 @@ ControlFlowNode feofUnchecked(InBandErrorReadFunctionCall read) {
3635
not isShortCircuitedEdge(mid, result) and
3736
result = mid.getASuccessor() and
3837
//Stop recursion on call to feof/ferror on the correct file
39-
not accessSameTarget(result.(FeofCall).getArgument(0), read.getFileExpr())
38+
not sameFileSource(result.(FeofCall), read)
4039
)
4140
}
4241

@@ -50,7 +49,7 @@ ControlFlowNode ferrorUnchecked(InBandErrorReadFunctionCall read) {
5049
not isShortCircuitedEdge(mid, result) and
5150
result = mid.getASuccessor() and
5251
//Stop recursion on call to ferror on the correct file
53-
not accessSameTarget(result.(FerrorCall).getArgument(0), read.getFileExpr())
52+
not sameFileSource(result.(FerrorCall), read)
5453
)
5554
}
5655

@@ -112,6 +111,6 @@ predicate missingEOFWEOFChecks(InBandErrorReadFunctionCall read) {
112111
// another char is read before the comparison to EOF
113112
exists(FileReadFunctionCall fc |
114113
macroUnchecked(read) = fc and
115-
accessSameTarget(read.getFileExpr(), fc.getFileExpr())
114+
sameFileSource(read, fc)
116115
)
117116
}

cpp/common/src/codingstandards/cpp/standardlibrary/FileAccess.qll

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class FOpenCall extends FunctionCall {
9898
}
9999

100100
abstract class FileAccess extends FunctionCall {
101-
abstract Expr getFileExpr();
101+
abstract VariableAccess getFileExpr();
102102
}
103103

104104
pragma[inline]
@@ -114,17 +114,17 @@ class ImplicitFileAccess extends FileAccess {
114114
string fileName;
115115

116116
ImplicitFileAccess() {
117-
fileName = "stdin" and
117+
fileName = ["stdin", "_Stdin"] and
118118
this.getTarget().hasGlobalName(["getchar", "getwchar", "scanf", "scanf_s"])
119119
or
120-
fileName = "stdout" and
120+
fileName = ["stdout", "_Stdout"] and
121121
this.getTarget().hasGlobalName(["printf", "printf_s", "puts", "putchar", "putwchar"])
122122
or
123-
fileName = "stderr" and this.getTarget().hasGlobalName("perror")
123+
fileName = ["stderr", "_Stderr"] and this.getTarget().hasGlobalName("perror")
124124
}
125125

126126
/** The expression corresponding to the accessed file */
127-
override Expr getFileExpr() {
127+
override VariableAccess getFileExpr() {
128128
fileName = result.(VariableAccess).getTarget().(GlobalVariable).toString() or
129129
fileName = result.findRootCause().(Macro).getName()
130130
}
@@ -141,10 +141,10 @@ class InBandErrorReadFunctionCall extends FileAccess {
141141
}
142142

143143
/** The expression corresponding to the accessed file */
144-
override Expr getFileExpr() {
144+
override VariableAccess getFileExpr() {
145145
if this instanceof ImplicitFileAccess
146146
then result = this.(ImplicitFileAccess).getFileExpr()
147-
else result = this.getArgument(0)
147+
else result = [this.getArgument(0), this.getArgument(0).(AddressOfExpr).getAnOperand()]
148148
}
149149
}
150150

@@ -167,10 +167,11 @@ class FileReadFunctionCall extends FileAccess {
167167
}
168168

169169
/** The expression corresponding to the accessed file */
170-
override Expr getFileExpr() {
170+
override VariableAccess getFileExpr() {
171171
if this instanceof ImplicitFileAccess
172172
then result = this.(ImplicitFileAccess).getFileExpr()
173-
else result = this.getArgument(filePos)
173+
else
174+
result = [this.getArgument(filePos), this.getArgument(filePos).(AddressOfExpr).getAnOperand()]
174175
}
175176
}
176177

@@ -195,10 +196,11 @@ class FileWriteFunctionCall extends FileAccess {
195196
}
196197

197198
/** The expression corresponding to the accessed file */
198-
override Expr getFileExpr() {
199+
override VariableAccess getFileExpr() {
199200
if this instanceof ImplicitFileAccess
200201
then result = this.(ImplicitFileAccess).getFileExpr()
201-
else result = this.getArgument(filePos)
202+
else
203+
result = [this.getArgument(filePos), this.getArgument(filePos).(AddressOfExpr).getAnOperand()]
202204
}
203205
}
204206

@@ -209,7 +211,9 @@ class FileCloseFunctionCall extends FileAccess {
209211
FileCloseFunctionCall() { this.getTarget().hasGlobalName("fclose") }
210212

211213
/** The expression corresponding to the accessed file */
212-
override Expr getFileExpr() { result = this.getArgument(0) }
214+
override VariableAccess getFileExpr() {
215+
result = [this.getArgument(0), this.getArgument(0).(AddressOfExpr).getAnOperand()]
216+
}
213217
}
214218

215219
/**
@@ -221,5 +225,7 @@ class FilePositioningFunctionCall extends FileAccess {
221225
}
222226

223227
/** The expression corresponding to the accessed file */
224-
override Expr getFileExpr() { result = this.getArgument(0) }
228+
override VariableAccess getFileExpr() {
229+
result = [this.getArgument(0), this.getArgument(0).(AddressOfExpr).getAnOperand()]
230+
}
225231
}

0 commit comments

Comments
 (0)