From 526ad6d4dbad92e3385c3f5b853dc309d5c76cb0 Mon Sep 17 00:00:00 2001 From: R Sai Pranav Date: Sun, 23 Aug 2026 04:09:29 +0530 Subject: [PATCH] r.stats: Use a 64-bit counter for cell counts 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 #7769 --- raster/r.stats/stats.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/raster/r.stats/stats.c b/raster/r.stats/stats.c index 66f67d49a28..43bb3513446 100644 --- a/raster/r.stats/stats.c +++ b/raster/r.stats/stats.c @@ -1,3 +1,4 @@ +#include #include #include "global.h" @@ -23,14 +24,14 @@ struct Node { struct Node *left; struct Node *right; struct Node *list; - long count; + grass_int64 count; double area; }; static struct Node **hashtable; static struct Node *node_list = NULL; static int node_count = 0; -static long total_count = 0; +static grass_int64 total_count = 0; int initialize_cell_stats(int n) { @@ -200,7 +201,7 @@ static int node_compare(const void *pp, const void *qq) static int node_compare_count_asc(const void *pp, const void *qq) { struct Node *const *p = pp, *const *q = qq; - long a, b; + grass_int64 a, b; a = (*p)->count; b = (*q)->count; @@ -213,7 +214,7 @@ static int node_compare_count_asc(const void *pp, const void *qq) static int node_compare_count_desc(const void *pp, const void *qq) { struct Node *const *p = pp, *const *q = qq; - long a, b; + grass_int64 a, b; a = (*p)->count; b = (*q)->count; @@ -550,7 +551,7 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts, break; case CSV: case PLAIN: - fprintf(stdout, "%s%ld", fs, (long)node->count); + fprintf(stdout, "%s%" PRId64, fs, node->count); break; } }