← 返回首页
Cpp: new experimental query cpp/guarded-free by mario-campos · Pull Request #16331 · github/codeql · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Cpp: new experimental query cpp/guarded-free#16331

Merged
MathiasVP merged 4 commits into
github:mainfrom
mario-campos:mario-campos/guarded-free
May 1, 2024
Merged

Cpp: new experimental query cpp/guarded-free#16331
MathiasVP merged 4 commits into
github:mainfrom
mario-campos:mario-campos/guarded-free

Conversation

Copy link
Copy Markdown
Contributor

This PR introduces a new experimental query that finds instances of free() guarded by a superfluous NULL check.

Copy link
Copy Markdown
Contributor

QHelp previews:

cpp/ql/src/experimental/Best Practices/GuardedFree.qhelp

Guarded Free

The free function, which deallocates heap memory, may accept a NULL pointer and take no action. Therefore, it is unnecessary to check its argument for the value of NULL before a function call to free. As such, these guards may hinder performance and readability.

Recommendation

A function call to free should not depend upon the value of its argument. Delete the if condition preceeding a function call to free when its only purpose is to check the value of the pointer to be freed.

Example

void test() { char *foo = malloc(100); // BAD if (foo) free(foo); // GOOD free(foo); }

References

Comment thread cpp/ql/src/experimental/Best Practices/GuardedFree.ql Outdated Show resolved Hide resolved
Copy link
Copy Markdown
Contributor

MathiasVP left a comment

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

LGTM!

MathiasVP merged commit a8f2cbc into github:main May 1, 2024
mario-campos deleted the mario-campos/guarded-free branch May 1, 2024 16:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Footer

© 2026 GitHub, Inc.