A sensor label that fills a 32-byte buffer can leave no terminating zero, causing a later logging operation to read past the buffer. Static analysis helps identify such risks before the code runs. section 1.7
Static analysis examines code without executing it. It can enforce rules about constructs as written, such as bans on recursion or raw new and delete. Those checks are deterministic: the analyzer either finds the construct or it does not. section 1.7
Reasoning about values is less certain because an analyzer may not see all the data involved. When it cannot establish a copy’s bound, it may report a possible overflow rather than a certain defect. A project’s ruleset and release policy determine whether that finding blocks a build; the team must also manage the resulting noise. section 1.7
An unbounded sensor-label copy
A sensor-reading function copies a label into a 32-byte array without checking its length. A safety-critical ruleset can flag the std::copy because its bound is not established at the call site. A label of exactly 32 bytes fits the array but leaves no terminating zero; a longer label can overflow it. Validate that the label is shorter than 32 bytes, or limit the copy to 31 bytes so the terminator survives. section 1.7
Listing 1.1 Sensor label copy without bounds check
void ProcessSensorReading(std::span<const std::uint8_t> raw_data) {
constexpr std::size_t kMaxLabelLength = 32; #A
std::array<char, kMaxLabelLength> sensor_label{}; #B
auto reading = ParseSensorData(raw_data);
std::copy(reading.label.begin(), #C
reading.label.end(),
sensor_label.begin());
LogReading(sensor_label.data(), reading.value); #D
}
Inspect the unchecked copy and the subsequent logging call in the sensor-reading example. section 1.7
Static analysis is one part of a memory-safety process, alongside safe design patterns, dynamic analysis, and code review. Dynamic analysis checks behavior while tests run; for example, AddressSanitizer can report the label defect if a test exercises the boundary. section 1.10 section 1.7










