liveBook · Manning

Static analysis

As explained in Memory Safe C++

Static analysis examines software without executing it to identify properties, defects, or rule violations.

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

How Memory Safe C++ covers it

How deep it goes
Distinguishes deterministic rule checks from approximate value reasoning and explains how rulesets and release policies affect findings.
Examples
Uses an unchecked sensor-label copy into a 32-byte array to illustrate possible overflow and missing termination.
What you can do after
Evaluate potential analyzer findings and decide whether they should block a release under project policy.
How it connects
Connects static analysis with memory safety, dynamic analysis, safe design patterns, and code review.

Also explained in

Compare how 11 books explain static analysis →

Related concepts

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