-
Notifications
You must be signed in to change notification settings - Fork 836
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
Reduce build warnings ( [-Wmissing-field-initializers] ) #1699
Comments
@henrygab what build environment are you running, and why there were no warnings in CI? Code initializes the struct regardless, so it is sort-of a non issue, I am just a bit confused by the conclusions here :) c/p into godbolt does not produce anything of sorts (also notice memset at the start that disappears without the braces) afaik POD structs are slightly different from normal classes in cases like this and nothing is done to the members without default value that we provided ourselves (i.e. |
Hi @mcspr, First, yes you are correct ... this is sort-of a non-issue. The old code should have had correct behavior, because it was The build environment that was used:PlatformIO, with a project that used arduino_core_2_7_4 / [email protected] to compile for ESP8266 boards. MotivationsI reported this because it produced warnings during build. I am not defending the decision to report this as a warning, since I cannot fathom the reasoning behind it. I've seen GCC add warnings for things that are bad habits or risky, even if not technically wrong. I didn't look into whether there is such a justification here. I filed this issue to raise awareness, and provide the opportunity to review. Maybe the fix won't happen ... in the end, that's not my decision. At the same time, I wry to provide information to help make an informed decision. I'm comfortable with the likelihood that I won't always agree with those decisions. 🙂 Let me know if there's more information I can provide.... |
@mcspr -- Max, After digging deeper, I think GCC got it wrong. Brace-initialization should not have caused an error, even with pedantic warnings enabled. Worse, I think you were right ... this doesn't zero-initialize the fields. I was overly trusting in GCC, and didn't dig deeply enough. The PR I proposed (and that was merged) may cause some uninitialized memory to be returned. This is "not good". I will immediately create a PR to revert this change. I will line to this issue (and the original PR). THANK YOU for asking the questions when something didn't seem right. My brain should also have flagged this. Mea culpa! P.S. - thanks for helping me notice that godbolt added the ESP32 compilers. That's a great addition to what was already an invaluable tool! |
FWIW, a "better" (IMHO) solution would be use this instead: stdAc::state_t result;
or we create a function that returns a safely (default) initialised either one of those and then remove the redundant/duplicate setting of default values later in the method. Or ... use: Lines 78 to 110 in 31432b4
e.g. stdAc::state_t result = stdAc::initState(&result, decode_type_t::KELON, -1, (prev == nullptr || prev->power) ^ _.PowerToggle, toCommonMode(getMode()), getTemp(), true, ... etc etc); |
At this point, why not implement // header
struct state_t {
...
state_t();
};
// cpp
state_t::state_t() :
vendor(decode_type_t::UNKNOWN),
model(-1),
power(false),
...
{} Or just resort to the header, but idk how would you would feel about keeping defaults there instead as part of the implementation // generate default constructor that will set these
struct state_t {
decode_type_t vendor = decode_type_t::UNKNOWN;
int16_t model = -1;
bool power = false;
opmode_t = opmode_t::kOff;
... etc ...
};
// designated init, just name things as they are
state_t state {
.vendor = decode_type_t::Blah,
.model = my_ac_model_const;
};
// state.power is still false by default initState Method way may also work, but now we have to track both method and the struct. Plus, there are a lot of fields, and it is harder to track at a glance without using some kind of editor extension that would show the function signature? Looking at the GCC manual,
struct s { int f, g, h; };
struct s x = { 3, 4 };
struct s { int f, g, h; };
s x = { }; I am able to see the issue with the GCC 4.8.2 that is used by the 2.7.4 esp8266 Core, so it seems just a mis-interpretation of the C vs. C++ rules that is fixed in the later versions of the compiler. Core 3.0.2 uses GCC 10.3 (CI examples build), and I am running GCC 11.2.1 locally (why I haven't seen the issue when trying to run tests)
|
Honestly, picking the best C++ approach/idiom is beyond my skill level. E.g. this project was my first C++ code. Heck, my My preference is that all |
Ref this and #1705, we should add a warning disable for that line, if we all agree the warning is incorrect, which I think we are. e.g. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
stdAc::state_t result{};
#pragma GCC diagnostic pop |
I just noticed that replacing Regarding init, my personal preference would be to just have the default-generated constructor and assign edit:
This is a wonderful "set of guidelines for using C++ well. " |
Awesome! See https://godbolt.org/z/7d9P5aMGx. This is the only change that can really be made without fundamentally changing some properties of the struct. I was unable to find any change by removing the If there is agreement, I can further update the PR to have this change instead? |
I'm happy as long as we are not adding bloat in the executable. As I said, this is above my experience level, so I'm happy to go with which ever option you two are happy with. |
Yes, please do I could take a look at the ctors since those are a whole separate thing, but only around ~monday or so. typedef struct -> struct should be harmless though |
The ctors are not required. Keeping as a POD type is OK, imho. PR is updated with removal of 'typedef'. |
* Revert PR #1700 as intialisation/zero is better with `{}`. * Address compiler warning via removing `typedef` * Adjust indention to fix linter issues. For #1699 Co-authored-by: David Conran <[email protected]>
@mcspr Were you going to do something or should we just mark this issue as fixed for now? |
_v2.8.1 (20220101)_ **[Bug Fixes]** - Arduino ESP32 Core v2.0.2+ crashes due to our timer hack. (#1715 #1715) - SONY: Fix old Sony CD-Player Remote (12 Bit) (#1714) **[Features]** - Add tool to convert protocol & code to raw timing info. (#1708 #1707 #1703) - Add basic support for COOLIX48 protocol. (#1697 #1694) - MITSUBISHI_AC: Added support for i-SAVE mode. (#1666) - TOSHIBA_AC: Add Filter setting support. aka. Pure. (#1693 #1692) - Airton: Add detailed A/C support. (#1688 #1670) **[Misc]** - Add a structured library version number. (#1717) - Workflows Split UnitTests (#1712) - Reduce time for workflow/Build (#1709) - Fix some compiler & linter warnings (#1699 #1700) - Fujitsu: Update supported A/C models (#1690 #1689 #1702 #1701) - Remove extra `const` qualifier for char pointer (#1704) - TCL: Update supported devices. (#1698) - ESP32-C3: Work around for some C3 specific compiler issues. (#1696 #1695)
## _v2.8.1 (20220101)_ **[Bug Fixes]** - Arduino ESP32 Core v2.0.2+ crashes due to our timer hack. (#1715 #1713 ) - SONY: Fix old Sony CD-Player Remote (12 Bit) (#1714) **[Features]** - Add tool to convert protocol & code to raw timing info. (#1708 #1707 #1703) - Add basic support for COOLIX48 protocol. (#1697 #1694) - MITSUBISHI_AC: Added support for i-SAVE mode. (#1666) - TOSHIBA_AC: Add Filter setting support. aka. Pure. (#1693 #1692) - Airton: Add detailed A/C support. (#1688 #1670) **[Misc]** - Add a structured library version number. (#1717) - Workflows Split UnitTests (#1712) - Reduce time for workflow/Build (#1709) - Fix some compiler & linter warnings (#1699 #1700) - Fujitsu: Update supported A/C models (#1690 #1689 #1702 #1701) - Remove extra `const` qualifier for char pointer (#1704) - TCL: Update supported devices. (#1698) - ESP32-C3: Work around for some C3 specific compiler issues. (#1696 #1695)
FYI, the committed changes above have now been included in the new v2.8.1 release of the library. |
* Ensure all `state_t` structs are initialised. * Remove (now) redundant code. * Add unit tests to confirm operation. Ref #1699
* Ensure all `state_t` structs are initialised. * Remove (now) redundant code. * Add unit tests to confirm operation. Ref #1699
Added default initialisation. |
_v2.8.2 (20220314)_ **[Bug Fixes]** - ESP32-C3: Fix reboot/crashes on ESP32-C3s when receiving. (#1768 #1751) **[Features]** - HITACHI_AC296: Add `IRac` class support & tests. (#1776 #1758 #1757) - Support for Hitachi RAS-70YHA3 (remote RAR-3U3) (#1758 #1757) - LG: Add Swing Toggle support for Model `LG6711A20083V` (#1771 #1770) - IRMQTTServer: add `MQTT_SERVER_AUTODETECT_ENABLE` via mqtt mDNS (#1769) - Experimental basic support for Kelon 168 bit / 21 byte protocol. (#1747 #1745 #1744) - MitsubishiAC: Tweak repeat gap timing. (#1760 #1759) - Gree YAP0F8 (Detected as Kelvinator) vertical position set support (#1756) - Make KELON (48 bit) protocol decoding stricter. (#1746 #1744) - IRMQTTServer V1.6.1 (#1740 #1739 #1729) - HITACHI_AC264: Add minimal detailed support. (#1735 #1729) - LG2: Improve Light toggle msg handling. (#1738 #1737) - MIDEA: Add support for Quiet, Clean & Freeze Protect controls. (#1734 #1733) - Add basic support for HITACHI_AC264 264bit protocol. (#1730 #1729) - ESP32-C3: Work around for some C3 specific compiler issues again. (#1732 #1695) **[Misc]** - MIDEA: Update supported devices (#1774 #1773 #1716) - Update devices supported by ELECTRA_AC (#1766 #1765) - Improve documentation for `encodePioneer()` (#1761 #1749) - Update (un)supported DAIKIN128 devices. (#1752) - Refactor `decodeCOOLIX()` code & add another test case. (#1750 #1748) - Simplify code based on state_t being initialised by default. (#1736 #1699) - Add comments to help Teknopoint users. (#1731 #1728) - Fix library version string calculation. (#1727 #1725) - Confirm we can reproduce `TurnOnFujitsuAC.ino` via IRac/IRMQTTServer. (#1726 #1701)
##_v2.8.2 (20220314)_ **[Bug Fixes]** - ESP32-C3: Fix reboot/crashes on ESP32-C3s when receiving. (#1768 #1751) **[Features]** - HITACHI_AC296: Add `IRac` class support & tests. (#1776 #1758 #1757) - Support for Hitachi RAS-70YHA3 (remote RAR-3U3) (#1758 #1757) - LG: Add Swing Toggle support for Model `LG6711A20083V` (#1771 #1770) - IRMQTTServer: add `MQTT_SERVER_AUTODETECT_ENABLE` via mqtt mDNS (#1769) - Experimental basic support for Kelon 168 bit / 21 byte protocol. (#1747 #1745 #1744) - MitsubishiAC: Tweak repeat gap timing. (#1760 #1759) - Gree YAP0F8 (Detected as Kelvinator) vertical position set support (#1756) - Make KELON (48 bit) protocol decoding stricter. (#1746 #1744) - IRMQTTServer V1.6.1 (#1740 #1739 #1729) - HITACHI_AC264: Add minimal detailed support. (#1735 #1729) - LG2: Improve Light toggle msg handling. (#1738 #1737) - MIDEA: Add support for Quiet, Clean & Freeze Protect controls. (#1734 #1733) - Add basic support for HITACHI_AC264 264bit protocol. (#1730 #1729) - ESP32-C3: Work around for some C3 specific compiler issues again. (#1732 #1695) **[Misc]** - MIDEA: Update supported devices (#1774 #1773 #1716) - Update devices supported by ELECTRA_AC (#1766 #1765) - Improve documentation for `encodePioneer()` (#1761 #1749) - Update (un)supported DAIKIN128 devices. (#1752) - Refactor `decodeCOOLIX()` code & add another test case. (#1750 #1748) - Simplify code based on state_t being initialised by default. (#1736 #1699) - Add comments to help Teknopoint users. (#1731 #1728) - Fix library version string calculation. (#1727 #1725) - Confirm we can reproduce `TurnOnFujitsuAC.ino` via IRac/IRMQTTServer. (#1726 #1701)
FYI, the changes mentioned above have now been included in the new v2.8.2 release of the library. |
Version/revision of the library used
v2.8.0
Describe the bug
Build warnings caused by initialization of a structure without the requisite fields in ir_Kelon.cpp line 443.
To Reproduce
Use the library. Enable all warnings in GCC via
-Wall -Wextra
.Actual results
Collapsed simplified build warnings
Example code used
Any code using v2.8.0 of this library, when set to compile with all warnings, should expose this bug.
Expected behaviour
Libraries should build cleanly at all warning levels.
Output of raw data from [IRrecvDumpV2.ino]
N/A
What brand/model IR demodulator are you using?
N/A
Circuit diagram and hardware used (if applicable)
N/A
I have followed the steps in the Troubleshooting Guide & read the FAQ
Yes ... not applicable to this type of bug
Has this library/code previously worked as expected for you?
N/A ... first work to integrate this.
Other useful information
Proposed fix:
Just use the default constructor for
stdAc::state_t
:stdAc::state_t result{};
Becomes:
stdAc::state_t result;
This appears to have the correct results. The default constructor appears to zero-initialize all fields in the structure. Each of the embedded
enum
types, when set to zero, appears to have a reasonable value. And, the result is substantially identical to the expressed intent of the writer of this code.I'll prep a PR for this....
The text was updated successfully, but these errors were encountered: