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
