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

r3.in.v5d: Fix unchecked return value #4141

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

Conversation

ShubhamDesai
Copy link
Contributor

This pull request addresses an issue identified by Coverity Scan (CID: 1207300), where the return value of the lseek function is not checked.

@github-actions github-actions bot added C Related code is in C module raster3d labels Aug 5, 2024
@@ -1967,7 +1967,9 @@ int v5dReadCompressedGrid(v5dstruct *v, int time, int var, float *ga, float *gb,

/* move to position in file */
pos = grid_position(v, time, var);
lseek(v->FileDesc, pos, SEEK_SET);
if (lseek(v->FileDesc, pos, SEEK_SET) == -1) {
printf("lseek failed.\n");
Copy link
Member

Choose a reason for hiding this comment

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

If that fails, more is needed than a simple printf and moving to the next line. See how lseek failures are handled elsewhere in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, i will refer those codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

referred the approach from this file : lib/segment/seek.c
errorno and G_fatal_error is used to log the error message

Copy link
Contributor

Choose a reason for hiding this comment

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

For some reason the raster3d library has partly its own error handling (API). I'm not familiar with the motivation for treating raster3d lib differently. In any case, by that logic it should be Rast3d_fatal_error().

However, in this module all functions calling lseek returns 0 by error, which is then handled accordingly in r3.in.v5d/main.c (although that may need some care, but that is out of scope for this PR). So, I'd suggest (1) to address all calls of lseek() in the r3.in.v5d/v5d.c file and (2) change (all related) printf() to G_warning() and return 0;.

(You could update SKIP(N) to a multiline macro with a do-while statement.)

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @nilason. The function is written to behave that way, so that of course wins over the general practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason the raster3d library has partly its own error handling (API). I'm not familiar with the motivation for treating raster3d lib differently. In any case, by that logic it should be Rast3d_fatal_error().

However, in this module all functions calling lseek returns 0 by error, which is then handled accordingly in r3.in.v5d/main.c (although that may need some care, but that is out of scope for this PR). So, I'd suggest (1) to address all calls of lseek() in the r3.in.v5d/v5d.c file and (2) change (all related) printf() to G_warning() and return 0;.

(You could update SKIP(N) to a multiline macro with a do-while statement.)

@nilason just one doubt about the second change that you suggested all printf to change to G_warning its related to the lseek printfs right and also should it be G_warning or G_fatal

Copy link
Contributor

Choose a reason for hiding this comment

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

@nilason just one doubt about the second change that you suggested all printf to change to G_warning its related to the lseek printfs right and also should it be G_warning or G_fatal

What I mean is to address all (but one) lseek in this file. I believe there are 14 cases. 12 of these are in functions returning 0 in case of failure, one is returning the lseek return value, and one is part of a macro.
In all these cases (except in ltell() which you should leave as is) you can check if lseek gives -1 (or < 0) and if that's the case, issue G_warning (no G_fatal_error) and return 0. There are two cases with failing lseek giving printf "error" message, which you can change to G_warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have done the changes. Could you please look into it once also please tell me if the SKIP(N) macro is correctly updated. I don't know whether my interpretation about it is correct or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good, the macro too!
The printf messages were never translated, so let’s use our “standard” text there also. strerror is more informative anyway.

@nilason nilason changed the title raster3d: Fix unchecked return value r3.in.v5d: Fix unchecked return value Aug 8, 2024
raster3d/r3.in.v5d/v5d.c Outdated Show resolved Hide resolved
raster3d/r3.in.v5d/v5d.c Outdated Show resolved Hide resolved
raster3d/r3.in.v5d/v5d.c Outdated Show resolved Hide resolved
raster3d/r3.in.v5d/v5d.c Outdated Show resolved Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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 raster3d
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants