-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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: "magic numbers" for MQTT topic buffer length in mqtt.cpp and button.cpp (use already defined MQTT_MAX_TOPIC_LEN) #4295
base: main
Are you sure you want to change the base?
Conversation
…d buffer overflows when value is increased
@softhack007 no it should be + 32, see lines ~150+ in if (buttonPublishMqtt && WLED_MQTT_CONNECTED) {
char subuf[MQTT_MAX_TOPIC_LEN + 32];
if (buttonType[b] == BTN_TYPE_PIR_SENSOR) sprintf_P(subuf, PSTR("%s/motion/%d"), mqttDeviceTopic, (int)b);
else sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, !buttonPressedBefore[b] ? "off" : "on");
} Here, it puts Elsewhere, in the same file it doesn't use the literal string static const char _mqtt_topic_button[] PROGMEM = "%s/button/%d"; // optimize flash usage |
I am now noticing other places that use the |
In if (lastEEPROMversion > 8)
{
readStringFromEEPROM(2300, mqttServer, 32);
readStringFromEEPROM(2333, mqttDeviceTopic, 32);
readStringFromEEPROM(2366, mqttGroupTopic, 32);
} Modifying this would corrupt the EEPROM config when upgrading to a version of WLED which has the max topic length increased. It would also mean calculating every hard-coded EEPROM address with an offset that's dependent on |
I think you don't need to worry much about EEPROM - this feature is deprecated, and the code is not active unless I think it would be best to just keep the EEPROM code as it is - just add a warning like this in wled_EEPROM.cpp (after #include "wled.h") #if defined(WLED_ENABLE_MQTT) && MQTT_MAX_TOPIC_LEN > 32
#error "MQTT topics length > 32 is not supported by the EEPROM module!"
#endif |
@softhack007 great! I just saw that the wled_eeprom.cpp file is only used for reading older settings indeed. I don't think a warning is even needed, we can just read it as a hardcoded 32-byte length and put it in the larger I think I'll continue this fix then. Will update the other files that touch mqttDeviceTopic/mqttGroupTopic tomorrow, and I'll resolve the threads about the |
All other references to I've added a comment explaining this, that's also warning against setting the value of
|
I would actually prefer a compile-time #warning added in mqtt.cpp. |
I've replaced the comment with a warning and an error:
Should be ready to merge now! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good to me
code reviewed but not tested from my side, as I don't have a MQTT broker setup at home.
I'd suggest we include this PR into the next feature update after 0.15.0 was released.
Thank you for putting this together, magic numbers are horrible so great to remove them. However I still see some magic numbers within your change. If these could be eliminated as well please E.g MQTT_MAX_TOPIC_LEN + 32 That is better than just 64, but you still have the magic number of 32 so it not really resolved the magic number issue |
I get what you're saying, but IMO this is way less worse than the situation was previously. With these "magic numbers", you see the code that uses it within 10 lines of the addition to the buffer length. You see the mqtt prefix variable, a static string, and a strcpy call to copy them both to the buffer. This is in contrast to the use of I'm also not sure as how to get rid of these effectively, storing the cstring that's added to the topic prefix, and calculating the length before generating a buffer that holds the composite of both strings seems overkill and would add (imo unnecessary) CPU time. |
For what it's worth coming from a first-time collaborator, the code works fine on my end. I have been controlling some WLED lights through MQTT with a build where the max length was increased to 64 using this PR without any weirdness. |
I suggest we merge this and possibly look at a more big-picture removal of magic numbers at a later date. |
It's definitely an improvement
It's definitely an improvement, can you not just define a constant with a nice name and a value of 32 and the same for your magic 6 ? |
See issue #4293.
As seen in
wled.h
,MQTT_MAX_TOPIC_LEN
is currently defined to be32
. All modified lines of code result in the same value by adding a number toMQTT_MAX_TOPIC_LEN
, allowing this variable to be increased without the risk of buffer overflows at a later date or by someone compiling the project with the modification themselves.