Skip to content

Commit 3035239

Browse files
Use a union in IPAddress for uint8_t[] <-> uint32_t conversion
Previously, pointer casting was used, but this resulted in strict-aliasing warnings: IPAddress.h: In member function ‘IPAddress::operator uint32_t() const’: IPAddress.h:46:61: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] operator uint32_t() const { return *((uint32_t*)_address); }; ^ IPAddress.h: In member function ‘bool IPAddress::operator==(const IPAddress&) const’: IPAddress.h:47:81: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; ^ IPAddress.h:47:114: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; Converting between unrelated types like this is commonly done using a union, which do not break the strict-aliasing rules. Using that union, inside IPAddress there is now an attribute _address.bytes for the raw byte arra, or _address.dword for the uint32_t version. Since we now have easy access to the uint32_t version, this also removes two memcpy invocations that can just become assignments. This patch does not change the generated code in any way, the compiler already optimized away the memcpy calls and the previous casts mean exactly the same. This is a different implementation of a part of arduino#1399 and it helps toward fixing arduino#1728.
1 parent 9dca56d commit 3035239

File tree

4 files changed

+44
-36
lines changed

4 files changed

+44
-36
lines changed

Diff for: hardware/arduino/avr/cores/arduino/IPAddress.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,53 @@
2222

2323
IPAddress::IPAddress()
2424
{
25-
memset(_address, 0, sizeof(_address));
25+
_address.dword = 0;
2626
}
2727

2828
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
2929
{
30-
_address[0] = first_octet;
31-
_address[1] = second_octet;
32-
_address[2] = third_octet;
33-
_address[3] = fourth_octet;
30+
_address.bytes[0] = first_octet;
31+
_address.bytes[1] = second_octet;
32+
_address.bytes[2] = third_octet;
33+
_address.bytes[3] = fourth_octet;
3434
}
3535

3636
IPAddress::IPAddress(uint32_t address)
3737
{
38-
memcpy(_address, &address, sizeof(_address));
38+
_address.dword = address;
3939
}
4040

