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

replacing few memset usages with safer alternatives #2037

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

mikea
Copy link
Collaborator

@mikea mikea commented May 14, 2024

I focused on most trivial usages first

@mikea mikea force-pushed the maizatskyi/2024-05-14-tidy branch from 5a0de5a to 7db059b Compare May 14, 2024 17:50
@mikea mikea force-pushed the maizatskyi/2024-05-14-tidy branch from 7db059b to d556aa6 Compare May 14, 2024 17:55
@mikea mikea requested review from harrishancock, kentonv and jasnell and removed request for harrishancock May 14, 2024 17:55
@mikea mikea marked this pull request as ready for review May 14, 2024 17:55
@@ -1704,8 +1704,7 @@ public:

// Evaluate this schema to a DynamicValue.
DynamicValue::Reader value;
word zeroWord[1];
memset(&zeroWord, 0, sizeof(zeroWord));
word zeroWord[1]{};
Copy link
Member

Choose a reason for hiding this comment

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

Does this actually zero-initialize given the unusual way class word is defined?

Copy link
Member

Choose a reason for hiding this comment

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

Even if it does... I wonder if this ends up violating aliasing rules. Word arrays are never actually accessed using the type word, but rather by reinterpret-casting to some other pointer type and reading that. Technically it is UB to write type word and then read as some other non-byte-sized type, or vice versa. There's an exception for byte-oriented access, which is allowed to alias everything; memset() operates on bytes, and so doesn't break aliasing rules.

Unsure to what extent any of this matters in practice.

Copy link
Collaborator Author

@mikea mikea May 15, 2024

Choose a reason for hiding this comment

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

Does this actually zero-initialize given the unusual way class word is defined?

yes, I was curious about this as well: https://godbolt.org/z/Wcsn99WKc

Technically it is UB to write type word and then read as some other non-byte-sized type, or vice versa.

Not sure if this matters, but we don't write to word here, but default-initialize it?
Is there a special compiler mode that could check this for us?

Unsure to what extent any of this matters in practice.

I could use kj::memzero() for word arrays instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Compilers claim to have some aliasing analysis but I wasn't able to trigger it, did I break the rules here: https://godbolt.org/z/WrrG9jres ?

Copy link
Member

Choose a reason for hiding this comment

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

That does appear to break the rules, not sure why it isn't detected. My understanding is that the compiler could legally re-order the assignment to buf2->content vs. f->d.

@mikea mikea requested a review from kentonv May 15, 2024 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants