mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
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
This commit is contained in:
parent
41c2be2068
commit
56107fb1f9
@ -41,24 +41,14 @@ bool IPAddress::isSet () const {
|
||||
}
|
||||
|
||||
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
|
||||
setV4();
|
||||
(*this)[0] = first_octet;
|
||||
(*this)[1] = second_octet;
|
||||
(*this)[2] = third_octet;
|
||||
(*this)[3] = fourth_octet;
|
||||
}
|
||||
uint8_t addr[] {
|
||||
first_octet,
|
||||
second_octet,
|
||||
third_octet,
|
||||
fourth_octet,
|
||||
};
|
||||
|
||||
void IPAddress::ctor32(uint32_t address) {
|
||||
setV4();
|
||||
v4() = address;
|
||||
}
|
||||
|
||||
IPAddress::IPAddress(const uint8_t *address) {
|
||||
setV4();
|
||||
(*this)[0] = address[0];
|
||||
(*this)[1] = address[1];
|
||||
(*this)[2] = address[2];
|
||||
(*this)[3] = address[3];
|
||||
*this = &addr[0];
|
||||
}
|
||||
|
||||
bool IPAddress::fromString(const char *address) {
|
||||
@ -116,8 +106,10 @@ bool IPAddress::fromString4(const char *address) {
|
||||
}
|
||||
|
||||
IPAddress& IPAddress::operator=(const uint8_t *address) {
|
||||
setV4();
|
||||
v4() = *reinterpret_cast<const uint32_t*>(address);
|
||||
uint32_t value;
|
||||
memcpy_P(&value, address, sizeof(value));
|
||||
|
||||
*this = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -128,7 +120,14 @@ IPAddress& IPAddress::operator=(uint32_t address) {
|
||||
}
|
||||
|
||||
bool IPAddress::operator==(const uint8_t* addr) const {
|
||||
return isV4() && v4() == *reinterpret_cast<const uint32_t*>(addr);
|
||||
if (!isV4()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t value;
|
||||
memcpy_P(&value, addr, sizeof(value));
|
||||
|
||||
return v4() == value;
|
||||
}
|
||||
|
||||
size_t IPAddress::printTo(Print& p) const {
|
||||
|
@ -61,19 +61,16 @@ class IPAddress: public Printable {
|
||||
return reinterpret_cast<const uint8_t*>(&v4());
|
||||
}
|
||||
|
||||
void ctor32 (uint32_t);
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
IPAddress();
|
||||
IPAddress(const IPAddress&);
|
||||
IPAddress(IPAddress&&);
|
||||
|
||||
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
||||
IPAddress(uint32_t address) { ctor32(address); }
|
||||
IPAddress(unsigned long address) { ctor32(address); }
|
||||
IPAddress(int address) { ctor32(address); }
|
||||
IPAddress(const uint8_t *address);
|
||||
IPAddress(uint32_t address) { *this = address; }
|
||||
IPAddress(unsigned long address) { *this = address; }
|
||||
IPAddress(int address) { *this = address; }
|
||||
IPAddress(const uint8_t *address) { *this = address; }
|
||||
|
||||
bool fromString(const char *address);
|
||||
bool fromString(const String &address) { return fromString(address.c_str()); }
|
||||
@ -88,7 +85,7 @@ class IPAddress: public Printable {
|
||||
operator bool () { return isSet(); } // <- both are needed
|
||||
|
||||
// generic IPv4 wrapper to uint32-view like arduino loves to see it
|
||||
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; } // for raw_address(const)
|
||||
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; }
|
||||
uint32_t& v4() { return ip_2_ip4(&_ip)->addr; }
|
||||
|
||||
bool operator==(const IPAddress& addr) const {
|
||||
@ -117,11 +114,18 @@ class IPAddress: public Printable {
|
||||
|
||||
// Overloaded index operator to allow getting and setting individual octets of the address
|
||||
uint8_t operator[](int index) const {
|
||||
return isV4()? *(raw_address() + index): 0;
|
||||
if (!isV4()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ip4_addr_get_byte_val(*ip_2_ip4(&_ip), index);
|
||||
}
|
||||
|
||||
uint8_t& operator[](int index) {
|
||||
setV4();
|
||||
return *(raw_address() + index);
|
||||
|
||||
uint8_t* ptr = reinterpret_cast<uint8_t*>(&v4());
|
||||
return *(ptr + index);
|
||||
}
|
||||
|
||||
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
||||
|
Loading…
x
Reference in New Issue
Block a user