4141
IPAddress::IPAddress(const uint8_t *address)
4242
{
43-
memcpy(_address, address, sizeof(_address));
43+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

4646
IPAddress& IPAddress::operator=(const uint8_t *address)
4747
{
48-
memcpy(_address, address, sizeof(_address));
48+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4949
return *this;
5050
}
5151

5252
IPAddress& IPAddress::operator=(uint32_t address)
5353
{
54-
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
54+
_address.dword = address;
5555
return *this;
5656
}
5757

5858
bool IPAddress::operator==(const uint8_t* addr) const
5959
{
60-
return memcmp(addr, _address, sizeof(_address)) == 0;
60+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
6161
}
6262

6363
size_t IPAddress::printTo(Print& p) const
6464
{
6565
size_t n = 0;
6666
for (int i =0; i < 3; i++)
6767
{
68-
n += p.print(_address[i], DEC);
68+
n += p.print(_address.bytes[i], DEC);
6969
n += p.print('.');
7070
}
71-
n += p.print(_address[3], DEC);
71+
n += p.print(_address.bytes[3], DEC);
7272
return n;
7373
}
7474

Diff for: hardware/arduino/avr/cores/arduino/IPAddress.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727

2828
class IPAddress : public Printable {
2929
private:
30-
uint8_t _address[4]; // IPv4 address
30+
union {
31+
uint8_t bytes[4]; // IPv4 address
32+
uint32_t dword;
33+
} _address;
34+
3135
// Access the raw byte array containing the address. Because this returns a pointer
3236
// to the internal structure rather than a copy of the address this function should only
3337
// be used when you know that the usage of the returned uint8_t* will be transient and not
3438
// stored.
35-
uint8_t* raw_address() { return _address; };
39+
uint8_t* raw_address() { return _address.bytes; };
3640

3741
public:
3842
// Constructors
@@ -43,13 +47,13 @@ class IPAddress : public Printable {
4347

4448
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4549
// to a four-byte uint8_t array is expected
46-
operator uint32_t() const { return *((uint32_t*)_address); };
47-
bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
50+
operator uint32_t() const { return _address.dword; };
51+
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
4852
bool operator==(const uint8_t* addr) const;
4953

5054
// Overloaded index operator to allow getting and setting individual octets of the address
51-
uint8_t operator[](int index) const { return _address[index]; };
52-
uint8_t& operator[](int index) { return _address[index]; };
55+
uint8_t operator[](int index) const { return _address.bytes[index]; };
56+
uint8_t& operator[](int index) { return _address.bytes[index]; };
5357

5458
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
5559
IPAddress& operator=(const uint8_t *address);

Diff for: hardware/arduino/sam/cores/arduino/IPAddress.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,53 @@
2222

2323
IPAddress::IPAddress()
2424
{
25-
memset(_address, 0, sizeof(_address));
25+
_address.dword = 0;
2626
}
2727

2828
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
2929
{
30-
_address[0] = first_octet;
31-
_address[1] = second_octet;
32-
_address[2] = third_octet;
33-
_address[3] = fourth_octet;
30+
_address.bytes[0] = first_octet;
31+
_address.bytes[1] = second_octet;
32+
_address.bytes[2] = third_octet;
33+
_address.bytes[3] = fourth_octet;
3434
}
3535

3636
IPAddress::IPAddress(uint32_t address)
3737
{
38-
memcpy(_address, &address, sizeof(_address));
38+
_address.dword = address;
3939
}
4040

4141
IPAddress::IPAddress(const uint8_t *address)
4242
{
43-
memcpy(_address, address, sizeof(_address));
43+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

4646
IPAddress& IPAddress::operator=(const uint8_t *address)
4747
{
48-
memcpy(_address, address, sizeof(_address));
48+
memcpy(_address.bytes, address, sizeof(_address.bytes));
4949
return *this;
5050
}
5151

5252
IPAddress& IPAddress::operator=(uint32_t address)
5353
{
54-
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
54+
_address.dword = address;
5555
return *this;
5656
}
5757

5858
bool IPAddress::operator==(const uint8_t* addr) const
5959
{
60-
return memcmp(addr, _address, sizeof(_address)) == 0;
60+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
6161
}
6262

6363
size_t IPAddress::printTo(Print& p) const
6464
{
6565
size_t n = 0;
6666
for (int i =0; i < 3; i++)
6767
{
68-
n += p.print(_address[i], DEC);
68+
n += p.print(_address.bytes[i], DEC);
6969
n += p.print('.');
7070
}
71-
n += p.print(_address[3], DEC);
71+
n += p.print(_address.bytes[3], DEC);
7272
return n;
7373
}
7474

Diff for: hardware/arduino/sam/cores/arduino/IPAddress.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727

2828
class IPAddress : public Printable {
2929
private:
30-
uint8_t _address[4]; // IPv4 address
30+
union {
31+
uint8_t bytes[4]; // IPv4 address
32+
uint32_t dword;
33+
} _address;
34+
3135
// Access the raw byte array containing the address. Because this returns a pointer
3236
// to the internal structure rather than a copy of the address this function should only
3337
// be used when you know that the usage of the returned uint8_t* will be transient and not
3438
// stored.
35-
uint8_t* raw_address() { return _address; };
39+
uint8_t* raw_address() { return _address.bytes; };
3640

3741
public:
3842
// Constructors
@@ -43,13 +47,13 @@ class IPAddress : public Printable {
4347

4448
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4549
// to a four-byte uint8_t array is expected
46-
operator uint32_t() const { return *((uint32_t*)_address); };
47-
bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
50+
operator uint32_t() const { return _address.dword; };
51+
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
4852
bool operator==(const uint8_t* addr) const;
4953

5054
// Overloaded index operator to allow getting and setting individual octets of the address
51-
uint8_t operator[](int index) const { return _address[index]; };
52-
uint8_t& operator[](int index) { return _address[index]; };
55+
uint8_t operator[](int index) const { return _address.bytes[index]; };
56+
uint8_t& operator[](int index) { return _address.bytes[index]; };
5357

5458
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
5559
IPAddress& operator=(const uint8_t *address);

0 commit comments

Comments
 (0)