Skip to content

Commit 0c0720a

Browse files
committed
C++: Improve use-after-free example code
* Remove the mismatch between `new` and `free` and use `delete` instead * Make the function `void`, so people copying the code will not forget to add a `return`. * Balance out the `...` for omitted code.
1 parent 5950865 commit 0c0720a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cpp/ql/src/Critical/UseAfterFree.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
int f() {
1+
void f() {
22
char* buf = new char[SIZE];
3-
....
3+
...
44
if (error) {
5-
free(buf); //error handling has freed the buffer
5+
delete buf; //error handling has freed the buffer
66
}
77
...
88
log_contents(buf); //but it is still used here for logging
9+
...
910
}

0 commit comments

Comments
 (0)