Skip to content

Commit bd4c650

Browse files
committed
Fix #1208, typesafe definition of osal_id_t
Modifies the osal_id_t typedef to be a non-integer value. The intent is to catch cases where it inappropriately being used as an integer value. This is transparent so long as the osal_id_t typedef and provided check and conversion routines are used.
1 parent 4cc6dbb commit bd4c650

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/os/inc/common_types.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,24 @@ extern "C"
9191
typedef size_t cpusize;
9292
typedef ptrdiff_t cpudiff;
9393

94+
#ifdef OSAL_OMIT_DEPRECATED
9495
/**
9596
* A type to be used for OSAL resource identifiers.
97+
* This is a type-safe ID, and cannot be implicitly converted to an integer.
98+
* Use the provided inline functions in osapi-idmap.h to interpret ID values.
9699
*/
97-
typedef uint32_t osal_id_t;
100+
typedef struct
101+
{
102+
uint32_t v;
103+
} osal_id_t;
104+
#else
105+
106+
/**
107+
* A type to be used for OSAL resource identifiers.
108+
* This typedef is backward compatible with the IDs from older versions of OSAL
109+
*/
110+
typedef uint32 osal_id_t;
111+
#endif
98112

99113
/**
100114
* A type used to represent a number of blocks or buffers

src/os/inc/osapi-idmap.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080
*/
8181
static inline unsigned long OS_ObjectIdToInteger(osal_id_t object_id)
8282
{
83+
#ifdef OSAL_OMIT_DEPRECATED
84+
return object_id.v;
85+
#else
8386
return object_id;
87+
#endif
8488
}
8589

8690
/*-------------------------------------------------------------------------------------*/
@@ -98,7 +102,11 @@ static inline unsigned long OS_ObjectIdToInteger(osal_id_t object_id)
98102
*/
99103
static inline osal_id_t OS_ObjectIdFromInteger(unsigned long value)
100104
{
105+
#ifdef OSAL_OMIT_DEPRECATED
106+
return (osal_id_t) {value};
107+
#else
101108
return (osal_id_t)value;
109+
#endif
102110
}
103111

104112
/*-------------------------------------------------------------------------------------*/
@@ -119,7 +127,7 @@ static inline osal_id_t OS_ObjectIdFromInteger(unsigned long value)
119127
*/
120128
static inline bool OS_ObjectIdEqual(osal_id_t object_id1, osal_id_t object_id2)
121129
{
122-
return (object_id1 == object_id2);
130+
return (OS_ObjectIdToInteger(object_id1) == OS_ObjectIdToInteger(object_id2));
123131
}
124132

125133
/*-------------------------------------------------------------------------------------*/
@@ -140,7 +148,7 @@ static inline bool OS_ObjectIdEqual(osal_id_t object_id1, osal_id_t object_id2)
140148
*/
141149
static inline bool OS_ObjectIdDefined(osal_id_t object_id)
142150
{
143-
return (object_id != 0);
151+
return (OS_ObjectIdToInteger(object_id) != 0);
144152
}
145153

146154
/*-------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)