Skip to content

Commit 79bd3ea

Browse files
committed
BLE: Fix relationnal operator of SafeEnum
The relationnal operators were targeting the base class which defines an implicit constructor to an integral value. This is wrong as it allows SafeEnum instances to be compared against integers. The fix is simple: define relationnal operators for the derived class. The derived class is known as it is passed as a template parameter of the base class. For extra safety the SafeEnum constructor is now explicit and protected.
1 parent f8ef143 commit 79bd3ea

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

features/FEATURE_BLE/ble/SafeEnum.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,85 +112,87 @@ struct SafeEnum {
112112
*/
113113
typedef LayoutType representation_t;
114114

115+
protected:
115116
/**
116117
* Construction of an enumeration value.
117118
*/
118-
SafeEnum(LayoutType value) : _value(value) { }
119+
explicit SafeEnum(LayoutType value) : _value(value) { }
119120

121+
public:
120122
/**
121-
* Equal to operator for SafeEnum instances.
123+
* Equal to operator for Target instances.
122124
*
123125
* @param lhs left hand side of the comparison
124126
* @param rhs right hand side of the comparison
125127
*
126128
* @return true if the inner value of lhs and rhs are equal and false
127129
* otherwise.
128130
*/
129-
friend bool operator==(SafeEnum lhs, SafeEnum rhs) {
131+
friend bool operator==(Target lhs, Target rhs) {
130132
return lhs._value == rhs._value;
131133
}
132134

133135
/**
134-
* Not equal to operator for SafeEnum instances.
136+
* Not equal to operator for Target instances.
135137
*
136138
* @param lhs left hand side of the comparison
137139
* @param rhs right hand side of the comparison
138140
*
139141
* @return true if the inner value of lhs and rhs are not equal and false
140142
* otherwise.
141143
*/
142-
friend bool operator!=(SafeEnum lhs, SafeEnum rhs) {
144+
friend bool operator!=(Target lhs, Target rhs) {
143145
return !(lhs == rhs);
144146
}
145147

146148
/**
147-
* Less than operator for SafeEnum instances.
149+
* Less than operator for Target instances.
148150
*
149151
* @param lhs left hand side of the comparison
150152
* @param rhs right hand side of the comparison
151153
*
152154
* @return true if the inner value of lhs is less than rhs and false otherwise.
153155
*/
154-
friend bool operator<(SafeEnum lhs, SafeEnum rhs) {
156+
friend bool operator<(Target lhs, Target rhs) {
155157
return lhs.value() < rhs.value();
156158
}
157159

158160
/**
159-
* Less than or equal to operator for SafeEnum instances.
161+
* Less than or equal to operator for Target instances.
160162
*
161163
* @param lhs left hand side of the comparison
162164
* @param rhs right hand side of the comparison
163165
*
164166
* @return true if the inner value of lhs is less than or equal to rhs and
165167
* false otherwise.
166168
*/
167-
friend bool operator<=(SafeEnum lhs, SafeEnum rhs) {
169+
friend bool operator<=(Target lhs, Target rhs) {
168170
return lhs.value() < rhs.value() || lhs == rhs;
169171
}
170172

171173
/**
172-
* Greater than operator for SafeEnum instances.
174+
* Greater than operator for Target instances.
173175
*
174176
* @param lhs left hand side of the comparison
175177
* @param rhs right hand side of the comparison
176178
*
177179
* @return true if the inner value of lhs is greater than rhs; false
178180
* otherwise.
179181
*/
180-
friend bool operator>(SafeEnum lhs, SafeEnum rhs) {
182+
friend bool operator>(Target lhs, Target rhs) {
181183
return !(lhs <= rhs);
182184
}
183185

184186
/**
185-
* Greater than or equal to operator for SafeEnum instances.
187+
* Greater than or equal to operator for Target instances.
186188
*
187189
* @param lhs left hand side of the comparison
188190
* @param rhs right hand side of the comparison
189191
*
190192
* @return true if the inner value of lhs is greater than or equal to rhs;
191193
* false otherwise.
192194
*/
193-
friend bool operator>=(SafeEnum lhs, SafeEnum rhs) {
195+
friend bool operator>=(Target lhs, Target rhs) {
194196
return !(lhs < rhs);
195197
}
196198

0 commit comments

Comments
 (0)