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 class initialization #3074

Merged
merged 4 commits into from
Feb 12, 2024

Conversation

sgaist
Copy link
Contributor

@sgaist sgaist commented Feb 10, 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 member variables initialization so that they are done in the constructor initialization list.

They were detected by cppcheck.

Which issue(s) this PR fixes:

Fixes #3073

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

incertum
incertum previously approved these changes Feb 11, 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

Yes this is also in line with some feedback @federico-sysdig gave in some libs PRs.

@poiana
Copy link
Contributor

poiana commented Feb 11, 2024

LGTM label has been added.

Git tree hash: bba4873503e26fad06b18b25ae3b2f332aa9f89e

@incertum
Copy link
Contributor

/unhold

Copy link
Contributor

@federico-sysdig federico-sysdig left a comment

Choose a reason for hiding this comment

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

I really like these kinds of cleanups. I left a few comments on how it can be done to further simplify the implementation.

userspace/engine/falco_common.h Outdated Show resolved Hide resolved
Comment on lines 52 to 59
: falco_version(FALCO_VERSION)
, engine_version(FALCO_ENGINE_VERSION)
, libs_version(FALCOSECURITY_LIBS_VERSION)
, plugin_api_version(inspector->get_plugin_api_version())
, driver_api_version(get_driver_api_version(inspector))
, driver_schema_version(get_driver_schema_version(inspector))
, default_driver_version(DRIVER_VERSION)

{
falco_version = FALCO_VERSION;
engine_version = FALCO_ENGINE_VERSION;
libs_version = FALCOSECURITY_LIBS_VERSION;
plugin_api_version = inspector->get_plugin_api_version();
driver_api_version = get_driver_api_version(inspector);
driver_schema_version = get_driver_schema_version(inspector);
default_driver_version = DRIVER_VERSION;
Copy link
Contributor

@federico-sysdig federico-sysdig Feb 11, 2024

Choose a reason for hiding this comment

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

Here 4 data members are initialized to a constant, thus they could/should have been initialized right at the declaration site (falco_version, engine_version, libs_version, and default_driver_version).
Note that the 4 SMF's, copy/move constructors + copy/move assignments, are generated by the compiler and an explicit default definition can/should be avoided.

    struct versions_info
    {
        /**
         * @brief Construct a versions info by using an inspector to obtain
         * versions about the drivers and the loaded plugins.
         */
        explicit versions_info(const std::shared_ptr<sinsp>&);

        /**
         * @brief Encode the versions info as a JSON object
         */
        nlohmann::json as_json() const;

        std::string falco_version = FALCO_VERSION;
        std::string engine_version = FALCO_ENGINE_VERSION;
        std::string libs_version = FALCOSECURITY_LIBS_VERSION;
        std::string plugin_api_version;
        std::string driver_api_version;
        std::string driver_schema_version;
        std::string default_driver_version = DRIVER_VERSION;
        std::unordered_map<std::string, std::string> plugin_versions;
    };

Note:
This is not related to this PR, but passing objects by shared_ptr when only the object is used, as we do for inspector in the constructor of versions_info, is overkill and should be avoided by passing a reference or a raw pointer. In this case the need comes from the two functions get_driver_api_version and get_driver_schema_version which take inspector as a smart pointer, again, without any real need to do so. Again, overkill for readability and, sometimes, performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, would this something to be done in a follow up PR ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, would this something to be done in a follow up PR ?

The note on the changing the parameter shared_ptr<sinsp> to a reference or a pointer? Yes, I'd say it can be addressed later.

Comment on lines 172 to 174
: m_initialized(false), m_total_samples(0)
: m_initialized(false)
, m_total_samples(0)
, m_config(config)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here, m_initialized and m_total_samples can be more simply initialized at the declaration site.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we have a PR that does this on a more global scale ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps, but you can get warmed up here, if you care.

@FedeDP
Copy link
Contributor

FedeDP commented Feb 12, 2024

/milestone 0.38.0

@poiana poiana added this to the 0.38.0 milestone Feb 12, 2024
@sgaist sgaist force-pushed the 3073_improve_initialization branch from 99b6571 to 3a021d1 Compare February 12, 2024 13:57
FedeDP
FedeDP previously approved these changes Feb 12, 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
Contributor

poiana commented Feb 12, 2024

LGTM label has been added.

Git tree hash: 11f862a17bdc9e18fd9eb9c62a37538613199ccf

…n list

Reported by cppcheck

Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>

# Conflicts:
#	userspace/engine/falco_common.h
The implementation provides more or less the same implementation
and thus it makes more sense to base it on std::runtime_error.

Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
@sgaist sgaist force-pushed the 3073_improve_initialization branch from 3a021d1 to 2b76feb Compare February 12, 2024 16:05
@poiana poiana removed the lgtm label Feb 12, 2024
@poiana poiana requested a review from FedeDP February 12, 2024 16:05
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 poiana added the lgtm label Feb 12, 2024
@poiana
Copy link
Contributor

poiana commented Feb 12, 2024

LGTM label has been added.

Git tree hash: 9df54a0b87e02fa96709e32dd3608a7d3f3525d0

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
Copy link
Contributor

poiana commented Feb 12, 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

@poiana poiana merged commit 8c98ca5 into falcosecurity:master Feb 12, 2024
29 checks passed
@sgaist sgaist deleted the 3073_improve_initialization branch February 12, 2024 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Improve initialization
5 participants