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 minor build errors, such as unnecessary copies from range enumeration. #775

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

r12f
Copy link
Contributor

@r12f r12f commented Apr 18, 2023

Problem

When building on ubuntu 22.04 with gcc 11.3, I am hitting a few minor errors during build: copies from for range enumeration and uninitialized variable before use.

More details are attached as below:

For range enumerations

common/events.cpp: In member function ‘int EventSubscriber::init(bool, int, const event_subscribe_sources_t*)’:
common/events.cpp:368:25: error: loop variable ‘e’ creates a copy from type ‘const std::__cxx11::basic_string<char>’ [-Werror=range-loop-construct]
  368 |         for (const auto e: *subs_sources) {
      |                         ^
common/events.cpp:368:25: note: use reference type to prevent copying
  368 |         for (const auto e: *subs_sources) {
      |                         ^
      |                         &
tests/events_ut.cpp: In function ‘std::string parse_read_evt(std::string&, internal_event_t&, std::string&, sequence_t&, std::string&, event_params_t&)’:
tests/events_ut.cpp:140:21: error: loop variable ‘e’ creates a copy from type ‘const std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >’ [-Werror=range-loop-construct]
  140 |     for (const auto e: evt) {
      |                     ^
tests/events_ut.cpp:140:21: note: use reference type to prevent copying
  140 |     for (const auto e: evt) {
      |                     ^
      |                     &

Uninitialized variable

tests/stringutility_ut.cpp: In member function ‘virtual void STRINGUTILITY_hex_to_binary_Test::TestBody()’:
tests/stringutility_ut.cpp:79:36: error: ‘a’ may be used uninitialized [-Werror=maybe-uninitialized]
   79 |     EXPECT_TRUE(swss::hex_to_binary("01020aff05", a.data(), a.size()));
      |                 ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/tuple:39,
                 from /usr/include/c++/11/bits/stl_map.h:63,
                 from /usr/include/c++/11/map:61,
                 from ./common/logger.h:6,
                 from ./common/stringutility.h:3,
                 from tests/stringutility_ut.cpp:1:
/usr/include/c++/11/array:176:7: note: by argument 1 of type ‘const std::array<unsigned char, 5>*’ to ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = unsigned char; long unsigned int _Nm = 5]’ declared here
  176 |       size() const noexcept { return _Nm; }
      |       ^~~~
tests/stringutility_ut.cpp:77:33: note: ‘a’ declared here
   77 |     std::array<std::uint8_t, 5> a;
      |                                 ^

Fix

Changed range from value to reference as compiler suggested and initialized the uninitialized array as 0.

@r12f r12f force-pushed the user/r12f/fix-build branch from 9d455df to fe9dcae Compare June 9, 2023 01:05
@r12f r12f force-pushed the user/r12f/fix-build branch from fe9dcae to d1aa4a3 Compare June 9, 2023 01:07
.gitignore Outdated Show resolved Hide resolved
@bingwang-ms
Copy link
Contributor

@prsunny Please help review this change. Thanks!

@prsunny prsunny requested a review from qiluo-msft June 15, 2023 21:12
Copy link
Contributor

@prsunny prsunny left a comment

Choose a reason for hiding this comment

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

lgtm

@@ -590,7 +591,8 @@ void do_test_subscribe(bool wrap)
hsub = events_init_subscriber_wrap(true, 100);
}
else {
hsub = events_init_subscriber(true, 100);
Copy link
Contributor

Choose a reason for hiding this comment

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

events_init_subscriber

You do not mention the reason of this change anywhere. Seems like it is not necessary.

Copy link
Contributor

@qiluo-msft qiluo-msft left a comment

Choose a reason for hiding this comment

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

As comment

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.

4 participants