Skip to content

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

Open
echoix wants to merge 1 commit into
OSGeo:mainfrom
echoix:alert-autofix-239
Open

r.to.rast3elev: Check return-value for 'scanf'-like functions#7660
echoix wants to merge 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
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.

1 participant