Skip to content

Commit ae35ae1

Browse files
committed
C++: Fix readlink FPs.
1 parent c2ef58d commit ae35ae1

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

cpp/ql/src/Likely Bugs/Memory Management/ImproperNullTermination.ql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ class ImproperNullTerminationReachability extends StackVariableReachabilityWithR
2929
override predicate isSourceActual(ControlFlowNode node, StackVariable v) {
3030
node = declWithNoInit(v)
3131
or
32-
exists(Call c, int arg |
32+
exists(Call c, int bufferArg, int sizeArg |
3333
c = node and
3434
(
35-
c.getTarget().hasName("readlink") and arg = 1
35+
c.getTarget().hasName("readlink") and bufferArg = 1 and sizeArg = 2
3636
or
37-
c.getTarget().hasName("readlinkat") and arg = 2
37+
c.getTarget().hasName("readlinkat") and bufferArg = 2 and sizeArg = 3
3838
) and
39-
c.getArgument(arg).(VariableAccess).getTarget() = v
39+
c.getArgument(bufferArg).(VariableAccess).getTarget() = v and
40+
(
41+
// buffer size parameter likely matches the full buffer size
42+
c.getArgument(sizeArg) instanceof SizeofOperator or
43+
c.getArgument(sizeArg).getValue().toInt() = v.getType().getSize()
44+
)
4045
)
4146
}
4247

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/ImproperNullTermination.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@
1111
| test.cpp:130:14:130:19 | buffer | Variable $@ may not be null terminated. | test.cpp:127:7:127:12 | buffer | buffer |
1212
| test.cpp:139:10:139:15 | buffer | Variable $@ may not be null terminated. | test.cpp:136:8:136:13 | buffer | buffer |
1313
| test.cpp:147:14:147:19 | buffer | Variable $@ may not be null terminated. | test.cpp:143:8:143:13 | buffer | buffer |
14-
| test.cpp:154:10:154:15 | buffer | Variable $@ may not be null terminated. | test.cpp:151:8:151:13 | buffer | buffer |
15-
| test.cpp:162:10:162:15 | buffer | Variable $@ may not be null terminated. | test.cpp:158:8:158:13 | buffer | buffer |
1614
| test.cpp:170:10:170:15 | buffer | Variable $@ may not be null terminated. | test.cpp:166:8:166:13 | buffer | buffer |
17-
| test.cpp:186:10:186:15 | buffer | Variable $@ may not be null terminated. | test.cpp:183:9:183:14 | buffer | buffer |
18-
| test.cpp:194:10:194:15 | buffer | Variable $@ may not be null terminated. | test.cpp:190:9:190:14 | buffer | buffer |
19-
| test.cpp:201:10:201:15 | buffer | Variable $@ may not be null terminated. | test.cpp:198:9:198:14 | buffer | buffer |
20-
| test.cpp:209:10:209:15 | buffer | Variable $@ may not be null terminated. | test.cpp:205:9:205:14 | buffer | buffer |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ void test_readlink(int fd, const char *path, size_t sz)
151151
char buffer[1024] = {0};
152152

153153
readlink(path, buffer, sizeof(buffer) - 1);
154-
strdup(buffer); // GOOD [FALSE POSITIVE]
154+
strdup(buffer); // GOOD
155155
}
156156

157157
{
158158
char buffer[1024];
159159

160160
memset(buffer, 0, sizeof(buffer));
161161
readlink(path, buffer, sizeof(buffer) - 1);
162-
strdup(buffer); // GOOD [FALSE POSITIVE]
162+
strdup(buffer); // GOOD
163163
}
164164

165165
{
@@ -183,29 +183,29 @@ void test_readlink(int fd, const char *path, size_t sz)
183183
char *buffer = (char *)malloc(1024);
184184

185185
readlink(path, buffer, 1024);
186-
strdup(buffer); // BAD
186+
strdup(buffer); // BAD [NOT DETECTED]
187187
}
188188

189189
{
190190
char *buffer = (char *)malloc(1024);
191191

192192
buffer[1023] = 0;
193193
readlink(path, buffer, 1023);
194-
strdup(buffer); // GOOD [FALSE POSITIVE]
194+
strdup(buffer); // GOOD
195195
}
196196

197197
{
198198
char *buffer = (char *)malloc(sz);
199199

200200
readlink(path, buffer, sz);
201-
strdup(buffer); // BAD
201+
strdup(buffer); // BAD [NOT DETECTED]
202202
}
203203

204204
{
205205
char *buffer = (char *)malloc(sz);
206206

207207
memset(buffer, 0, sz);
208208
readlink(path, buffer, sz - 1);
209-
strdup(buffer); // GOOD [FALSE POSITIVE]
209+
strdup(buffer); // GOOD
210210
}
211211
}

0 commit comments

Comments
 (0)