Skip to content

r.stats: Use a 64-bit counter for cell counts - #7839

Closed
Pranav-error wants to merge 2 commits into
OSGeo:mainfrom
Pranav-error:fix-rstats-count-overflow
Closed

Pranav-error wants to merge 2 commits into
OSGeo:mainfrom
Pranav-error:fix-rstats-count-overflow

Conversation

@Pranav-error

Copy link
Copy Markdown
Contributor

Fixes #7769.

The per-category and total counters in r.stats were long. That is 64 bits on Linux and macOS but 32 bits on Windows, so a map with more than about 2.1 billion cells wraps and the counts print negative. The reporter's maps are 105063x74530, about 7.8 billion cells, which matches the negative values in the issue.

This is @marisn's diagnosis from the issue thread; the change is to act on it.

struct Node.count, total_count and the two comparison variables become grass_int64, and the count is printed with PRId64 the way r.info already prints total_cells. The JSON path already went through a double and is unchanged.

Verification

I cannot reproduce the overflow on macOS, because long is already 64 bits there — that is the reason the bug is Windows-only, and it is also why I have not added a test: triggering it needs a map of more than 2^31 cells.

What I did check:

  • builds clean, clang-format reports no changes
  • r.stats output is byte for byte identical to main on a small map for -c, -c with two inputs, -cp, and format=json, so the counter and format changes do not alter normal output

raw_stats.c also casts to long, but those are CELL category values rather than counts, so they are left alone.

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

The per-category and total counters were long, which is 32 bits on
Windows, so a map with more than about 2.1 billion cells wrapped and
r.stats printed negative counts. The reporter's maps were 105063x74530,
or roughly 7.8 billion cells.

Use grass_int64 for the counters and the comparison variables, and print
with PRId64 the way r.info already does.

Fixes OSGeo#7769
Copilot AI lite review requested due to automatic review settings August 22, 2026 22:39

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 C Related code is in C module labels Aug 22, 2026
@marisn

marisn commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

There is a PR already open for this issue: #7790

@Pranav-error

Copy link
Copy Markdown
Contributor Author

Ah, missed that — thanks for pointing it out. #7790 predates this and is already green, so I'll close in favor of it. Sorry for the duplicate work.

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 raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] r.stats reports negative counts when tabulating across 2 rasters

4 participants