Skip to content

Use size_t for indices in summary - #1306

Merged
eivindjahren merged 8 commits into
mainfrom
size_t_summary
Oct 28, 2026
Merged

eivindjahren merged 8 commits into
mainfrom
size_t_summary

Conversation

@eivindjahren

Copy link
Copy Markdown
Collaborator

No description provided.

@eivindjahren
eivindjahren force-pushed the size_t_summary branch 2 times, most recently from fd13b5d to 7ab38d9 Compare October 26, 2026 15:19
@eivindjahren
eivindjahren requested a balanced review from Copilot October 26, 2026 15:20
@eivindjahren eivindjahren self-assigned this Oct 26, 2026
@eivindjahren eivindjahren moved this to Ready for Review in SCOUT Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unsigned report-step overflow can access invalid memory, and sparse report-only vectors retain artificial trailing entries.

2 open issues
What changed in this PR

Migrates summary indices and counts to size_t across the C++ implementation, public interfaces, and Python bindings.

Changes:

  • Replaces negative index sentinels with optional values while preserving Python’s -1 lookup results.
  • Improves handling of empty summaries, missing reports, and invalid indices.
  • Expands regression coverage for summary loading, writing, interpolation, and errors.
File Description
tests/​rd_tests/​test_sum.py Adds broad summary regression coverage.
tests/​rd_tests/​test_rd_sum.py Tests negative-index binding errors.
python/​resdata/​summary/​rd_sum_keyword_vector.py Rejects excessively negative indices.
lib/​tests/​test_time_index.cpp Removes signed report-step tests.
lib/​tests/​test_rd_sum.cpp Updates index types and optional-result tests.
lib/​tests/​test_fortio.cpp Removes negative-count test.
lib/​resdata/​well_rseg_loader.cpp Uses unsigned keyword index maps.
lib/​resdata/​smspec_node.cpp Migrates parameter index constructors.
lib/​resdata/​rd_unsmry_loader.cpp Migrates loader indices and counts.
lib/​resdata/​rd_sum.cpp Updates summary APIs and lookup results.
lib/​resdata/​rd_sum_vector.cpp Stores optional parameter indices.
lib/​resdata/​rd_sum_vector_pybind.cpp Accepts unsigned keyword indices.
lib/​resdata/​rd_sum_tstep.cpp Migrates timestep indices and write maps.
lib/​resdata/​rd_sum_pybind.cpp Updates bindings and preserves lookup sentinels.
lib/​resdata/​rd_sum_file_data.cpp Updates report handling and corrects first-day units.
lib/​resdata/​rd_sum_data.cpp Migrates case indexing and adds bounds checks.
lib/​resdata/​rd_smspec.cpp Updates parameter maps and size tracking.
lib/​resdata/​rd_kw.cpp Uses unsigned indexed-read arguments.
lib/​resdata/​rd_file_view.cpp Updates keyword and summary indices.
lib/​resdata/​FortIO.cpp Uses unsigned seek counts.
lib/​private-include/​detail/​resdata/​rd_unsmry_loader.hpp Updates loader declarations and storage.
lib/​private-include/​detail/​resdata/​rd_sum_file_data.hpp Introduces optional report ranges.
lib/​include/​resdata/​smspec_node.hpp Exposes unsigned parameter indices.
lib/​include/​resdata/​rd_sum.hpp Updates public summary signatures.
lib/​include/​resdata/​rd_sum_vector.hpp Updates keyword-vector signatures.
lib/​include/​resdata/​rd_sum_tstep.hpp Updates timestep access and writing signatures.
lib/​include/​resdata/​rd_sum_data.hpp Updates data APIs and optional lookups.
lib/​include/​resdata/​rd_smspec.hpp Updates metadata indices and mapping interfaces.
lib/​include/​resdata/​rd_kw.hpp Updates indexed-read declarations.
lib/​include/​resdata/​rd_file.hpp Updates summary-view index type.
lib/​include/​resdata/​rd_file_view.hpp Updates indexed-loading interfaces.
lib/​include/​resdata/​FortIO.hpp Updates seek argument declarations.
lib/​CMakeLists.txt Removes the deleted test source.

Review effort: Balanced

Comment thread lib/resdata/rd_sum_file_data.cpp Resolved
@eivindjahren
eivindjahren force-pushed the size_t_summary branch 2 times, most recently from 0b7b7bf to 9e54325 Compare October 27, 2026 06:40
@ajaust
ajaust self-requested a review October 27, 2026 07:56

@ajaust ajaust left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I have some smaller comments. 🙂

Comment thread tests/rd_tests/test_sum.py Resolved
Comment thread tests/rd_tests/test_sum.py Resolved
Comment thread lib/resdata/rd_sum_vector.cpp Resolved
Comment thread lib/resdata/rd_sum_file_data.cpp Resolved
Comment thread lib/resdata/rd_sum_data.cpp Resolved

@ajaust ajaust left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! One additional comment to raise awareness, but looks good overall. 👍

template <typename ReportNr = int>
std::filesystem::path filename(std::filesystem::path path, FileType,
bool fmt_file, int report_nr = -1);
bool fmt_file, ReportNr report_nr = -1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a slight risk that we get an underflow if we would call

filename<std::size_t>(path, type, fmt)

I don't think it is a huge risk now, but I wanted to mention it.

@eivindjahren eivindjahren Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, its an underflow which is then formatted as size_t_max so just a weird looking filename :D

@github-project-automation github-project-automation Bot moved this from Ready for Review to Reviewed in SCOUT Oct 28, 2026
Integers read from file are validated as non-negative before being
converted to size_t. fread_indexed_data now takes a vector<size_t>
index_map, and pybind summary methods take size_t so negative values
raise TypeError. The ministep number is purely informational and is
kept as the int read from file.

This also fixes some existing bugs:

- Looking up a report step by days or time no longer loops forever when
  a report step in the range is missing.
- Empty data in the summary index and file data (length, first_report,
  last_report, lookup_time, lookup_days) now throws instead of causing
  undefined behaviour.
- rd_sum_data_iget checks that params_index is in range and throws
  std::out_of_range instead of reading out of bounds.
- Report-only time and data vectors skip report steps missing from the
  data instead of reading the invalid range of a missing report, and
  are sized by the values actually written, so they no longer end with
  a spurious zero entry when report steps are missing. This replaces the
  report_only flag of the rd_sum_data allocators with separate report
  functions.
- Adding a time step with report step SIZE_MAX throws
  std::invalid_argument instead of wrapping report_step + 1 to zero and
  indexing outside the report map.
This would cause size_t underflows.
@eivindjahren
eivindjahren merged commit 61da652 into main Oct 28, 2026
12 checks passed
@eivindjahren
eivindjahren deleted the size_t_summary branch October 28, 2026 07:51
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in SCOUT Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants