liveBook · Manning

Out-of-bounds array access

As explained in Modern C

Out-of-bounds array access is accessing an array element outside the range of elements belonging to that array.

Out-of-bounds array access means accessing an element outside the range belonging to its array. C often leaves verification to the programmer, so such access can contribute to crashes, data loss, incorrect results, sensitive-information exposure, or financial and physical harm. section 15.0

Make array expectations visible

C function declarations can express different expectations about an array argument: `void func(double a[static 1]);` indicates a non-null pointer to at least one element, while `void func(double a[static 7]);` expresses an expectation of at least seven elements. A size-dependent form is `void func(size_t n, double a[n]);`; the pointer form `void func(double *a);` communicates less about array requirements. section 15.0

TAKEAWAY

  • Do not trade off safety for performance. section 15.0
  • Treat array-bound verification as a programming responsibility when the language does not enforce it. section 15.0
void func(double a[static 1]);
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":0,\"ch\":10},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":19},{\"line\":0,\"ch\":25}]]"}
!@%STYLE%@!

Inspect the declaration that marks a single required array element with `static 1`. section 15.0

How Modern C covers it

How deep it goes
Defines the failure and consequences, then signals array-parameter notation without developing detection or prevention procedures.
Examples
Uses C declarations with `static 1`, `static 7`, variable-length array syntax, and pointer syntax to express array expectations.
What you can do after
Evaluate C array-parameter declarations for how clearly they communicate non-null and minimum-size expectations.
How it connects
Connects array bounds with uninitialized objects, object lifetimes, and integer overflow as C safety concerns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.