-
-
Notifications
You must be signed in to change notification settings - Fork 437
lib/raster3d/tilewrite: Use >= for tile index bounds check
#7723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
echoix
wants to merge
3
commits into
main
Choose a base branch
from
ai-findings-autofix/lib-raster3d-tilewrite.c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f98c0a5
Apply suggested fix to lib/raster3d/tilewrite.c from Copilot Autofix
echoix 0271cdb
Merge remote-tracking branch 'upstream/main' into ai-findings-autofix…
wenzeslaus 4693358
lib/raster3d: Add test for Rast3d_write_tile tile index bounds check
wenzeslaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Test tile index bounds checking in Rast3d_write_tile""" | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| import grass.script as gs | ||
|
|
||
| WRITE_TILE_SCRIPT = """\ | ||
| import sys | ||
| from ctypes import POINTER, byref, c_float, cast | ||
|
|
||
| from grass.lib.gis import G_gisinit | ||
| from grass.lib.raster import FCELL_TYPE | ||
| from grass.lib.raster3d import ( | ||
| RASTER3D_NO_CACHE, | ||
| RASTER3D_Map, | ||
| RASTER3D_Region, | ||
| Rast3d_adjust_region, | ||
| Rast3d_init_defaults, | ||
| Rast3d_open_cell_new, | ||
| Rast3d_write_tile, | ||
| ) | ||
|
|
||
| index_offset = int(sys.argv[1]) | ||
|
|
||
| G_gisinit("test") | ||
| Rast3d_init_defaults() | ||
|
|
||
| region = RASTER3D_Region() | ||
| region.north = 100 | ||
| region.south = 0 | ||
| region.east = 100 | ||
| region.west = 0 | ||
| region.top = 100 | ||
| region.bottom = 0 | ||
| region.rows = 10 | ||
| region.cols = 10 | ||
| region.depths = 10 | ||
| Rast3d_adjust_region(byref(region)) | ||
|
|
||
| handle = Rast3d_open_cell_new( | ||
| "bounds_check_3d", FCELL_TYPE, RASTER3D_NO_CACHE, byref(region) | ||
| ) | ||
| if not handle: | ||
| sys.exit("Rast3d_open_cell_new failed") | ||
| map_ptr = cast(handle, POINTER(RASTER3D_Map)) | ||
| tile = (c_float * map_ptr.contents.tileSize)() | ||
| Rast3d_write_tile(map_ptr, map_ptr.contents.nTiles + index_offset, tile, FCELL_TYPE) | ||
| print("tile write accepted") | ||
| """ | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def session(tmp_path): | ||
| """Active session in an XY project (scope: function)""" | ||
| project = tmp_path / "xy_test" | ||
| gs.create_project(project) | ||
| with gs.setup.init(project, env=os.environ.copy()) as session: | ||
| yield session | ||
|
|
||
|
|
||
| def write_tile(index_offset, session, tmp_path): | ||
| """Call Rast3d_write_tile with tile index nTiles + index_offset. | ||
|
|
||
| Runs in a subprocess because an invalid index triggers a fatal error | ||
| which exits the calling process. | ||
| """ | ||
| script = tmp_path / "write_tile.py" | ||
| script.write_text(WRITE_TILE_SCRIPT, encoding="utf-8") | ||
| return subprocess.run( | ||
| [sys.executable, str(script), str(index_offset)], | ||
| env=session.env, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=60, | ||
| check=False, | ||
| ) | ||
|
|
||
|
|
||
| def test_write_tile_accepts_last_index(session, tmp_path): | ||
| """Writing the last valid tile index (nTiles - 1) is accepted""" | ||
| result = write_tile(-1, session, tmp_path) | ||
| assert result.returncode == 0, result.stderr | ||
| assert "tile write accepted" in result.stdout | ||
|
|
||
|
|
||
| def test_write_tile_rejects_index_past_end(session, tmp_path): | ||
| """A tile index equal to nTiles is rejected as out of range""" | ||
| result = write_tile(0, session, tmp_path) | ||
| assert result.returncode != 0, "out-of-range tile index was accepted" | ||
| assert "tileIndex out of range" in result.stderr | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.