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

Improve const correctness #3083

Merged
merged 1 commit into from Feb 15, 2024
Merged

Conversation

sgaist
Copy link
Contributor

@sgaist sgaist commented Feb 13, 2024

What type of PR is this?

Uncomment one (or more) /kind <> lines:

/kind bug

/kind cleanup

/kind design

/kind documentation

/kind failing-test

/kind feature

/kind release

Any specific area of the project related to this PR?

Uncomment one (or more) /area <> lines:

/area build

/area engine

/area tests

/area proposals

/area CI

What this PR does / why we need it:

This PR refactors the code to make use of const references rather than just reference or copy when applicable.
It also applies similar recommendations for pointers.

They were detected by cppcheck.

Which issue(s) this PR fixes:

Fixes #3082

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Copy link

This PR may bring feature or behavior changes in the Falco engine and may require the engine version to be bumped.

Please double check userspace/engine/falco_engine_version.h file. See versioning for FALCO_ENGINE_VERSION.

/hold

@@ -33,7 +33,7 @@ static std::vector<std::string> rule_matching_names = {
"all"
};

bool falco_common::parse_priority(std::string v, priority_type& out)
bool falco_common::parse_priority(const std::string& v, priority_type& out)
Copy link
Contributor

Choose a reason for hiding this comment

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

@federico-sysdig may we summon you again to help decide on a style more uniformly? For example have gotten feedback both ways, pass by value vs const ref ... and lately we transitioned to using std::string_view in libs. Thank you 🙏 .

Copy link
Contributor

Choose a reason for hiding this comment

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

TL;DR:
Pass by value all types that are cheap to copy (e.g. built-in types, but not only...) and pass by const reference the other ones.

There are other cases where you'll here advice to pass-by-value and then move or pass-by-rvalue-reference. I won't discuss them here. I think the matter gets more confused and difficult and a simple rule that is good enough is a superior choice. So, these cases apart an std::string should not be passed by value as it might not be "cheap" to copy, meaning it might require a heap allocation (unless it's very short, but small-string-optimization is another topic I'd leave out of the discussion).

Regarding std::string_view, the type is not built-in, clearly, but it's implemented as a pair of pointers (or a pointer and an integer, which is the same), thus it is cheap to copy and should be passed by value. See it as a more structured way to pass (char* str, int strlen), like we often do in C.

"Style" might come into play on the choice between const std::string& or std::string_view, but the latter came only recently with the library moving to C++17, so you won't see it very much. It is more efficient at times when the caller of the function uses a string literal as no temporary string gets created, so it is a better choice which I fully endorse, but, beware, it cannot be always used for passing the string buffer to a C function as it gives no guarantee of being null-terminated and it has some gotchas if you use it with an associative container whose key is an std::string (e.g. std::map<std::string, widget> C; and then, later on, C.find(mystringview)).

Briefly put, passing strings by const reference is fine, passing them through a std::string_view is even better, if you can afford it.

@@ -128,7 +128,7 @@ std::string falco_formats::format_event(sinsp_evt *evt, const std::string &rule,
line = full_line;
}

return line.c_str();
return line;
Copy link
Contributor

Choose a reason for hiding this comment

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

ohhh

@incertum
Copy link
Contributor

/milestone 0.38.0

@poiana poiana added this to the 0.38.0 milestone Feb 13, 2024
LucaGuerra
LucaGuerra previously approved these changes Feb 15, 2024
Copy link
Contributor

@LucaGuerra LucaGuerra left a comment

Choose a reason for hiding this comment

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

Thank you!

@poiana
Copy link

poiana commented Feb 15, 2024

LGTM label has been added.

Git tree hash: 0bbfbb72f405468c07f8c4a6ece269fc240c9538

@FedeDP
Copy link
Contributor

FedeDP commented Feb 15, 2024

Uh needs a rebase :)

FedeDP
FedeDP previously approved these changes Feb 15, 2024
Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

/approve

@poiana
Copy link

poiana commented Feb 15, 2024

LGTM label has been added.

Git tree hash: f4dab3003225f41dd0be2a91582bd0085d850434

incertum
incertum previously approved these changes Feb 15, 2024
Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

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

/approve

@incertum
Copy link
Contributor

CI seems to be in a bad state right now all over, we will check tomorrow.

@sgaist
Copy link
Contributor Author

sgaist commented Feb 15, 2024

@incertum it's innocent (in this case), I broke the test with too much constness.

Reported by cppcheck

Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
@sgaist
Copy link
Contributor Author

sgaist commented Feb 15, 2024

I may have found something that could be refactored in a followup PR.

In falco_engine::describe_rule, does an empty pointer and an empty string represent the same thing ? I would say yes since it would not make sense to have a rule indexed at an empty string but it also does not seem to be forbidden.

@sgaist sgaist dismissed stale reviews from incertum and FedeDP via 7d1e379 February 15, 2024 20:18
@poiana poiana removed the lgtm label Feb 15, 2024
@incertum
Copy link
Contributor

I may have found something that could be refactored in a followup PR.

In falco_engine::describe_rule, does an empty pointer and an empty string represent the same thing ? I would say yes since it would not make sense to have a rule indexed at an empty string but it also does not seem to be forbidden.

@jasondellaluce and @mstemm, would you have any insights since you worked a lot on those parts?

Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

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

/approve

@poiana poiana added the lgtm label Feb 15, 2024
@poiana
Copy link

poiana commented Feb 15, 2024

LGTM label has been added.

Git tree hash: 9122969f58f3d490f292659289d553967e3e4e60

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

/approve

@poiana
Copy link

poiana commented Feb 15, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, incertum, sgaist

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@incertum
Copy link
Contributor

/unhold

@poiana poiana merged commit 5e497a4 into falcosecurity:master Feb 15, 2024
28 of 29 checks passed
@sgaist sgaist deleted the 3082_const_correctness branch February 15, 2024 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Fix const correctness
6 participants