liveBook · Manning

Safe-programming discipline

As explained in Memory Safe C++

Safe-programming discipline is the consistent application of safe practices, including under schedule pressure.

A sensor label can fill a fixed-size buffer without overflowing it, yet leave no null terminator for the logger. Preventing such defects requires safe-programming discipline: applying safe practices consistently, even when deadlines make shortcuts tempting. 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
}

A 32-character label fills every byte of the value-initialized buffer. The copy is legal, but LogReading then walks past the buffer looking for a null terminator. section 1.7

Discipline is one foundation of memory safety, alongside knowledge of hazards and a culture that values safety. Automated checks carry part of the load because they do not depend on someone remembering a manual check under pressure. A systematic process starts with design choices and ends with review. section 1.7

How Memory Safe C++ covers it

How deep it goes
Explains the discipline’s rationale and places it in a process from design choice to review, without detailing intermediate stages.
Examples
Uses a patient-monitoring sensor label that fills a 32-byte buffer and leaves a logger without a null terminator.
What you can do after
Evaluate/understand how consistent practices, automated checks, and shared culture support memory safety.
How it connects
Connects discipline to memory safety, safety culture, automated checks, and code review.

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.