`MPSStateSpace::Sample` accesses results out of bounds when the output vector is empty

Đang mở Phù hợp với người mới
#1,126 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@aadityarshah đang làm issue này rồi.

Từ ngày 12/10/2026.

  • #1127 của @aadityarshah — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu tại lib/mps_statespace.h, ở MPSStateSpace::Sample(), và kiểm tra cách results được chuẩn bị trước khi truy cập theo chỉ số trong vòng lặp lấy mẫu. Báo cáo tái hiện vấn đề với một vector đầu ra rỗng và một mẫu; hãy so sánh với các bài kiểm thử lấy mẫu hiện có, vốn định kích thước trước cho vector đó. Công việc hoàn tất khi việc lấy mẫu với vector ban đầu rỗng tránh được truy cập ngoài phạm vi và các bài kiểm thử liên quan bao quát trường hợp này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the issue

While investigating the implementation of lib/mps_statespace.h, I found that MPSStateSpace::Sample() may access elements beyond the size of the output vector.

The current implementation calls

results->reserve(num_samples);
for (unsigned i = 0; i < num_samples; i++) {
  SampleOnce(state, scratch, scratch2, &rand_source, &(*results)[i]);
}

std::vector::reserve() increases capacity without changing the vector's size. If the caller provides an initially empty results vector, the indexed access is out of bounds resulting in undefined behavior.

Tell us how to reproduce the issue

To reproduce the issue, following steps should be followed:

  1. Create an MPS state and the two required scratch states.
  2. Initialize the state to |000>.
  3. Create an empty std::vector<std::vector<bool>>.
  4. Call Sample() with num_samples = 1

A minimal example would be as follows:

auto ss = qsim::mps::MPSStateSpace<qsim::For, float>(1);
auto state = ss.Create(3, 4);
auto scratch = ss.Create(3, 4);
auto scratch2 = ss.Create(3, 4);

ss.SetStateZero(state);

std::vector<std::vector<bool>> results;
ss.Sample(state, scratch, scratch2, 1, 1234, &results);

When compiled with GCC 15.2.0 using the flag -D_GLIBCXX_DEBUG , the program aborts giving the following diagnostic

Error: attempt to subscript container with out-of-bounds index 0, but 
container only holds 0 elements.

The existing sampling tests pre-size the outer results vector before calling Sample() so they do not cover this case.

Tell us the version of qsim or qsimcirq (if relevant)

No response

Tell us the computing environment (if relevant)

Ubuntu 26.04.1 LTS, GCC 15.2.0, C++17.

Ngôn ngữ chính
C++
Star
702
Fork
219
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
8

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của quantumlib/qsim

Tất cả issue của quantumlib/qsim

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.