Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle u8 pointers when assigning and comparing #8818

Merged
merged 4 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions cores/esp8266/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 {
Expand Down
24 changes: 14 additions & 10 deletions cores/esp8266/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()); }
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down