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

C++: Promote cpp/guarded-free out of experimental #18111

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

Conversation

jketema
Copy link
Contributor

@jketema jketema commented Nov 26, 2024

Pull Request checklist

All query authors

Internal query authors only

  • Autofixes generated based on these changes are valid, only needed if this PR makes significant changes to .ql, .qll, or .qhelp files. See the documentation (internal access required).
  • Changes are validated at scale (internal access required).
  • Adding a new query? Consider also adding the query to autofix.

Copy link
Contributor

QHelp previews:

cpp/ql/src/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 condition preceding 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);
}

In this example the condition checking the value of foo can be deleted.

References

@jketema jketema marked this pull request as ready for review November 26, 2024 21:46
@jketema jketema requested a review from a team as a code owner November 26, 2024 21:46
@jketema jketema added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Nov 26, 2024
@jketema jketema changed the title C++: Promote cpp/guarded-free C++: Promote cpp/guarded-free out of experimental Nov 26, 2024
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

Changes LGTM. A sample of the new DCA results LGTM.

We could replace FreeCall with DeallocationExpr and fc.getArgument(0) with fc.getFreedExpr(), to add results for all kinds of free variants including, I think, delete. This is assuming we're confident all of these variants are in fact safe on NULL. It probably makes sense to promote the query as it is and consider this as a follow-up step afterwards?

Needs a docs review.

@jketema
Copy link
Contributor Author

jketema commented Nov 27, 2024

It probably makes sense to promote the query as it is and consider this as a follow-up step afterwards?

That would have my preference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ documentation ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants