Skip to content

Commit 6566983

Browse files
authored
Catch invalid symptom in decode_to_errors to avoid segfault (#215)
Put the check in the init loop to avoid a separate loop over the detections.
1 parent f60aecb commit 6566983

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/tesseract.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ void TesseractDecoder::decode_to_errors(const std::vector<uint64_t>& detections,
292292
std::vector<DetectorCostTuple> initial_detector_cost_tuples(num_errors);
293293

294294
for (size_t d : detections) {
295+
if (d >= num_detectors) {
296+
throw std::runtime_error(
297+
"Symptom " + std::to_string(d) +
298+
" references a detector >= num_detectors (= " + std::to_string(num_detectors) + ").");
299+
}
295300
initial_detectors[d] = true;
296301
for (int ei : d2e[d]) {
297302
++initial_detector_cost_tuples[ei].detectors_count;

src/tesseract.test.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,29 @@ TEST(tesseract, EneighborsCorrectness_ComplexGrid) {
369369
EXPECT_EQ(t_dec.get_eneighbors()[6], expected_e6_neighbors);
370370
EXPECT_EQ(t_dec.get_eneighbors()[7], expected_e7_neighbors);
371371
}
372+
373+
TEST(tesseract, DecodeToErrorsThrowsOnInvalidSymptom) {
374+
stim::DetectorErrorModel dem(R"DEM(
375+
error(0.1) D0 D1
376+
error(0.1) D1 D2
377+
error(0.1) D2 D3
378+
detector(0, 0, 0) D0
379+
detector(1, 0, 0) D1
380+
detector(2, 0, 0) D2
381+
detector(2, 0, 0) D2
382+
)DEM");
383+
384+
TesseractConfig config(dem);
385+
TesseractDecoder decoder(config);
386+
387+
uint64_t invalid_symptom = decoder.num_detectors;
388+
389+
try {
390+
decoder.decode_to_errors({invalid_symptom});
391+
} catch (const std::runtime_error& err) {
392+
EXPECT_EQ("Symptom " + std::to_string(invalid_symptom) +
393+
" references a detector >= num_detectors (= " +
394+
std::to_string(decoder.num_detectors) + ").",
395+
err.what());
396+
}
397+
}

0 commit comments

Comments
 (0)