1
0
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:
Max Prokhorov 2023-01-20 12:26:12 +03:00 committed by GitHub
parent 41c2be2068
commit 56107fb1f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 30 deletions

View File

@ -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) { IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
setV4(); uint8_t addr[] {
(*this)[0] = first_octet; first_octet,
(*this)[1] = second_octet; second_octet,
(*this)[2] = third_octet; third_octet,
(*this)[3] = fourth_octet; fourth_octet,
} };
void IPAddress::ctor32(uint32_t address) { *this = &addr[0];
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];
} }
bool IPAddress::fromString(const char *address) { bool IPAddress::fromString(const char *address) {
@ -116,8 +106,10 @@ bool IPAddress::fromString4(const char *address) {
} }
IPAddress& IPAddress::operator=(const uint8_t *address) { IPAddress& IPAddress::operator=(const uint8_t *address) {
setV4(); uint32_t value;
v4() = *reinterpret_cast<const uint32_t*>(address); memcpy_P(&value, address, sizeof(value));
*this = value;
return *this; return *this;
} }
@ -128,7 +120,14 @@ IPAddress& IPAddress::operator=(uint32_t address) {
} }
bool IPAddress::operator==(const uint8_t* addr) const { 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 { size_t IPAddress::printTo(Print& p) const {

View File

@ -61,19 +61,16 @@ class IPAddress: public Printable {
return reinterpret_cast<const uint8_t*>(&v4()); return reinterpret_cast<const uint8_t*>(&v4());
} }
void ctor32 (uint32_t);
public: public:
// Constructors
IPAddress(); IPAddress();
IPAddress(const IPAddress&); IPAddress(const IPAddress&);
IPAddress(IPAddress&&); IPAddress(IPAddress&&);
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
IPAddress(uint32_t address) { ctor32(address); } IPAddress(uint32_t address) { *this = address; }
IPAddress(unsigned long address) { ctor32(address); } IPAddress(unsigned long address) { *this = address; }
IPAddress(int address) { ctor32(address); } IPAddress(int address) { *this = address; }
IPAddress(const uint8_t *address); IPAddress(const uint8_t *address) { *this = address; }
bool fromString(const char *address); bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); } 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 operator bool () { return isSet(); } // <- both are needed
// generic IPv4 wrapper to uint32-view like arduino loves to see it // 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; } uint32_t& v4() { return ip_2_ip4(&_ip)->addr; }
bool operator==(const IPAddress& addr) const { 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 // Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const { 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) { uint8_t& operator[](int index) {
setV4(); 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 // Overloaded copy operators to allow initialisation of IPAddress objects from other types