Skip to content

Incorrect pthread error check (status < 0) in OS_BSP_Initialize misses all failures #1617

Backlog

Description

@steven1518

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions