Skip to content

r.coin: Fail when r.stats returns no data - #7841

Merged
echoix merged 1 commit into
OSGeo:mainfrom
Pranav-error:fix-rcoin-empty-stats
Aug 23, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
Pranav-error:fix-rcoin-empty-stats

Conversation

@Pranav-error

Copy link
Copy Markdown
Contributor

Fixes #7734.

When r.stats produces no output, make_coin() parsed zero records and nothing noticed:

  • the read loop ends without entering the "Unexpected output from r.stats" branch, which only catches malformed lines
  • the category lists are then allocated with G_calloc(0, ...)
  • collapse() starts its counter at 1, so an empty list still reports one category
  • that category's value is read from the zero length allocation, which is undefined behaviour and in practice reads as 0

The result was a normal looking coincidence report for a single category 0, with EXIT_SUCCESS. A failure of r.stats was indistinguishable from a valid result, which is how it went unnoticed in the OSGeo4W CI in #7732.

Treating zero records as an error also removes the read from the zero length allocation.

I have not done the second improvement suggested in the issue, checking the child's exit status, because G_popen_close() discards it and that is a lib/gis change.

Reproducing it

Two maps of only NULL cells are enough, since r.coin runs r.stats with the flag that skips the no data value, so nothing is printed:

r.mapcalc "empty1 = null()"
r.mapcalc "empty2 = null()"
r.coin first=empty1 second=empty2 units=c

Before, that prints a full report with cat# 0 and all zeros, and exits 0. After, it exits 1 with ERROR: No data returned from r.stats.

Tests

test_r_coin_errors_without_data in the existing raster/r.coin/tests/test_coin.py does exactly the above and expects the module to fail. Checked against the unfixed build, where it fails with DID NOT RAISE CalledModuleError; the existing test_r_coin passes either way. clang-format and ruff report no changes.

I used an AI assistant while preparing this. I understand the change and can explain it.

With no records parsed from r.stats, the category lists were allocated
with a length of zero, collapse() counted from one and reported a single
category anyway, and that category's value was read from the zero length
allocation. r.coin printed a normal looking report for a category 0 and
exited successfully, so a failure of r.stats looked like a valid result.

Treat zero records as an error, which also removes the read from the zero
length allocation.

Fixes OSGeo#7734
Copilot AI lite review requested due to automatic review settings August 23, 2026 00:24

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added raster Related to raster data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels Aug 23, 2026
@echoix
echoix merged commit fc2cd0a into OSGeo:main Aug 23, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] r.coin: Prints a category 0 table and exits with success when r.stats produces no output

3 participants