Skip to content

Commit 87fe8f9

Browse files
committed
mbed_error: Avoid negative left shift
User uses of `MBED_MAKE_ERROR` assemble a new error that gets its bottom 16 bits from an existing negative 32-bit error code. This lead to undefined behaviour when `<<` was used on it - even though it was a shift by zero! Avoid the undefined behaviour warning from Clang by masking before shifting.
1 parent 7fb9ae6 commit 87fe8f9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

platform/mbed_error.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,29 @@ extern "C" {
5050
#endif
5151

5252
#define MBED_ERROR_STATUS_CODE_MASK (0x0000FFFF)
53+
#define MBED_ERROR_STATUS_CODE_UNSHIFTED_MASK (0x0000FFFF)
5354
#define MBED_ERROR_STATUS_CODE_POS (0)
5455
#define MBED_ERROR_STATUS_CODE_FIELD_SIZE (16)
5556

5657
#define MBED_ERROR_STATUS_MODULE_MASK (0x00FF0000)
58+
#define MBED_ERROR_STATUS_MODULE_UNSHIFTED_MASK (0x000000FF)
5759
#define MBED_ERROR_STATUS_MODULE_POS (16)
5860
#define MBED_ERROR_STATUS_MODULE_FIELD_SIZE (8)
5961

6062
#define MBED_ERROR_STATUS_TYPE_MASK (0x60000000)
63+
#define MBED_ERROR_STATUS_TYPE_UNSHIFTED_MASK (0x00000003)
6164
#define MBED_ERROR_STATUS_TYPE_POS (29)
6265
#define MBED_ERROR_STATUS_TYPE_FIELD_SIZE (2)
6366

6467
/* mbed_error_status_t Status Encoding */
6568
//|31(1 bit) Always Negative|30-29(2 bits) |28-24 | 23-16(8 bits) | 15-0(16 bits) |
6669
//|-1 |TYPE |(unused/reserved) | MODULE TYPE | ERROR CODE |
6770

68-
#define MAKE_MBED_ERROR(type, module, error_code) (mbed_error_status_t) \
69-
((0x80000000) | \
70-
(MBED_ERROR_STATUS_CODE_MASK & (error_code << MBED_ERROR_STATUS_CODE_POS)) | \
71-
(MBED_ERROR_STATUS_MODULE_MASK & (module << MBED_ERROR_STATUS_MODULE_POS)) | \
72-
(MBED_ERROR_STATUS_TYPE_MASK & (type << MBED_ERROR_STATUS_TYPE_POS)))
71+
#define MAKE_MBED_ERROR(type, module, error_code) (mbed_error_status_t) \
72+
((0x80000000) | \
73+
((mbed_error_status_t) (error_code & MBED_ERROR_STATUS_CODE_UNSHIFTED_MASK) << MBED_ERROR_STATUS_CODE_POS) | \
74+
((mbed_error_status_t) (module & MBED_ERROR_STATUS_MODULE_UNSHIFTED_MASK) << MBED_ERROR_STATUS_MODULE_POS) | \
75+
((mbed_error_status_t) (type & MBED_ERROR_STATUS_TYPE_UNSHIFTED_MASK) << MBED_ERROR_STATUS_TYPE_POS))
7376

7477
#define MBED_GET_ERROR_TYPE( error_status ) ((error_status & MBED_ERROR_STATUS_TYPE_MASK) >> MBED_ERROR_STATUS_TYPE_POS)
7578
#define MBED_GET_ERROR_MODULE( error_status ) ((error_status & MBED_ERROR_STATUS_MODULE_MASK) >> MBED_ERROR_STATUS_MODULE_POS)
@@ -83,7 +86,7 @@ extern "C" {
8386
*
8487
\verbatim
8588
| 31 Always Negative | 30-29(2 bits) | 28-24 | 23-16(8 bits) | 15-0(16 bits) |
86-
| -1 | TYPE | (unused/reserved) | MODULE TYPE | ERROR CODE |
89+
| -1 | TYPE | (unused/reserved) | MODULE TYPE | ERROR CODE |
8790
\endverbatim
8891
*
8992
* The error status value range for each error type is as follows:\n

0 commit comments

Comments
 (0)