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(stream): avoid using designated initializer #1247

Merged
merged 1 commit into from
May 19, 2023

Conversation

Abhra303
Copy link
Contributor

Following the comment, a warning is popping up while building the stream_family.cc file.

[19/37] Building CXX object src/server/CMakeFiles/dragonfly_lib.dir/stream_family.cc.o
/home/roy/work/dragonfly/src/server/stream_family.cc: In function ‘facade::OpResult<streamID> dfly::{anonymous}::OpAdd(const dfly::OpArgs&, std::string_view, const dfly::{anonymous}::AddOpts&, facade::CmdArgList)’:
/home/roy/work/dragonfly/src/server/stream_family.cc:543:5: warning: missing initializer for member ‘streamAddTrimArgs::id’ [-Wmissing-field-initializers]
  543 |     };
      |     ^
/home/roy/work/dragonfly/src/server/stream_family.cc:543:5: warning: missing initializer for member ‘streamAddTrimArgs::id_given’ [-Wmissing-field-initializers]
/home/roy/work/dragonfly/src/server/stream_family.cc:543:5: warning: missing initializer for member ‘streamAddTrimArgs::seq_given’ [-Wmissing-field-initializers]
[...]

It is because a designated initializer is used to initialize a streamAddTrimArgs type variable. The -Wmissing-field-initializers throws a warning if all the struct fields are not initialized while using a designated initializer.

In our case, we don't need to set other fields as they are mainly used for redis's internal xadd implementation specific fields and are not used in streamTrim function.

.approx_trim = opts.trim_approx,
.limit = opts.limit,
};
streamAddTrimArgs add_args;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still uninitialized, you should add this -

Suggested change
streamAddTrimArgs add_args;
streamAddTrimArgs add_args = {0};

C++ is a bit weird sometimes ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, but I am unsure - doesn't C++ automatically set struct field to empty values?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's complicated... to be technical, the trivial default constructor leaves integer members uninitialized. Adding '= {0}' makes it into a zero initialization so you get all zeros instead of random stack memory.

(obligatory link to https://twitter.com/i/status/1004017362381795329)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-)

Signed-off-by: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>
@dranikpg
Copy link
Contributor

By the way, designated initializers are not part of the standard (until 20) 🤓, but are supported by most compilers. I actually wanted to leave a comment on this case, but checked other places and saw that we use them from time to time.

@romange romange merged commit dc50db3 into dragonflydb:main May 19, 2023
6 checks passed
romange pushed a commit that referenced this pull request Jun 1, 2023
Signed-off-by: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>
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

4 participants