From 1875e351cd16ec855e6e344b9d1ec518cd5d6b55 Mon Sep 17 00:00:00 2001 From: sai pranav Date: Wed, 26 Aug 2026 14:04:22 +0530 Subject: [PATCH 1/2] r.clump: Fail with the clump limit instead of overflowing the index r.clump asked G_realloc() for 18446744065119617024 bytes and stopped with a memory error, which reads as a memory problem when it is not one. label and nalloc are both 32 bit. nalloc starts at INCR and grows in INCR steps, so it lands exactly on 2^31 and wraps to INT_MIN, and nalloc * sizeof(CELL) then converts to (size_t)(INT_MIN * 4), which is 18446744065119617024 - the number in the report. Clump IDs are raster values of type CELL, so as @metzm said on the issue the limit of one CELL cannot be raised. Report it as the limit it is instead, and stop INCR short of INT_MAX so nalloc cannot overflow either. Fixes #6412 --- raster/r.clump/clump.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/raster/r.clump/clump.c b/raster/r.clump/clump.c index a3ff528ee41..ac0a6883016 100644 --- a/raster/r.clump/clump.c +++ b/raster/r.clump/clump.c @@ -26,9 +26,15 @@ #include #include "local_proto.h" #include +#include #include -#define INCR 1024 +#define INCR 1024 + +/* Clump IDs are stored as CELL, a 32 bit signed integer, so the number of + * clumps cannot exceed the largest CELL value. Stopping INCR short of that + * also keeps nalloc, which grows in INCR steps, from overflowing. */ +#define MAX_LABEL (INT_MAX - INCR) int print_time(time_t *); @@ -311,6 +317,13 @@ CELL clump(int *in_fd, int out_fd, int diag, int minsize) if (NEW == 0 || OLD == NEW) { /* ok */ if (OLD == 0) { /* start a new clump */ + if (label >= MAX_LABEL) + G_fatal_error( + _("Too many clumps: the maximum number of clumps " + "is %d. Reduce the extent or the resolution of " + "the computational region, or use a threshold " + "to merge similar cells."), + MAX_LABEL); label++; cur_clump[col] = label; if (label >= nalloc) { @@ -641,6 +654,13 @@ CELL clump_n(int *in_fd, char **inname, int nin, double threshold, int out_fd, if (NEW == 0 || OLD == NEW) { /* ok */ if (OLD == 0) { /* start a new clump */ + if (label >= MAX_LABEL) + G_fatal_error( + _("Too many clumps: the maximum number of clumps " + "is %d. Reduce the extent or the resolution of " + "the computational region, or use a threshold " + "to merge similar cells."), + MAX_LABEL); label++; cur_clump[col] = label; if (label >= nalloc) { From 132fcd8d4c52caba3a6948f4cb7978037e35d9fd Mon Sep 17 00:00:00 2001 From: R Sai Pranav Date: Tue, 1 Sep 2026 15:12:38 +0530 Subject: [PATCH 2/2] r.clump: Reword the clump limit message Per review: say "the maximum supported number of clumps". Re-wrapped the string so the lines stay within 80 columns. --- raster/r.clump/clump.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/raster/r.clump/clump.c b/raster/r.clump/clump.c index ac0a6883016..af1ef1dbf3d 100644 --- a/raster/r.clump/clump.c +++ b/raster/r.clump/clump.c @@ -319,10 +319,10 @@ CELL clump(int *in_fd, int out_fd, int diag, int minsize) /* start a new clump */ if (label >= MAX_LABEL) G_fatal_error( - _("Too many clumps: the maximum number of clumps " - "is %d. Reduce the extent or the resolution of " - "the computational region, or use a threshold " - "to merge similar cells."), + _("Too many clumps: the maximum supported number " + "of clumps is %d. Reduce the extent or the " + "resolution of the computational region, or use " + "a threshold to merge similar cells."), MAX_LABEL); label++; cur_clump[col] = label; @@ -656,10 +656,10 @@ CELL clump_n(int *in_fd, char **inname, int nin, double threshold, int out_fd, /* start a new clump */ if (label >= MAX_LABEL) G_fatal_error( - _("Too many clumps: the maximum number of clumps " - "is %d. Reduce the extent or the resolution of " - "the computational region, or use a threshold " - "to merge similar cells."), + _("Too many clumps: the maximum supported number " + "of clumps is %d. Reduce the extent or the " + "resolution of the computational region, or use " + "a threshold to merge similar cells."), MAX_LABEL); label++; cur_clump[col] = label;