Skip to content

Commit

Permalink
StreamConstPtr: disallow passing a String temporary (#8410)
Browse files Browse the repository at this point in the history
* StreamConstPtr: prevent from passing a temporary String instance
* unconditionally allow progmem chars
* missing virtual destructor in Stream
(warning: deleting object of abstract class type 'Stream' which has non-virtual destructor will cause undefined behavior [-Wdelete-non-virtual-dtor])
  • Loading branch information
d-a-v authored Jan 3, 2022
1 parent e3c79de commit dde2c76
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions cores/esp8266/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Stream: public Print {
virtual int peek() = 0;

Stream() {}
virtual ~Stream() {}

// parsing methods

Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/StreamDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class StreamConstPtr: public StreamNull
size_t _peekPointer = 0;

public:
StreamConstPtr(const String&& string) = delete; // prevents passing String temporary, use ctor(buffer,size) if you know what you are doing
StreamConstPtr(const String& string): _buffer(string.c_str()), _size(string.length()), _byteAddressable(true) { }
StreamConstPtr(const char* buffer, size_t size): _buffer(buffer), _size(size), _byteAddressable(__byteAddressable(buffer)) { }
StreamConstPtr(const uint8_t* buffer, size_t size): _buffer((const char*)buffer), _size(size), _byteAddressable(__byteAddressable(buffer)) { }
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/StreamSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Stream& operator << (Stream& out, Stream& stream)

Stream& operator << (Stream& out, const char* text)
{
StreamConstPtr(text).sendAll(out);
StreamConstPtr(text, strlen_P(text)).sendAll(out);
return out;
}

Expand Down

0 comments on commit dde2c76

Please sign in to comment.