Repository navigation
Description
Describe the bug
OS_BSP_Initialize() does not detect errors from the pthread functions.
It checks the return values of pthread_mutexattr_init(), pthread_mutexattr_setprotocol() and pthread_mutex_init() with if (status < 0). But these functions return 0 on success and a positive error number on failure. Thus the check is never true, and the BSP does not report the errors.
To Reproduce
This behavior is not specific to one platform. POSIX defines that these functions "shall return zero; otherwise, an error number shall be returned" (pthread_mutexattr_setprotocol, pthread_mutex_lock). Error numbers are positive. Thus status < 0 is never true.
On Ubuntu 22.04 with glibc 2.35, the normal path has no errors, because PTHREAD_PRIO_INHERIT is supported. To cause a failure, call pthread_mutexattr_setprotocol(&attr, 12345). The function returns 22 (EINVAL). The check status < 0 is false, and the BSP does not report the error.
Expected behavior
The BSP detects the failures and reports them with BSP_DEBUG.
Code snips
osal/src/bsp/generic-linux/src/bsp_start.c
Lines 81 to 95 in 2a96a4f
| status = pthread_mutexattr_init(&mutex_attr); | |
| if (status < 0) | |
| { | |
| BSP_DEBUG("pthread_mutexattr_init: %s\n", strerror(status)); | |
| } | |
| status = pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT); | |
| if (status < 0) | |
| { | |
| BSP_DEBUG("pthread_mutexattr_setprotocol: %s\n", strerror(status)); | |
| } | |
| status = pthread_mutex_init(&OS_BSP_GenericLinuxGlobal.AccessMutex, &mutex_attr); | |
| if (status < 0) | |
| { | |
| BSP_DEBUG("pthread_mutex_init: %s\n", strerror(status)); | |
| } |
System observed on:
Ubuntu 22.04, OSAL dev (2a96a4f)
Additional context
To fix this, check status != 0. The same incorrect check occurs in 15 locations. These are in OS_BSP_Initialize(), OS_BSP_Lock_Impl() and OS_BSP_Unlock_Impl() of the generic-linux, generic-qnx and generic-vxworks-rtp BSPs. One change can fix all of them.
Reporter Info
Jiahao Shi, Purdue University
Activity