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

Merged
merged 8 commits into from
Nov 29, 2024

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

github-actions bot commented Nov 26, 2024

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 the 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.

@mchammer01 mchammer01 self-requested a review November 28, 2024 14:01
@mchammer01
Copy link
Contributor

I'll review this on behalf of Docs.

mchammer01
mchammer01 previously approved these changes Nov 28, 2024
Copy link
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

LGTM ✨
Approving this but left a few comments and suggestions following editorial review.

cpp/ql/src/Best Practices/GuardedFree.qhelp Outdated Show resolved Hide resolved
cpp/ql/src/Best Practices/GuardedFree.qhelp Outdated Show resolved Hide resolved
cpp/ql/src/Best Practices/GuardedFree.qhelp Outdated Show resolved Hide resolved
@jketema
Copy link
Contributor Author

jketema commented Nov 28, 2024

LGTM ✨ Approving this but left a few comments and suggestions following editorial review.

Thanks for the review and the suggestions!

@jketema
Copy link
Contributor Author

jketema commented Nov 29, 2024

@geoffw0 Would you mind re-approving so we can merge this?

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.

👍

@jketema jketema merged commit bcb7901 into github:main Nov 29, 2024
16 checks passed
@jketema jketema deleted the guarded-free branch November 29, 2024 09:43
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.

3 participants