In this section:
This error is generated whenever an illegal value will be used to index an array that is being written. If this error can be detected during compilation, a compilation error will be issued instead of the normal runtime error.
|
This code attempts to access an illegal array element due to an incorrect loop range.
/* * File: writindx.c */ main() { int i, a[10]; for(i=1; i<=10; i++) a[i] = 0; return (0); } |
Other problems include writimd2.c
and writimd3.c
. A diagnosis similar to the one that follows applies to both.
[writindx.c:9] **WRITE_BAD_INDEX** >> a[i] = 0; Writing array out of range: a[i] ---- Associated Common Weakness Enumerations ---- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer CWE-124: Buffer Underwrite CWE-787: Out-of-bounds write CWE-786: Access of memory location before start of buffer CWE-786: Access of memory location after end of buffer Index used: 10 Valid range: 0 thru 9 (inclusive) Stack trace where the error occurred: main() writindx.c, 9 **Memory corrupted. Program may crash!!** |
This is normally a fatal error and is often introduced algorithmically. Other common sources include loops with incorrect initial or terminal conditions, as in this example, for which the corrected code is the following:
main() { int i, a[10]; for(i=; i<sizeof(a)/sizeof(a[0]); i++) a[i] = 0; return (0); } |
The table below shows Common Weakness Enumerations associated with this error.
CWE | Description |
---|---|
CWE-119 | Improper Restriction of Operations within the Bounds of a Memory Buffer |
CWE-124 | Buffer Underwrite |
CWE-787 | Out-of-bounds write |
CWE-786 | Access of memory location before start of buffer |
CWE-786 | Access of memory location after end of buffer |