Repository navigation
lib/raster3d: fix int overflow in Rast3d_copy_values byte offset - #7970
Merged
Merged
Conversation
… offset. eltLength, offsSrc and offsDst are all int, so the byte offset is multiplied in 32-bit arithmetic. G_incr_void_ptr() (lib/gis/alloc.c:182) already takes a size_t, but the product has overflowed before it gets there. In our crash, the element offset was 681 M, which fits in an int. Multiplied by 4 bytes for FCELL it becomes 2.7 G, which overflows, goes negative, and memcpy writes before the buffer. The minimal fix: widen the multiplication to size_t: 67 src = G_incr_void_ptr(src, (size_t)eltLength * offsSrc); 68 dst = G_incr_void_ptr(dst, (size_t)eltLength * offsDst); That fixes our case: blocks of up to 2³¹ − 1 cells, which is 8 GiB of FCELL. Line 70 already casts the memcpy length the same way ((size_t)nElts * eltLength), so the fix makes lines 67–68 consistent with the line just below them. A complete fix for blocks over 2³¹ cells would also need two more changes. Both alter the public API, so they suit a separate PR: - lib/raster3d/getblock.c:66–67 computes the destination offset (z + dz) * nx * ny + (y + dy) * nx + (x + dx) in int. - Rast3d_copy_values() takes its offsets as int (the prototype is at include/grass/defs/raster3d.h:173). Both would have to become size_t.
nilason
approved these changes
Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… offset.
eltLength, offsSrc and offsDst are all int, so the byte offset is multiplied in 32-bit arithmetic. G_incr_void_ptr() (lib/gis/alloc.c:182) already takes a size_t, but the product has overflowed before it gets there. In our crash, the element offset was 681 M, which fits in an int. Multiplied by 4 bytes for FCELL it becomes 2.7 G, which overflows, goes negative, and memcpy writes before the buffer.
The minimal fix: widen the multiplication to size_t:
67 src = G_incr_void_ptr(src, (size_t)eltLength * offsSrc);
68 dst = G_incr_void_ptr(dst, (size_t)eltLength * offsDst);
That fixes our case: blocks of up to 2³¹ − 1 cells, which is 8 GiB of FCELL. Line 70 already casts the memcpy length the same way ((size_t)nElts * eltLength), so the fix makes lines 67–68 consistent with the line just below them. A complete fix for blocks over 2³¹ cells would also need two more changes. Both alter the public API, so they suit a separate PR: