Compare commits

...
5 changed files with 17 additions and 23 deletions

No files matched your search

+3 -8
View File
@@ -230,15 +230,10 @@ struct LogBuffer {
return p;
}
// Copies str unchecked and without its null terminator, so the caller reserves space; returns the new end
static char *copy_string(char *p, const char *str) {
static inline char *ESPHOME_ALWAYS_INLINE copy_string(char *p, const char *str) {
#ifdef USE_ESP8266
// Only ESP8266 caps the length; tags may be in PROGMEM, so read each byte once
for (size_t i = 0; i < MAX_TAG_LENGTH; i++) {
const char c = static_cast<char>(progmem_read_byte(reinterpret_cast<const uint8_t *>(str + i)));
if (c == '\0')
break;
*p++ = c;
}
// Only ESP8266 caps the length; tags may be in PROGMEM
p = progmem_copy_str(p, p + MAX_TAG_LENGTH, str);
#else
const size_t len = strlen(str);
memcpy(p, str, len); // NOLINT(bugprone-not-null-terminated-result) - the header is built piece by piece
+1 -1
View File
@@ -532,7 +532,7 @@ class LoggerMessageTrigger final : public Trigger<uint8_t, const char *, const c
#ifdef USE_ESP8266
// User lambdas may strcmp the tag, which may be in PROGMEM. The copy lives in
// the trigger so an automation that suspends (delay) still sees a valid tag.
ESPHOME_strncpy_P(trigger->ram_tag_, tag, MAX_TAG_LENGTH);
buf_append_str_p(trigger->ram_tag_, sizeof(trigger->ram_tag_), 0, tag);
tag = trigger->ram_tag_;
#endif
trigger->trigger(level, tag, message);
+2 -3
View File
@@ -168,9 +168,8 @@ bool MQTTComponent::publish(const char *topic, ProgmemStr payload) {
return false;
// On ESP8266, ProgmemStr is __FlashStringHelper* - need to copy from flash
char buf[64];
strncpy_P(buf, reinterpret_cast<const char *>(payload), sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
return global_mqtt_client->publish(topic, buf, strlen(buf), this->qos_, this->retain_);
const size_t len = buf_append_str_p(buf, sizeof(buf), 0, reinterpret_cast<const char *>(payload));
return global_mqtt_client->publish(topic, buf, len, this->qos_, this->retain_);
}
#endif
+10
View File
@@ -27,6 +27,16 @@ std::string progmem_string(ProgmemStr str) {
memcpy_P(result.data(), src, result.size());
return result;
}
// Out of line: progmem_copy_str() is forced inline, and this has many callers
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str) {
if (pos >= size) {
return size;
}
char *end = progmem_copy_str(buf + pos, buf + size - 1, str); // reserve space for null terminator
*end = '\0';
return end - buf;
}
#endif
ESPHOME_LOG_TAG(TAG, "helpers");
+1 -11
View File
@@ -1518,17 +1518,7 @@ inline char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end,
/// @param str PROGMEM-resident string to append (must not be null)
/// @return New position after appending; returns `size` if `pos >= size`, otherwise
/// returns at most `size - 1` because one byte is reserved for the null terminator
inline size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str) {
if (pos >= size) {
return size;
}
size_t remaining = size - pos - 1; // reserve space for null terminator
size_t len = strnlen_P(str, remaining);
memcpy_P(buf + pos, str, len);
pos += len;
buf[pos] = '\0';
return pos;
}
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str);
/// Safely append a string to buffer, returning new position (capped at size).
/// More efficient than buf_append_printf for plain string literals.
/// On ESP8266 the literal is wrapped with PSTR() so it stays in flash.