Skip to content

Commit

Permalink
Fix Print and File incompatible writes w/casts
Browse files Browse the repository at this point in the history
Print and File have ambiguous resolutions for single-argument
write(0)s.

Fix by adding explicit methods.  A write of any integer will not be a
const char* write (i.e. won't write a string) but will instead just
write the integer truncated to 8 bits (as makes sense).
  • Loading branch information
earlephilhower committed Mar 10, 2019
1 parent d3a7556 commit 77eca65
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class File : public Stream
// Print methods:
size_t write(uint8_t) override;
size_t write(const uint8_t *buf, size_t size) override;
size_t write(int8_t t) { return write((uint8_t)t); }
size_t write(int16_t t) { return write((uint8_t)(t & 0xff)); }
size_t write(int32_t t) { return write((uint8_t)(t & 0xff)); }
size_t write(uint16_t t) { return write((uint8_t)(t & 0xff)); }
size_t write(uint32_t t) { return write((uint8_t)(t & 0xff)); }

// Stream methods:
int available() override;
Expand Down
8 changes: 8 additions & 0 deletions tests/host/fs/test_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,12 @@ TEST_CASE("Multisplendored File::writes", "[fs]")
f.write((const uint8_t*)&bigone, 4);
f.close();
REQUIRE(readFileSD("/file.txt") == "aAbbcctheendxyz@@@@");
File g = SD.open("/file.txt", FILE_WRITE);
g.write(0);
g.close();
g = SD.open("/file.txt", FILE_READ);
uint8_t u = 0x66;
g.read(&u, 1);
g.close();
REQUIRE(u == 0);
}

0 comments on commit 77eca65

Please sign in to comment.