Skip to content

Commit a6330b2

Browse files
Cleanup and bounds checking
Moved implementation details .h->.cpp. Added bounds checking on index operators. Added constructor to MacAddress8 to match MacAddress.
1 parent dca7e22 commit a6330b2

File tree

4 files changed

+124
-78
lines changed

4 files changed

+124
-78
lines changed

Diff for: cores/esp32/MacAddress.cpp

+47-13
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,55 @@ uint64_t MacAddress::Value() {
7373
return _mac.val;
7474
}
7575

76-
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
77-
MacAddress& MacAddress::operator=(const uint8_t *mac)
78-
{
79-
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
76+
//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0];
77+
uint8_t MacAddress::operator[](int index) const {
78+
index = EnforceIndexBounds(index);
79+
return _mac.bytes[index];
80+
}
81+
82+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
83+
uint8_t& MacAddress::operator[](int index) {
84+
index = EnforceIndexBounds(index);
85+
return _mac.bytes[index];
86+
}
87+
88+
//Overloaded copy operator: init MacAddress object from byte array
89+
MacAddress& MacAddress::operator=(const uint8_t *macbytearray) {
90+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
8091
return *this;
8192
}
8293

83-
MacAddress& MacAddress::operator=(uint64_t macval)
84-
{
94+
//Overloaded copy operator: init MacAddress object from uint64_t
95+
MacAddress& MacAddress::operator=(uint64_t macval) {
8596
_mac.val = macval;
8697
return *this;
8798
}
8899

89100
//Compare class to byte array
90-
bool MacAddress::operator==(const uint8_t *mac) const
91-
{
92-
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
101+
bool MacAddress::operator==(const uint8_t *macbytearray) const {
102+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
93103
}
94104

95105
//Allow comparing value of two classes
96-
bool MacAddress::operator==(const MacAddress& mac2) const
97-
{
106+
bool MacAddress::operator==(const MacAddress& mac2) const {
98107
return _mac.val == mac2._mac.val;
99108
}
100-
109+
110+
//Type converter object to uint64_t [same as .Value()]
111+
MacAddress::operator uint64_t() const {
112+
return _mac.val;
113+
}
114+
115+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
116+
MacAddress::operator const uint8_t*() const {
117+
return _mac.bytes;
118+
}
119+
120+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
121+
MacAddress::operator const uint64_t*() const {
122+
return &_mac.val;
123+
}
124+
101125
size_t MacAddress::printTo(Print& p) const
102126
{
103127
size_t n = 0;
@@ -109,4 +133,14 @@ size_t MacAddress::printTo(Print& p) const
109133
}
110134
return n;
111135
}
112-
136+
137+
//Bounds checking
138+
int MacAddress::EnforceIndexBounds(int i) const {
139+
if(i < 0) {
140+
return 0;
141+
}
142+
if(i >= sizeof(_mac.bytes)) {
143+
return sizeof(_mac.bytes)-1;
144+
}
145+
return i;
146+
}

Diff for: cores/esp32/MacAddress.h

+10-25
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,15 @@ class MacAddress : public Printable {
4646
String toString() const;
4747
uint64_t Value();
4848

49-
// Overloaded index operator to allow getting and setting individual octets of the address
50-
uint8_t operator[](int index) const
51-
{
52-
return _mac.bytes[index];
53-
}
54-
uint8_t& operator[](int index)
55-
{
56-
return _mac.bytes[index];
57-
}
58-
59-
MacAddress& operator=(const uint8_t *mac);
49+
uint8_t operator[](int index) const;
50+
uint8_t& operator[](int index);
51+
MacAddress& operator=(const uint8_t *macbytearray);
6052
MacAddress& operator=(uint64_t macval);
61-
bool operator==(const uint8_t *mac) const;
53+
bool operator==(const uint8_t *macbytearray) const;
6254
bool operator==(const MacAddress& mac2) const;
63-
64-
operator uint64_t() const
65-
{
66-
return _mac.val;
67-
}
68-
operator const uint8_t*() const
69-
{
70-
return _mac.bytes;
71-
}
72-
operator const uint64_t*() const
73-
{
74-
return &_mac.val;
75-
}
55+
operator uint64_t() const;
56+
operator const uint8_t*() const;
57+
operator const uint64_t*() const;
7658

7759
virtual size_t printTo(Print& p) const;
7860

@@ -83,6 +65,9 @@ class MacAddress : public Printable {
8365
friend class Server;
8466
friend class DhcpClass;
8567
friend class DNSClient;
68+
69+
private:
70+
int EnforceIndexBounds(int i) const;
8671
};
8772

8873
#endif

Diff for: cores/esp32/MacAddress8.cpp

+54-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ MacAddress8::MacAddress8(const uint8_t *macbytearray) {
1515
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
1616
}
1717

18+
MacAddress8::MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) {
19+
_mac.bytes[0] = b1;
20+
_mac.bytes[1] = b2;
21+
_mac.bytes[2] = b3;
22+
_mac.bytes[3] = b4;
23+
_mac.bytes[4] = b5;
24+
_mac.bytes[5] = b6;
25+
_mac.bytes[6] = b7;
26+
_mac.bytes[7] = b8;
27+
}
28+
1829
//Parse user entered string into MAC address
1930
bool MacAddress8::fromCStr(const char *buf) {
2031
char cs[24];
@@ -64,37 +75,54 @@ uint64_t MacAddress8::Value() {
6475
return _mac.val;
6576
}
6677

67-
//Implicit conversion object to number [same as .Value()]
68-
MacAddress8::operator uint64_t() const
69-
{
70-
return _mac.val;
78+
uint8_t MacAddress8::operator[](int index) const {
79+
index = EnforceIndexBounds(index);
80+
return _mac.bytes[index];
7181
}
7282

73-
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
74-
MacAddress8& MacAddress8::operator=(const uint8_t *mac)
75-
{
76-
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
83+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
84+
uint8_t& MacAddress8::operator[](int index) {
85+
index = EnforceIndexBounds(index);
86+
return _mac.bytes[index];
87+
}
88+
89+
//Overloaded copy operator: init MacAddress object from byte array
90+
MacAddress8& MacAddress8::operator=(const uint8_t *macbytearray) {
91+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
7792
return *this;
7893
}
7994

80-
MacAddress8& MacAddress8::operator=(uint64_t macval)
81-
{
95+
//Overloaded copy operator: init MacAddress object from uint64_t
96+
MacAddress8& MacAddress8::operator=(uint64_t macval) {
8297
_mac.val = macval;
8398
return *this;
8499
}
85100

86101
//Compare class to byte array
87-
bool MacAddress8::operator==(const uint8_t *mac) const
88-
{
89-
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
102+
bool MacAddress8::operator==(const uint8_t *macbytearray) const {
103+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
90104
}
91105

92106
//Allow comparing value of two classes
93-
bool MacAddress8::operator==(const MacAddress8& mac2) const
94-
{
107+
bool MacAddress8::operator==(const MacAddress8& mac2) const {
95108
return _mac.val == mac2._mac.val;
96109
}
97110

111+
//Type converter object to uint64_t [same as .Value()]
112+
MacAddress8::operator uint64_t() const {
113+
return _mac.val;
114+
}
115+
116+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
117+
MacAddress8::operator const uint8_t*() const {
118+
return _mac.bytes;
119+
}
120+
121+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
122+
MacAddress8::operator const uint64_t*() const {
123+
return &_mac.val;
124+
}
125+
98126
size_t MacAddress8::printTo(Print& p) const
99127
{
100128
size_t n = 0;
@@ -106,3 +134,14 @@ size_t MacAddress8::printTo(Print& p) const
106134
}
107135
return n;
108136
}
137+
138+
//Bounds checking
139+
int MacAddress8::EnforceIndexBounds(int i) const {
140+
if(i < 0) {
141+
return 0;
142+
}
143+
if(i >= sizeof(_mac.bytes)) {
144+
return sizeof(_mac.bytes)-1;
145+
}
146+
return i;
147+
}

Diff for: cores/esp32/MacAddress8.h

+13-25
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,24 @@ class MacAddress8 : public Printable {
3434
MacAddress8();
3535
MacAddress8(uint64_t mac);
3636
MacAddress8(const uint8_t *macbytearray);
37+
MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8);
3738
virtual ~MacAddress8() {}
3839
bool fromCStr(const char *buf);
3940
bool fromString(const String &macstr);
4041
void toBytes(uint8_t *buf);
41-
int toCStr(char *buf);
42+
int toCStr(char *buf);
4243
String toString() const;
4344
uint64_t Value();
4445

45-
operator uint64_t() const;
46-
MacAddress8& operator=(const uint8_t *mac);
46+
uint8_t operator[](int index) const;
47+
uint8_t& operator[](int index);
48+
MacAddress8& operator=(const uint8_t *macbytearray);
4749
MacAddress8& operator=(uint64_t macval);
48-
bool operator==(const uint8_t *mac) const;
50+
bool operator==(const uint8_t *macbytearray) const;
4951
bool operator==(const MacAddress8& mac2) const;
50-
51-
// Overloaded index operator to allow getting and setting individual octets of the address
52-
uint8_t operator[](int index) const
53-
{
54-
return _mac.bytes[index];
55-
}
56-
uint8_t& operator[](int index)
57-
{
58-
return _mac.bytes[index];
59-
}
60-
61-
operator const uint8_t*() const
62-
{
63-
return _mac.bytes;
64-
}
65-
operator const uint64_t*() const
66-
{
67-
return &_mac.val;
68-
}
69-
52+
operator uint64_t() const;
53+
operator const uint8_t*() const;
54+
operator const uint64_t*() const;
7055
virtual size_t printTo(Print& p) const;
7156

7257
// future use in Arduino Networking
@@ -75,7 +60,10 @@ class MacAddress8 : public Printable {
7560
friend class Client;
7661
friend class Server;
7762
friend class DhcpClass;
78-
friend class DNSClient;
63+
friend class DNSClient;
64+
65+
private:
66+
int EnforceIndexBounds(int i) const;
7967
};
8068

8169
#endif

0 commit comments

Comments
 (0)