Skip to content
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

g.region/r.to.rast3elev: fixed scanf error to recognize EOF as a possible return value #3452

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

naidneelttil
Copy link
Contributor

@naidneelttil naidneelttil commented Feb 25, 2024

scanf can return EOF as a return value, which can be a security issue if not accounted for.

@github-actions github-actions bot added raster Related to raster data processing C Related code is in C module general raster3d labels Feb 25, 2024
Copy link
Member

@HuidaeCho HuidaeCho left a comment

Choose a reason for hiding this comment

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

Thanks for your PR. Most (all?) of these checks are for G_parser() options. G_parser() does all necessary sanity checks and returns non-NULL values only if there are any valid values. Even an empty string with an option name (e.g., map="") returns a NULL in answer, so EOF from sscanf() is not really a concern when we already check for the nullity of answer.

sscanf() returns EOF if its first argument is an empty string ("\0"), but G_parser() doesn't return one in answer. answer will be either NULL or a non-empty string.

grass/lib/gis/parser.c

Lines 1180 to 1182 in 87d2d42

/* an empty string is not a valid answer, skip */
if (!*string)
return;

raster/r.out.vtk/writeascii.c Outdated Show resolved Hide resolved
general/g.region/main.c Outdated Show resolved Hide resolved
raster/r.out.vtk/writeascii.c Outdated Show resolved Hide resolved
raster/r.out.vtk/writeascii.c Outdated Show resolved Hide resolved
raster/r.to.rast3elev/main.c Show resolved Hide resolved
raster/r.to.rast3elev/main.c Show resolved Hide resolved
raster/r.watershed/ram/init_vars.c Outdated Show resolved Hide resolved
raster/r.watershed/seg/init_vars.c Outdated Show resolved Hide resolved
raster3d/r3.out.vtk/writeVTKData.c Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the raster3d label Mar 7, 2024
Copy link
Contributor Author

@naidneelttil naidneelttil left a comment

Choose a reason for hiding this comment

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

in this PR, I removed the explicit EOF checks but kept the checks for a scanf return value, since this is generally good practice and one less thing to worry about if ever refactoring.

@neteler neteler added this to the 8.4.0 milestone Mar 12, 2024
@echoix
Copy link
Member

echoix commented Mar 13, 2024

@HuidaeCho with this PR now limited to only three changes with checks like == 1, is this PR ready to go?

@neteler neteler changed the title fixed scanf error to recognize EOF as a possible return value Mar 13, 2024
@nilason
Copy link
Contributor

nilason commented Apr 9, 2024

Not only does the parser either return NULL or a non-empty string, the string is also guaranteed to contain a valid integer as it already been tested with

int check_int(const char *ans, const char **opts)

There is no way a compiler/static analyser could know this.

I see two possible "solutions" to this:

  1. Change to early exit strategy: eg. if (sscanf(value, "%i", &pix) != 1) die(parm.grow);
  2. Silence the warning with void: eg. if ((void)sscanf(value, "%i", &pix)) {

if (sscanf(value, "%i", &pix) == 1) { without response if the if condition is false, will only lead to new problems.

@wenzeslaus wenzeslaus modified the milestones: 8.4.0, Future Apr 27, 2024
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 general module raster Related to raster data processing
6 participants