From 915da9ae13e157cd5d2be9aedf47ab650fc38c9d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 25 Jun 2025 17:22:23 +0200 Subject: [PATCH] make the bot happy --- esphome/components/api/api_connection.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index dd76725c45..ea604e470e 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -485,6 +485,9 @@ class APIConnection : public APIServerConnection { // Optimized MessageCreator class using tagged pointer class MessageCreator { + // Ensure pointer alignment allows LSB tagging + static_assert(alignof(std::string *) > 1, "String pointer alignment must be > 1 for LSB tagging"); + public: // Constructor for function pointer MessageCreator(MessageCreatorPtr ptr) { @@ -502,14 +505,14 @@ class APIConnection : public APIServerConnection { // Destructor ~MessageCreator() { - if (is_string()) { + if (has_tagged_string_ptr()) { delete get_string_ptr(); } } // Copy constructor MessageCreator(const MessageCreator &other) { - if (other.is_string()) { + if (other.has_tagged_string_ptr()) { auto *str = new std::string(*other.get_string_ptr()); data_.tagged = reinterpret_cast(str) | 1; } else { @@ -524,11 +527,11 @@ class APIConnection : public APIServerConnection { MessageCreator &operator=(const MessageCreator &other) { if (this != &other) { // Clean up current string data if needed - if (is_string()) { + if (has_tagged_string_ptr()) { delete get_string_ptr(); } // Copy new data - if (other.is_string()) { + if (other.has_tagged_string_ptr()) { auto *str = new std::string(*other.get_string_ptr()); data_.tagged = reinterpret_cast(str) | 1; } else { @@ -541,7 +544,7 @@ class APIConnection : public APIServerConnection { MessageCreator &operator=(MessageCreator &&other) noexcept { if (this != &other) { // Clean up current string data if needed - if (is_string()) { + if (has_tagged_string_ptr()) { delete get_string_ptr(); } // Move data @@ -558,7 +561,7 @@ class APIConnection : public APIServerConnection { private: // Check if this contains a string pointer - bool is_string() const { return (data_.tagged & 1) != 0; } + bool has_tagged_string_ptr() const { return (data_.tagged & 1) != 0; } // Get the actual string pointer (clears the tag bit) std::string *get_string_ptr() const { return reinterpret_cast(data_.tagged & ~uintptr_t(1)); }