-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Rewrite IPAddress without union #8819
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,29 +23,27 @@ | |
#include <stdint.h> | ||
#include <WString.h> | ||
#include <Printable.h> | ||
#include <array> | ||
|
||
// A class to make it easier to handle and pass around IP addresses | ||
|
||
class IPAddress: public Printable | ||
{ | ||
private: | ||
union { | ||
uint8_t bytes[4]; // IPv4 address | ||
uint32_t dword; | ||
} _address; | ||
alignas(alignof(uint32_t)) std::array<uint8_t,4> _address{}; // IPv4 address | ||
|
||
// Access the raw byte array containing the address. Because this returns a pointer | ||
// to the internal structure rather than a copy of the address this function should only | ||
// be used when you know that the usage of the returned uint8_t* will be transient and not | ||
// stored. | ||
uint8_t* raw_address() | ||
{ | ||
return _address.bytes; | ||
return _address.data(); | ||
} | ||
|
||
public: | ||
// Constructors | ||
IPAddress(); | ||
IPAddress() = default; | ||
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); | ||
IPAddress(uint32_t address); | ||
IPAddress(const uint8_t *address); | ||
|
@@ -54,26 +52,27 @@ class IPAddress: public Printable | |
bool fromString(const char *address); | ||
bool fromString(const String &address) { return fromString(address.c_str()); } | ||
|
||
|
||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer | ||
// to a four-byte uint8_t array is expected | ||
operator uint32_t() const | ||
{ | ||
return _address.dword; | ||
return reinterpret_cast<const uint32_t&>(_address.front()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem, reinterpret_cast should be used with utmost care, especially with alignment. |
||
} | ||
bool operator==(const IPAddress& addr) const | ||
{ | ||
return _address.dword == addr._address.dword; | ||
return _address == addr._address; | ||
} | ||
bool operator==(const uint8_t* addr) const; | ||
|
||
// Overloaded index operator to allow getting and setting individual octets of the address | ||
uint8_t operator[](int index) const | ||
{ | ||
return _address.bytes[index]; | ||
return _address[index]; | ||
} | ||
uint8_t& operator[](int index) | ||
{ | ||
return _address.bytes[index]; | ||
return _address[index]; | ||
} | ||
|
||
// Overloaded copy operators to allow initialisation of IPAddress objects from other types | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
_address.front()
guaranteed to be 32-bit aligned?If not, then this will cause undefined behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is guaranteed be aligned as structure -- array is once member in this structure -- it has same address. It behavior same as union or c-style array (uint8_t a[4]).
Standart says (https://timsong-cpp.github.io/cppwp/n4659/array):
https://timsong-cpp.github.io/cppwp/n4659/dcl.init.aggr#1:
https://timsong-cpp.github.io/cppwp/n4659/dcl.init.aggr#2:
and (https://timsong-cpp.github.io/cppwp/n4659/container.requirements#general-6):
https://timsong-cpp.github.io/cppwp/n4659/sequence.reqmts#tab:containers.sequence.optional:
It mean that
&_address.front() == &(*_address.begin()) == &_address == &IPAddress
and correspondingly an align of
_address.front()
depend on an align ofIPAddress
structure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this example shows that
alignof
= 1 for struct withstd::array<uint8_t,4>
member.alignas(4)
is required to achieve 32-bit alignmentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this example shows that class
IPAddress
has 4 bytes of aligment because it inherits from an abstract type (with vtable).in this case, the _address field still has 1 byte of alignment. -- need
alignas( alignof(uint32_t) )
forIPAddress
.as here
I didn’t initially take into account that inheritance occurs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i fix this omission.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, of course, I would like to generally highlight that
uint32_t
may not exist at all on a specific platform.Maybe
uint_least32_t
should be used for compatibility?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint32_t
is known in all platforms this repository covers.I don't know
uint_least32_t
, but it seems to be not guaranteed to be a fixed length type, which will cause issues when it is used in some struct of fixed length or offset of members (e.g. storing a struct or transferring it to another node)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://timsong-cpp.github.io/cppwp/n4659/cstdint#syn -- types that are at least N-bit