Skip to content

r.to.rast3elev: Check return-value for 'scanf'-like functions - #7660

Merged
echoix merged 1 commit into
OSGeo:mainfrom
echoix:alert-autofix-239
Jul 12, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
echoix:alert-autofix-239

Conversation

@echoix

@echoix echoix commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

From me:

Resolves two CodeQL warning.

In https://en.cppreference.com/c/io/fscanf, it shows how the return value of sscanf is the number of successfully assigned arguments, with can also be zero or EOF.

The change is to explicitly check for the value 1.


From the auto fix (adapted):

Potential fix for https://github.com/OSGeo/grass/security/code-scanning/529
And https://github.com/OSGeo/grass/security/code-scanning/530

The correct fix is to validate sscanf return values against the exact expected number of matched items, not just truthiness.

In raster/r.to.rast3elev/main.c, update both numeric parsing checks:

  • Line around if (sscanf(param.upper->answer, "%lf", &db.upper))
  • Line around if (sscanf(param.lower->answer, "%lf", &db.lower))

Change each condition to == 1, since each call expects exactly one %lf conversion. This preserves existing behavior for valid input while correctly rejecting parse failures and any non-success return values (including EOF).

No new methods, imports, or dependencies are needed.

… check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-actions github-actions Bot added raster Related to raster data processing C Related code is in C module labels Jul 9, 2026
@echoix
echoix requested a review from nilason July 10, 2026 11:35

@wenzeslaus wenzeslaus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great and clean.

@echoix
echoix merged commit ae111ef into OSGeo:main Jul 12, 2026
27 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 12, 2026
@echoix

echoix commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

I have a follow-up next with (most) of the remaining instances of this.

echoix added a commit to echoix-org/grass that referenced this pull request Jul 12, 2026
…7660)

Potential fix for code scanning alert no. 239: Incorrect return-value check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
echoix added a commit that referenced this pull request Jul 13, 2026
…e function" (#7673)

Follow-up from #7660

This PR applies the same logic as the other PR, where how sscanf can also return EOF "if input failure occurs before the first receiving argument was assigned". (See https://en.cppreference.com/c/io/fscanf).

Here, the little touch that is different for `raster/r.out.vtk/writeascii.c` and `raster3d/r3.out.vtk/writeVTKData.c`, is that I assume that if there's the `EOF` case, or any invalid case for the `null_value`, we would still want the null value to be 0. I think that's the design intention behind the code.


This was assisted by AI (Copilot-generated fix suggestions for code scanning alerts), but we can reason through it and I applied some myself in order to batch them in one single PR.

This should fix:
- https://github.com/OSGeo/grass/security/code-scanning/525
- https://github.com/OSGeo/grass/security/code-scanning/526
- https://github.com/OSGeo/grass/security/code-scanning/527
- https://github.com/OSGeo/grass/security/code-scanning/528
- https://github.com/OSGeo/grass/security/code-scanning/531
- https://github.com/OSGeo/grass/security/code-scanning/532
- https://github.com/OSGeo/grass/security/code-scanning/533


* Potential fix for code scanning alert no. 525: Incorrect return-value check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 528: Incorrect return-value check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Update writeascii.c

* r3.out.vtk: Fix "Incorrect return-value check for a 'scanf'-like function" for null value

* r.watershed: Fix "Incorrect return-value check for a 'scanf'-like function" for disturbed_land default value

Adresses https://github.com/OSGeo/grass/security/code-scanning/532

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
echoix added a commit to wenzeslaus/grass that referenced this pull request Jul 14, 2026
…e function" (OSGeo#7673)

Follow-up from OSGeo#7660

This PR applies the same logic as the other PR, where how sscanf can also return EOF "if input failure occurs before the first receiving argument was assigned". (See https://en.cppreference.com/c/io/fscanf).

Here, the little touch that is different for `raster/r.out.vtk/writeascii.c` and `raster3d/r3.out.vtk/writeVTKData.c`, is that I assume that if there's the `EOF` case, or any invalid case for the `null_value`, we would still want the null value to be 0. I think that's the design intention behind the code.


This was assisted by AI (Copilot-generated fix suggestions for code scanning alerts), but we can reason through it and I applied some myself in order to batch them in one single PR.

This should fix:
- https://github.com/OSGeo/grass/security/code-scanning/525
- https://github.com/OSGeo/grass/security/code-scanning/526
- https://github.com/OSGeo/grass/security/code-scanning/527
- https://github.com/OSGeo/grass/security/code-scanning/528
- https://github.com/OSGeo/grass/security/code-scanning/531
- https://github.com/OSGeo/grass/security/code-scanning/532
- https://github.com/OSGeo/grass/security/code-scanning/533


* Potential fix for code scanning alert no. 525: Incorrect return-value check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 528: Incorrect return-value check for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Update writeascii.c

* r3.out.vtk: Fix "Incorrect return-value check for a 'scanf'-like function" for null value

* r.watershed: Fix "Incorrect return-value check for a 'scanf'-like function" for disturbed_land default value

Adresses https://github.com/OSGeo/grass/security/code-scanning/532

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@echoix
echoix deleted the alert-autofix-239 branch September 12, 2026 19:47
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.

2 participants