...
| Code Block | ||
|---|---|---|
| ||
void clear(int array[]) {
size_t i;
for (i = 0; i < sizeof(array) / sizeof(array[0]); ++i) {
array[i] = 0;
}
}
/* ... */
void dowork(void) {
int dis[12];
clear(dis);
/* ... */
}
|
| Wiki Markup |
|---|
The footnote in Section 6.5.3.4 of the C Standard \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] explains: |
...
| Code Block | ||
|---|---|---|
| ||
void clear(int array[], size_t size) {
size_t i;
for (i = 0; i < size; i++) {
array[i] = 0;
}
}
/* ... */
void dowork(void) {
int dis[12];
clear(dis, sizeof(dis) / sizeof(dis[0]));
/* ... */
}
|
| Wiki Markup |
|---|
This {{sizeof(array) / sizeof(array\[0\])}} idiom will succeed provided the original definition of {{array}} is visible. |
Risk Assessment
Incorrectly using the sizeof operator to determine the size of an array can result in a buffer overflow, allowing the execution of arbitrary code.
...
The LDRA tool suite V 7.6.0 is able to detect violations of this recommendation.
Compass Rose /ROSE can detect violations of the recommendation, but it cannot distinguish between incomplete array declarations and pointer declarations.
...