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.gisenv: Fix Resource Leak issue in main.c #4966

Merged
merged 6 commits into from
Jan 31, 2025
Merged

Conversation

ShubhamDesai
Copy link
Contributor

This pull request fixes issue identified by Coverity Scan (CID : 1207681)
Used G_free() to fix this issue.

@github-actions github-actions bot added C Related code is in C module general labels Jan 19, 2025
@ShubhamDesai
Copy link
Contributor Author

@nilason won't the G_free directly work here?

@nilason
Copy link
Contributor

nilason commented Jan 19, 2025

@nilason won't the G_free directly work here?

Not quite sure what you mean.

@nilason
Copy link
Contributor

nilason commented Jan 19, 2025

@nilason won't the G_free directly work here?

Not quite sure what you mean.

If you mean G_free(name) directly after u_name = G_store(name);, then yes.

@ShubhamDesai
Copy link
Contributor Author

@nilason won't the G_free directly work here?

Not quite sure what you mean.

If you mean G_free(name) directly after u_name = G_store(name);, then yes.

I used G_free() directly but still it failed.
why this issue arised? can you please help.

@nilason
Copy link
Contributor

nilason commented Jan 21, 2025

@nilason won't the G_free directly work here?

Not quite sure what you mean.

If you mean G_free(name) directly after u_name = G_store(name);, then yes.

I used G_free() directly but still it failed. why this issue arised? can you please help.

I now see you mean the tests are failing. The reason wasn't obvious to me at first. This may be a good exercise :).
Hint: understand the purpose of the function, and study the function line-by-line, in particular the use of the variable value, therein lies the root of the problem.

@ShubhamDesai
Copy link
Contributor Author

ShubhamDesai commented Jan 21, 2025

@nilason won't the G_free directly work here?

Not quite sure what you mean.

If you mean G_free(name) directly after u_name = G_store(name);, then yes.

I used G_free() directly but still it failed. why this issue arised? can you please help.

I now see you mean the tests are failing. The reason wasn't obvious to me at first. This may be a good exercise :). Hint: understand the purpose of the function, and study the function line-by-line, in particular the use of the variable value, therein lies the root of the problem.

The parse_variable function parses a string such as "VAR=value", separating the variable name (VAR) and value (value). Based on your hint I updated the function and tested locally with following testcases
Input with a variable and value:
Input: "VAR=value"
Result: "VAR"
Value: "value"

Input with only a variable (no =):
Input: "VAR"
Result: "VAR"
Value: (null)

Input with an empty value:
Input: "VAR="
Result: "VAR"
Value: (null)

All of them were as expected. It passed on my local windows machine
Could you please review once.

@metzm
Copy link
Contributor

metzm commented Jan 27, 2025

Looks good to me, but please check with valgrind that there are no invalid memory read/write attempts.

@metzm
Copy link
Contributor

metzm commented Jan 27, 2025

Correction: does not look good to me, the strings stored in char *value by parse_variable() are not G_free()d at the end in main(). First, if there would be so many GRASS envs, that string their values in memory could lead to OOM errors, this fix would not help. Second, there is only a very limited number of GRASS envs, memory consumption by parse_variable() storing variable + value in name should not even be measurable on a current system. IMHO this fix does not make sense, I would leave the code as it is.

@metzm
Copy link
Contributor

metzm commented Jan 27, 2025

In short: storing data somewhere else without free'ing them is not a solution, and there is not much to free anyway, the system can take care of that.

@ShubhamDesai
Copy link
Contributor Author

@nilason can you review once and tell me what is wrong.

Copy link
Contributor

@nilason nilason left a comment

Choose a reason for hiding this comment

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

Correction: does not look good to me, the strings stored in char *value by parse_variable() are not G_free()d at the end in main(). First, if there would be so many GRASS envs, that string their values in memory could lead to OOM errors, this fix would not help. Second, there is only a very limited number of GRASS envs, memory consumption by parse_variable() storing variable + value in name should not even be measurable on a current system. IMHO this fix does not make sense, I would leave the code as it is.

In short: storing data somewhere else without free'ing them is not a solution, and there is not much to free anyway, the system can take care of that.

It is in this case, as probably in many of the other 600+ resource leak issues reported by Coverity Scan, indeed more of a "false positive". At least, Coverity Scan does not report (correctly) on unfreed memory in main functions or immediately before an exit (as with G_fatal_error). I'm of the opinion that it is advantageous to limit the issues of tools such as Coverity Scan raises to be able to catch also the "real" ones. Easy and uncontroversial fixes, even if not critical, should be made for that reason alone.

@nilason nilason added this to the 8.5.0 milestone Jan 31, 2025
@echoix echoix merged commit 4f3097d into OSGeo:main Jan 31, 2025
27 checks passed
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
Projects
Development

Successfully merging this pull request may close these issues.

6 participants