Skip to content

Commit d776d9f

Browse files
authored
Merge pull request #821 from geoffw0/query-tags-case
CPP: Improve ArrayArgSizeMismatch.ql
2 parents a5aee9e + bf7cdad commit d776d9f

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
|-----------------------------|-----------|--------------------------------------------------------------------|
1111
| Use of string copy function in a condition (`cpp/string-copy-return-value-as-boolean`) | correctness | This query identifies calls to string copy functions used in conditions, where it's likely that a different function was intended to be called. |
1212
| Lossy function result cast (`cpp/lossy-function-result-cast`) | correctness | Finds function calls whose result type is a floating point type, which are implicitly cast to an integral type. Newly available but not displayed by default on LGTM. |
13+
| Array argument size mismatch (`cpp/array-arg-size-mismatch`) | reliability | Finds function calls where the size of an array being passed is smaller than the array size of the declared parameter. Newly displayed on LGTM. |
1314

1415
## Changes to existing queries
1516

1617
| **Query** | **Expected impact** | **Change** |
1718
|----------------------------|------------------------|------------------------------------------------------------------|
19+
| Array argument size mismatch (`cpp/array-arg-size-mismatch`) | Fewer false positives | An exception has been added to this query for variable sized arrays. |
1820
| Suspicious add with sizeof (`cpp/suspicious-add-sizeof`) | Fewer false positives | Pointer arithmetic on `char * const` expressions (and other variations of `char *`) are now correctly excluded from the results. |
1921
| Suspicious pointer scaling (`cpp/suspicious-pointer-scaling`) | Fewer false positives | False positives involving types that are not uniquely named in the snapshot have been fixed. |
2022
| Call to memory access function may overflow buffer (`cpp/overflow-buffer`) | More correct results | Calls to `fread` are now examined by this query. |

cpp/ql/src/Likely Bugs/Conversion/ArrayArgSizeMismatch.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* @kind problem
66
* @id cpp/array-arg-size-mismatch
77
* @problem.severity warning
8+
* @precision high
89
* @tags reliability
910
*/
1011
import cpp
12+
import semmle.code.cpp.commons.Buffer
1113

1214
from Function f, FunctionCall c, int i, ArrayType argType, ArrayType paramType, int a, int b
1315
where f = c.getTarget() and
@@ -17,6 +19,7 @@ where f = c.getTarget() and
1719
b = paramType.getArraySize() and
1820
argType.getBaseType().getSize() = paramType.getBaseType().getSize() and
1921
a < b and
22+
not memberMayBeVarSize(_, c.getArgument(i).(VariableAccess).getTarget()) and
2023
// filter out results for inconsistent declarations
2124
strictcount(f.getParameter(i).getType().getSize()) = 1
2225
select c.getArgument(i), "Array of size " + a +
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:24:4:24:7 | arr3 | Array of size 3 passed to $@ which expects an array of size 4. | test.cpp:8:6:8:6 | g | g |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Conversion/ArrayArgSizeMismatch.ql
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
typedef unsigned long size_t;
3+
void *malloc(size_t size);
4+
5+
#define NUM (4)
6+
7+
void f(int *vs);
8+
void g(int vs[4]);
9+
void h(float fs[NUM]);
10+
11+
struct myStruct
12+
{
13+
unsigned int num;
14+
float data[0];
15+
};
16+
17+
void test(float f3[3], float f4[4], float f5[5], float *fp)
18+
{
19+
int arr3[3], arr4[4], arr5[5];
20+
21+
f(arr3); // GOOD
22+
f(arr4); // GOOD
23+
f(arr5); // GOOD
24+
g(arr3); // BAD
25+
g(arr4); // GOOD
26+
g(arr5); // GOOD
27+
28+
h(f3); // BAD [NOT DETECTED]
29+
h(f4); // GOOD
30+
h(f5); // GOOD
31+
h(fp); // GOOD
32+
33+
{
34+
// variable size struct
35+
myStruct *ms;
36+
37+
ms = (myStruct *)malloc(sizeof(myStruct) + (4 * sizeof(float)));
38+
ms->num = 4;
39+
ms->data[0] = ms->data[1] = ms->data[2] = ms->data[3] = 0;
40+
h(ms->data); // GOOD
41+
}
42+
43+
{
44+
// char array
45+
char ca[4 * sizeof(int)];
46+
47+
g((int *)ca); // GOOD
48+
}
49+
};

0 commit comments

Comments
 (0)