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

fix(build): replace {0} zero-init with {} #1731

Merged
merged 1 commit into from
Mar 7, 2024

Conversation

gnosek
Copy link
Contributor

@gnosek gnosek commented Mar 7, 2024

Apparently my compiler (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0) does not like initializing structs with {0} at all.

Honestly, I'm not convinced this is valid C++ either, though I'm pretty sure it is valid C. E.g. this page:
https://en.cppreference.com/w/cpp/language/zero_initialization does not mention {0} as a valid way to zero-initialize a struct.

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

Any specific area of the project related to this PR?

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

/area API-version

/area build

/area CI

/area driver-kmod

/area driver-bpf

/area driver-modern-bpf

/area libscap-engine-bpf

/area libscap-engine-gvisor

/area libscap-engine-kmod

/area libscap-engine-modern-bpf

/area libscap-engine-nodriver

/area libscap-engine-noop

/area libscap-engine-source-plugin

/area libscap-engine-savefile

/area libscap

/area libpman

/area libsinsp

/area tests

/area proposals

Does this PR require a change in the driver versions?

/version driver-API-version-major

/version driver-API-version-minor

/version driver-API-version-patch

/version driver-SCHEMA-version-major

/version driver-SCHEMA-version-minor

/version driver-SCHEMA-version-patch

What this PR does / why we need it:

Not sure how it works for everybody else, but I'm getting tons of build errors for struct initialization that does seem off.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Apparently my compiler (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0)
does not like initializing structs with {0} at all.

Honestly, I'm not convinced this is valid C++ either, though I'm
pretty sure it is valid C. E.g. this page:
https://en.cppreference.com/w/cpp/language/zero_initialization
does not mention {0} as a valid way to zero-initialize a struct.

Signed-off-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
@gnosek
Copy link
Contributor Author

gnosek commented Mar 7, 2024

@federico-sysdig please take a look

Copy link
Member

@Andreagit97 Andreagit97 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 Mar 7, 2024

LGTM label has been added.

Git tree hash: c452a825fecf240f8bed3bc648a14c9a35ecd873

@Andreagit97 Andreagit97 added this to the 0.15.0 milestone Mar 7, 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 poiana merged commit 83f1529 into falcosecurity:master Mar 7, 2024
41 checks passed
@gnosek gnosek deleted the fix-struct-zero-init branch March 7, 2024 10:40
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.

LGTM. Great change!
To my surprise, the empty braces syntax does indeed initialize all struct members without a default constructor (such as built-in integers) to zero. Note that this should only apply to C++, not C.
https://en.cppreference.com/w/cpp/language/zero_initialization
I just had to "paranoidly" see it live for myself. For the curious ones, here's the code:

#include <iostream>
#include <cstring>

struct widget
{
  int val1;
  int val2;
  short val3;
  int val4;
};

int main()
{
  char buffer1[sizeof(widget)];
  char buffer2[sizeof(widget)];
  memset(buffer1, 1, sizeof(buffer1));
  memset(buffer2, 1, sizeof(buffer2));

  widget* ptr1 = new (buffer1)widget;
  widget* ptr2 = new (buffer2)widget{}; // the magic is here

  std::cout << ptr1->val1 << " - "
            << ptr1->val2 << " - "
            << ptr1->val3 << " - "
            << ptr1->val4 << '\n';

  std::cout << ptr2->val1 << " - "
            << ptr2->val2 << " - "
            << ptr2->val3 << " - "
            << ptr2->val4 << '\n';
}

Possible output:

16843009 - 16843009 - 257 - 16843009
0 - 0 - 0 - 0

NOTE: I have no idea how your compilation error was never detected. It's another surprise that this code never went through a gcc version similar to yours.

@poiana
Copy link
Contributor

poiana commented Mar 7, 2024

@federico-sysdig: changing LGTM is restricted to collaborators

In response to this:

LGTM. Great change!
To my surprise, the empty braces syntax does indeed initialize all struct members without a default constructor (such as built-in integers) to zero.
https://en.cppreference.com/w/cpp/language/zero_initialization
I just had to "paranoidly" see it live for myself. For the curious ones, here's the code:

#include <iostream>
#include <cstring>

struct widget
{
 int val1;
 int val2;
 short val3;
 int val4;
};

int main()
{
 char buffer1[sizeof(widget)];
 char buffer2[sizeof(widget)];
 memset(buffer1, 1, sizeof(buffer1));
 memset(buffer2, 1, sizeof(buffer2));

 widget* ptr1 = new (buffer1)widget;
 widget* ptr2 = new (buffer2)widget{};

 std::cout << ptr1->val1 << " - "
           << ptr1->val2 << " - "
           << ptr1->val3 << " - "
           << ptr1->val4 << '\n';

 std::cout << ptr2->val1 << " - "
           << ptr2->val2 << " - "
           << ptr2->val3 << " - "
           << ptr2->val4 << '\n';
}

Possible output:

16843009 - 16843009 - 257 - 16843009
0 - 0 - 0 - 0

NOTE: I have no idea how your compilation error was never detected. It's another surprise that this code never went through a gcc version similar to yours.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@poiana
Copy link
Contributor

poiana commented Mar 7, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP, federico-sysdig, gnosek

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:
  • OWNERS [Andreagit97,FedeDP,gnosek]

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

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.

5 participants