Skip to content

Commit

Permalink
Fix nasa#1208, typesafe definition of osal_id_t
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jphickey committed Jan 18, 2022
1 parent 4cc6dbb commit bd4c650
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/os/inc/common_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,24 @@ extern "C"
typedef size_t cpusize;
typedef ptrdiff_t cpudiff;

#ifdef OSAL_OMIT_DEPRECATED
/**
* A type to be used for OSAL resource identifiers.
* This is a type-safe ID, and cannot be implicitly converted to an integer.
* Use the provided inline functions in osapi-idmap.h to interpret ID values.
*/
typedef uint32_t osal_id_t;
typedef struct
{
uint32_t v;
} osal_id_t;
#else

/**
* A type to be used for OSAL resource identifiers.
* This typedef is backward compatible with the IDs from older versions of OSAL
*/
typedef uint32 osal_id_t;
#endif

/**
* A type used to represent a number of blocks or buffers
Expand Down
12 changes: 10 additions & 2 deletions src/os/inc/osapi-idmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
*/
static inline unsigned long OS_ObjectIdToInteger(osal_id_t object_id)
{
#ifdef OSAL_OMIT_DEPRECATED
return object_id.v;
#else
return object_id;
#endif
}

/*-------------------------------------------------------------------------------------*/
Expand All @@ -98,7 +102,11 @@ static inline unsigned long OS_ObjectIdToInteger(osal_id_t object_id)
*/
static inline osal_id_t OS_ObjectIdFromInteger(unsigned long value)
{
#ifdef OSAL_OMIT_DEPRECATED
return (osal_id_t) {value};
#else
return (osal_id_t)value;
#endif
}

/*-------------------------------------------------------------------------------------*/
Expand All @@ -119,7 +127,7 @@ static inline osal_id_t OS_ObjectIdFromInteger(unsigned long value)
*/
static inline bool OS_ObjectIdEqual(osal_id_t object_id1, osal_id_t object_id2)
{
return (object_id1 == object_id2);
return (OS_ObjectIdToInteger(object_id1) == OS_ObjectIdToInteger(object_id2));
}

/*-------------------------------------------------------------------------------------*/
Expand All @@ -140,7 +148,7 @@ static inline bool OS_ObjectIdEqual(osal_id_t object_id1, osal_id_t object_id2)
*/
static inline bool OS_ObjectIdDefined(osal_id_t object_id)
{
return (object_id != 0);
return (OS_ObjectIdToInteger(object_id) != 0);
}

/*-------------------------------------------------------------------------------------*/
Expand Down

0 comments on commit bd4c650

Please sign in to comment.