Skip to content

Fix validation of record_size in fortio_fread_buffer#1185

Merged
eivindjahren merged 3 commits into
mainfrom
fix_fortio_fread_buffer
Jun 10, 2026
Merged

Fix validation of record_size in fortio_fread_buffer#1185
eivindjahren merged 3 commits into
mainfrom
fix_fortio_fread_buffer

Conversation

@eivindjahren

Copy link
Copy Markdown
Collaborator

No description provided.

@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch from 9fdbec8 to 156fb0b Compare June 10, 2026 06:56
@eivindjahren eivindjahren requested review from ajaust and Copilot June 10, 2026 07:15
@eivindjahren eivindjahren self-assigned this Jun 10, 2026
@eivindjahren eivindjahren moved this to Ready for Review in SCOUT Jun 10, 2026
@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch from 4311213 to 413e90c Compare June 10, 2026 07:19

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.

Pull request overview

This pull request updates the low-level Fortran unformatted read path to validate record sizes when reading into a fixed-size buffer, adds regression tests for malformed keyword records, and adjusts Windows wheel repair configuration.

Changes:

  • Refactors fortio_fread_buffer() to validate record sizes against the destination buffer and return false on inconsistencies instead of aborting.
  • Adds Python tests covering malformed/unexpected record-size scenarios when reading keywords via ResdataKW.fread().
  • Adds a cibuildwheel Windows repair-wheel-command using delvewheel.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
tests/rd_tests/test_rd_kw.py Adds regression tests for malformed/unexpected Fortran record markers and multi-record keyword reads.
pyproject.toml Configures cibuildwheel Windows wheel repair using delvewheel.
lib/resdata/FortIO.cpp Refactors buffered record reading to validate record sizes and avoid overreads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/rd_tests/test_rd_kw.py Outdated
Comment thread tests/rd_tests/test_rd_kw.py Outdated
Comment thread lib/resdata/FortIO.cpp
Comment on lines +503 to +518
char *end = buffer + buffer_size;
char *itr = buffer;
for (int record_size = 0; itr < end; itr += record_size) {
record_size = fortio_init_read(fortio);
if (record_size < 0)
return false;
if (end - itr < static_cast<ptrdiff_t>(record_size))
return false;
size_t items_read = 0;
if (record_size > 0)
items_read = fread(itr, 1, record_size, fortio->stream);
if (items_read != static_cast<size_t>(record_size) ||
!fortio_complete_read(fortio, record_size))
return false;
}
return itr == end;

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.

This is not the case as encountering a zero-length record still consumes the four bytes from the stream. If the stream is not progressed, then fortio_init_read returns -1 and you get an early exit.

Comment thread pyproject.toml Outdated
@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch 3 times, most recently from d74ff8a to 51ab1cd Compare June 10, 2026 07:56
Comment thread lib/resdata/FortIO.cpp
Comment thread lib/resdata/FortIO.cpp
Comment thread lib/resdata/FortIO.cpp Outdated
@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch 4 times, most recently from 11c7e90 to 2797737 Compare June 10, 2026 08:44
Comment thread lib/resdata/FortIO.cpp
@github-project-automation github-project-automation Bot moved this from Ready for Review to Reviewed in SCOUT Jun 10, 2026
@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch from 2797737 to d04072c Compare June 10, 2026 12:59
@eivindjahren eivindjahren force-pushed the fix_fortio_fread_buffer branch from d04072c to 1e050c9 Compare June 10, 2026 13:04
@eivindjahren eivindjahren merged commit 3ce65d8 into main Jun 10, 2026
12 checks passed
@eivindjahren eivindjahren deleted the fix_fortio_fread_buffer branch June 10, 2026 14:01
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in SCOUT Jun 10, 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