Skip to content

Commit e4b9d31

Browse files
authored
Merge pull request #194 from raulgarciamsft/overflow_buffer_negindex
Detect access to an array using a negative index
2 parents a3562aa + 28050e1 commit e4b9d31

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
# qltest projects and artifacts
99
*/ql/test/**/*.testproj
1010
*/ql/test/**/*.actual
11+
/.vs/slnx.sqlite
12+
/.vs/ql/v15/Browse.VC.opendb
13+
/.vs/ql/v15/Browse.VC.db
14+
/.vs/ProjectSettings.json

cpp/ql/src/Security/CWE/CWE-119/OverflowBuffer.ql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from BufferAccess ba, string bufferDesc, int accessSize, int accessType,
2929
where accessSize = ba.getSize()
3030
and bufferSize = getBufferSize(ba.getBuffer(bufferDesc, accessType),
3131
bufferAlloc)
32-
and accessSize > bufferSize
32+
and (accessSize > bufferSize or (accessSize <= 0 and accessType = 3))
3333
and if accessType = 1 then (
3434
message = "This '" + ba.getName() + "' operation accesses "
3535
+ plural(accessSize, " byte", " bytes")
@@ -41,8 +41,13 @@ where accessSize = ba.getSize()
4141
+ " but the $@ is only "
4242
+ plural(bufferSize, " byte", " bytes") + "."
4343
) else (
44-
message = "This array indexing operation accesses byte offset "
45-
+ (accessSize - 1) + " but the $@ is only "
46-
+ plural(bufferSize, " byte", " bytes") + "."
44+
if accessSize > 0 then (
45+
message = "This array indexing operation accesses byte offset "
46+
+ (accessSize - 1) + " but the $@ is only "
47+
+ plural(bufferSize, " byte", " bytes") + "."
48+
) else (
49+
message = "This array indexing operation accesses a negative index "
50+
+ ((accessSize/ba.getActualType().getSize()) - 1) + " on the $@."
51+
)
4752
)
4853
select ba, message, bufferAlloc, bufferDesc

0 commit comments

Comments
 (0)