Skip to content

BLE: Fix SafeEnum type safety #9393

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

Merged
merged 2 commits into from
Jan 28, 2019
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
28 changes: 15 additions & 13 deletions features/FEATURE_BLE/ble/SafeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,85 +112,87 @@ struct SafeEnum {
*/
typedef LayoutType representation_t;

protected:
/**
* Construction of an enumeration value.
*/
SafeEnum(LayoutType value) : _value(value) { }
explicit SafeEnum(LayoutType value) : _value(value) { }

public:
/**
* Equal to operator for SafeEnum instances.
* Equal to operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs and rhs are equal and false
* otherwise.
*/
friend bool operator==(SafeEnum lhs, SafeEnum rhs) {
friend bool operator==(Target lhs, Target rhs) {
return lhs._value == rhs._value;
}

/**
* Not equal to operator for SafeEnum instances.
* Not equal to operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs and rhs are not equal and false
* otherwise.
*/
friend bool operator!=(SafeEnum lhs, SafeEnum rhs) {
friend bool operator!=(Target lhs, Target rhs) {
return !(lhs == rhs);
}

/**
* Less than operator for SafeEnum instances.
* Less than operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs is less than rhs and false otherwise.
*/
friend bool operator<(SafeEnum lhs, SafeEnum rhs) {
friend bool operator<(Target lhs, Target rhs) {
return lhs.value() < rhs.value();
}

/**
* Less than or equal to operator for SafeEnum instances.
* Less than or equal to operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs is less than or equal to rhs and
* false otherwise.
*/
friend bool operator<=(SafeEnum lhs, SafeEnum rhs) {
friend bool operator<=(Target lhs, Target rhs) {
return lhs.value() < rhs.value() || lhs == rhs;
}

/**
* Greater than operator for SafeEnum instances.
* Greater than operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs is greater than rhs; false
* otherwise.
*/
friend bool operator>(SafeEnum lhs, SafeEnum rhs) {
friend bool operator>(Target lhs, Target rhs) {
return !(lhs <= rhs);
}

/**
* Greater than or equal to operator for SafeEnum instances.
* Greater than or equal to operator for Target instances.
*
* @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison
*
* @return true if the inner value of lhs is greater than or equal to rhs;
* false otherwise.
*/
friend bool operator>=(SafeEnum lhs, SafeEnum rhs) {
friend bool operator>=(Target lhs, Target rhs) {
return !(lhs < rhs);
}

Expand Down
2 changes: 1 addition & 1 deletion features/FEATURE_BLE/source/gap/AdvertisingDataBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ uint8_t *AdvertisingDataBuilder::findField(adv_data_type_t type)
for (uint8_t idx = 0; idx < _payload_length;) {
uint8_t fieldType = _buffer[idx + FIELD_TYPE_INDEX];

if (fieldType == type) {
if (fieldType == type.value()) {
return _buffer.data() + idx;
}

Expand Down