Skip to content

v.class: Fix out-of-bounds read when no records are selected - #7692

Merged
wenzeslaus merged 1 commit into
OSGeo:mainfrom
petrasovaa:fix-v.class
Jul 14, 2026
Merged

wenzeslaus merged 1 commit into
OSGeo:mainfrom
petrasovaa:fix-v.class

Conversation

@petrasovaa

@petrasovaa petrasovaa commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Context

test_v_class_where_no_matches fails intermittently on Windows CI but passes on Linux. It is not a flaky test: it was asserting on the contents of uninitialized memory.

When the where clause selects no records, nrec is 0, so v.class hands an empty array to the classification library, which reads data[0] and data[count - 1]:

Invalid read of size 8
   at AS_class_interval (class.c:66)
 Address 0xdc86008 is 8 bytes before a block of size 1 alloc'd

The printed breaks were whatever the heap held. Linux zeroes fresh heap pages, so they came out as 0.0 and the test passed; Windows reuses dirty memory, so sometimes not.

Change

v.class keeps its behavior (warning, zeroed breaks, exit 0) but now produces it deliberately: with no records it skips the classification and stats calls, leaving the zero-initialized breaks and frequencies in place. AS_class_apply_algorithm also rejects an empty array, removing the same latent bug in d.vect.thematic.

Commit message

An empty WHERE result left an empty data array, which the classification
algorithms then indexed at data[0] and data[count - 1]. The values printed
as class breaks were whatever the heap happened to hold, which is zeroed
on Linux but not on Windows, making the test flaky there.

Warn the user and keep the zero-initialized breaks when nothing is selected, and
reject an empty array in AS_class_apply_algorithm.

An empty WHERE result left an empty data array, which the classification
algorithms then indexed at data[0] and data[count - 1]. The values printed
as class breaks were whatever the heap happened to hold, which is zeroed
on Linux but not on Windows, making the test flaky there.

Warn and keep the zero-initialized breaks when nothing is selected, and
reject an empty array in AS_class_apply_algorithm.
@petrasovaa petrasovaa added the bug Something isn't working label Jul 14, 2026

@wenzeslaus wenzeslaus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change to make valid but "zero" output on empty selection makes sense for the tool.

I have some doubts about using warning for no selection, but @petrasovaa says it is appropriate given the context (unlike e.g. for v.db.select).

I assume more work in the library would be needed to do something else than fatal at this point.

@wenzeslaus
wenzeslaus enabled auto-merge (squash) July 14, 2026 09:05
@github-actions github-actions Bot added vector Related to vector data processing Python Related code is in Python C Related code is in C libraries module tests Related to Test Suite labels Jul 14, 2026
@wenzeslaus
wenzeslaus merged commit d4b639d into OSGeo:main Jul 14, 2026
26 of 29 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 14, 2026
echoix pushed a commit to wenzeslaus/grass that referenced this pull request Jul 14, 2026
)

An empty WHERE result left an empty data array, which the classification
algorithms then indexed at data[0] and data[count - 1]. The values printed
as class breaks were whatever the heap happened to hold, which is zeroed
on Linux but not on Windows, making the test flaky there.

Warn the user and keep the zero-initialized breaks when nothing is selected, and
reject an empty array in AS_class_apply_algorithm.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working C Related code is in C libraries module Python Related code is in Python tests Related to Test Suite vector Related to vector data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants