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

fix memory leak in the example #2578

Open
wants to merge 1 commit into
base: v3/master
Choose a base branch
from

Conversation

bsulmanas
Copy link

error string is created via strdup and it must be passed to free() to avoid a memory leak.

Not really important in this particular case, but if would be consistent with msc_rules_cleanup(rules) and msc_cleanup(modsec)

error string is created via strdup and it must be passed to free to avoid a memory leak.
@bsulmanas bsulmanas changed the title free error string fix memory leak in the example Jun 14, 2021
@zimmerle zimmerle added the 3.x Related to ModSecurity version 3.x label Jul 9, 2021
Copy link
Contributor

@zimmerle zimmerle left a comment

Choose a reason for hiding this comment

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

Hi @bsulmanas,

Please see the review inline.

@@ -70,6 +70,7 @@ int main (int argc, char **argv)
end:
msc_rules_cleanup(rules);
msc_cleanup(modsec);
free(error);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @bsulmanas,

The variable error is indeed filled if there is an error. Otherwise, it may point towards NULL. Considering the former case and the suggested patch, it will lead to a double-free-ish mistake. The variable error can be sefely freed on lines 45 and 54, where its usage is known.

Copy link

Choose a reason for hiding this comment

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

Yeah, zimmerle's suggestion is a good solution.

To keep it at the 'end' label as your code is right now, you would need to wrap it inside a if ( error != NULL ) because reaching the end label doesn't mean there was an error, and if there was no error the code would try to do a free( NULL );...

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I'm fairly sure that the PR is safe as it stands. Calling free on a NULL pointer has been considered safe from very early days.

See, for example 7.20.3.2 from https://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf

"If ptr is a null pointer, no action occurs"

Copy link

sonarcloud bot commented Jan 25, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Related to ModSecurity version 3.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants