| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -66,7 +66,7 @@ abstract private class StdStringTaintFunction extends TaintFunction { | |||
| 66 | 66 | * Gets the index of a parameter to this function that is an iterator. | |
| 67 | 67 | */ | |
| 68 | 68 | final int getAnIteratorParameterIndex() { | |
| 69 | - this.getParameter(result).getType() instanceof Iterator | ||
| 69 | + this.getParameter(result).getUnspecifiedType() instanceof Iterator | ||
| 70 | 70 | } | |
| 71 | 71 | } | |
| 72 | 72 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -172,5 +172,5 @@ where | |||
| 172 | 172 | not arg.isFromUninstantiatedTemplate(_) and | |
| 173 | 173 | not actual.getUnspecifiedType() instanceof ErroneousType | |
| 174 | 174 | select arg, | |
| 175 | - "This argument should be of type '" + expected.getName() + "' but is of type '" + | ||
| 175 | + "This format specifier for type '" + expected.getName() + "' does not match the argument type '" + | ||
| 176 | 176 | actual.getUnspecifiedType().getName() + "'." | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,22 +5,23 @@ | |||
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | 7 | <overview> | |
| 8 | - <p>This rule finds return statements that return pointers to an object allocated on the stack. | ||
| 9 | - The lifetime of a stack allocated memory location only lasts until the function returns, and | ||
| 10 | - the contents of that memory become undefined after that. Clearly, using a pointer to stack | ||
| 8 | + <p>This rule finds return statements that return pointers to an object allocated on the stack. | ||
| 9 | + The lifetime of a stack allocated memory location only lasts until the function returns, and | ||
| 10 | + the contents of that memory become undefined after that. Clearly, using a pointer to stack | ||
| 11 | 11 | memory after the function has already returned will have undefined results. </p> | |
| 12 | 12 | ||
| 13 | 13 | </overview> | |
| 14 | 14 | <recommendation> | |
| 15 | - <p>Use the functions of the <tt>malloc</tt> family to dynamically allocate memory on the heap for data that is used across function calls.</p> | ||
| 15 | + <p>Use the functions of the <tt>malloc</tt> family, or <tt>new</tt>, to dynamically allocate memory on the heap for data that is used across function calls.</p> | ||
| 16 | 16 | ||
| 17 | 17 | </recommendation> | |
| 18 | - <example><sample src="ReturnStackAllocatedMemory.cpp" /> | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 18 | + <example> | ||
| 19 | + <p>The following example allocates an object on the stack and returns a pointer to it. This is incorrect because the object is deallocated | ||
| 20 | + when the function returns, and the pointer becomes invalid.</p> | ||
| 21 | + <sample src="ReturnStackAllocatedMemoryBad.cpp" /> | ||
| 23 | 22 | ||
| 23 | + <p>To fix this, allocate the object on the heap using <tt>new</tt> and return a pointer to the heap-allocated object.</p> | ||
| 24 | + <sample src="ReturnStackAllocatedMemoryGood.cpp" /> | ||
| 24 | 25 | </example> | |
| 25 | 26 | ||
| 26 | 27 | <references> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + Record *mkRecord(int value) { | ||
| 2 | + Record myRecord(value); | ||
| 3 | + | ||
| 4 | + return &myRecord; // BAD: returns a pointer to `myRecord`, which is a stack-allocated object. | ||
| 5 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + Record *mkRecord(int value) { | ||
| 2 | + Record *myRecord = new Record(value); | ||
| 3 | + | ||
| 4 | + return myRecord; // GOOD: returns a pointer to a `myRecord`, which is a heap-allocated object. | ||
| 5 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,14 @@ | |||
| 1 | - unsigned limit = get_limit(); | ||
| 2 | - unsigned total = 0; | ||
| 3 | - while (limit - total > 0) { // wrong: if `total` is greater than `limit` this will underflow and continue executing the loop. | ||
| 1 | + uint32_t limit = get_limit(); | ||
| 2 | + uint32_t total = 0; | ||
| 3 | + | ||
| 4 | + while (limit - total > 0) { // BAD: if `total` is greater than `limit` this will underflow and continue executing the loop. | ||
| 4 | 5 | total += get_data(); | |
| 5 | - } | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + while (total < limit) { // GOOD: never underflows here because there is no arithmetic. | ||
| 9 | + total += get_data(); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + while ((int64_t)limit - total > 0) { // GOOD: never underflows here because the result always fits in an `int64_t`. | ||
| 13 | + total += get_data(); | ||
| 14 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,17 @@ | |||
| 1 | 1 | char *file_name; | |
| 2 | 2 | FILE *f_ptr; | |
| 3 | - | ||
| 3 | + | ||
| 4 | 4 | /* Initialize file_name */ | |
| 5 | - | ||
| 5 | + | ||
| 6 | 6 | f_ptr = fopen(file_name, "w"); | |
| 7 | 7 | if (f_ptr == NULL) { | |
| 8 | 8 | /* Handle error */ | |
| 9 | 9 | } | |
| 10 | - | ||
| 10 | + | ||
| 11 | 11 | /* ... */ | |
| 12 | - | ||
| 12 | + | ||
| 13 | 13 | if (chmod(file_name, S_IRUSR) == -1) { | |
| 14 | 14 | /* Handle error */ | |
| 15 | - } | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + fclose(f_ptr); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,8 @@ | |||
| 1 | 1 | char *file_name; | |
| 2 | 2 | int fd; | |
| 3 | - | ||
| 3 | + | ||
| 4 | 4 | /* Initialize file_name */ | |
| 5 | - | ||
| 5 | + | ||
| 6 | 6 | fd = open( | |
| 7 | 7 | file_name, | |
| 8 | 8 | O_WRONLY | O_CREAT | O_EXCL, | |
@@ -11,9 +11,11 @@ fd = open( | |||
| 11 | 11 | if (fd == -1) { | |
| 12 | 12 | /* Handle error */ | |
| 13 | 13 | } | |
| 14 | - | ||
| 14 | + | ||
| 15 | 15 | /* ... */ | |
| 16 | - | ||
| 16 | + | ||
| 17 | 17 | if (fchmod(fd, S_IRUSR) == -1) { | |
| 18 | 18 | /* Handle error */ | |
| 19 | - } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + close(fd); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | * @name Iterator to expired container | |
| 3 | 3 | * @description Using an iterator owned by a container whose lifetime has expired may lead to unexpected behavior. | |
| 4 | 4 | * @kind problem | |
| 5 | - * @precision medium | ||
| 5 | + * @precision high | ||
| 6 | 6 | * @id cpp/iterator-to-expired-container | |
| 7 | 7 | * @problem.severity warning | |
| 8 | 8 | * @security-severity 8.8 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments