Skip to content

Commit 56107fb

Browse files
authored
Properly handle u8 pointers when assigning and comparing (#8818)
* simplify ctors and operator=, use a common code paths instead of special handling here and there * fix u8->u32 casts, copy before using u8 data * do not use raw_address() internally
1 parent 41c2be2 commit 56107fb

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

cores/esp8266/IPAddress.cpp

+19-20
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,14 @@ bool IPAddress::isSet () const {
4141
}
4242

4343
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
44-
setV4();
45-
(*this)[0] = first_octet;
46-
(*this)[1] = second_octet;
47-
(*this)[2] = third_octet;
48-
(*this)[3] = fourth_octet;
49-
}
44+
uint8_t addr[] {
45+
first_octet,
46+
second_octet,
47+
third_octet,
48+
fourth_octet,
49+
};
5050

51-
void IPAddress::ctor32(uint32_t address) {
52-
setV4();
53-
v4() = address;
54-
}
55-
56-
IPAddress::IPAddress(const uint8_t *address) {
57-
setV4();
58-
(*this)[0] = address[0];
59-
(*this)[1] = address[1];
60-
(*this)[2] = address[2];
61-
(*this)[3] = address[3];
51+
*this = &addr[0];
6252
}
6353

6454
bool IPAddress::fromString(const char *address) {
@@ -116,8 +106,10 @@ bool IPAddress::fromString4(const char *address) {
116106
}
117107

118108
IPAddress& IPAddress::operator=(const uint8_t *address) {
119-
setV4();
120-
v4() = *reinterpret_cast<const uint32_t*>(address);
109+
uint32_t value;
110+
memcpy_P(&value, address, sizeof(value));
111+
112+
*this = value;
121113
return *this;
122114
}
123115

@@ -128,7 +120,14 @@ IPAddress& IPAddress::operator=(uint32_t address) {
128120
}
129121

130122
bool IPAddress::operator==(const uint8_t* addr) const {
131-
return isV4() && v4() == *reinterpret_cast<const uint32_t*>(addr);
123+
if (!isV4()) {
124+
return false;
125+
}
126+
127+
uint32_t value;
128+
memcpy_P(&value, addr, sizeof(value));
129+
130+
return v4() == value;
132131
}
133132

134133
size_t IPAddress::printTo(Print& p) const {

cores/esp8266/IPAddress.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,16 @@ class IPAddress: public Printable {
6161
return reinterpret_cast<const uint8_t*>(&v4());
6262
}
6363

64-
void ctor32 (uint32_t);
65-
6664
public:
67-
// Constructors
6865
IPAddress();
6966
IPAddress(const IPAddress&);
7067
IPAddress(IPAddress&&);
7168

7269
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
73-
IPAddress(uint32_t address) { ctor32(address); }
74-
IPAddress(unsigned long address) { ctor32(address); }
75-
IPAddress(int address) { ctor32(address); }
76-
IPAddress(const uint8_t *address);
70+
IPAddress(uint32_t address) { *this = address; }
71+
IPAddress(unsigned long address) { *this = address; }
72+
IPAddress(int address) { *this = address; }
73+
IPAddress(const uint8_t *address) { *this = address; }
7774

7875
bool fromString(const char *address);
7976
bool fromString(const String &address) { return fromString(address.c_str()); }
@@ -88,7 +85,7 @@ class IPAddress: public Printable {
8885
operator bool () { return isSet(); } // <- both are needed
8986

9087
// generic IPv4 wrapper to uint32-view like arduino loves to see it
91-
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; } // for raw_address(const)
88+
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; }
9289
uint32_t& v4() { return ip_2_ip4(&_ip)->addr; }
9390

9491
bool operator==(const IPAddress& addr) const {
@@ -117,11 +114,18 @@ class IPAddress: public Printable {
117114

118115
// Overloaded index operator to allow getting and setting individual octets of the address
119116
uint8_t operator[](int index) const {
120-
return isV4()? *(raw_address() + index): 0;
117+
if (!isV4()) {
118+
return 0;
119+
}
120+
121+
return ip4_addr_get_byte_val(*ip_2_ip4(&_ip), index);
121122
}
123+
122124
uint8_t& operator[](int index) {
123125
setV4();
124-
return *(raw_address() + index);
126+
127+
uint8_t* ptr = reinterpret_cast<uint8_t*>(&v4());
128+
return *(ptr + index);
125129
}
126130

127131
// Overloaded copy operators to allow initialisation of IPAddress objects from other types

0 commit comments

Comments
 (0)