Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong usage of NXSEM_INITIALIZER #6084

Closed
pkarashchenko opened this issue Apr 16, 2022 · 0 comments · Fixed by #6085
Closed

Wrong usage of NXSEM_INITIALIZER #6084

pkarashchenko opened this issue Apr 16, 2022 · 0 comments · Fixed by #6085

Comments

@pkarashchenko
Copy link
Contributor

During code review I've noticed that few places in code use NXSEM_INITIALIZER(0, SEM_PRIO_NONE) as static initializer. Those places are:

  • fs/aio/aio_initialize.c:
static sem_t g_aioc_freesem = NXSEM_INITIALIZER(CONFIG_FS_NAIOC,
                                                SEM_PRIO_NONE);
  • sched/wqueue/kwork_thread.c:
#if defined(CONFIG_SCHED_HPWORK)
/* The state of the kernel mode, high priority work queue(s). */

struct hp_wqueue_s g_hpwork =
{
  {},
  NXSEM_INITIALIZER(0, SEM_PRIO_NONE),
};

#endif /* CONFIG_SCHED_HPWORK */

#if defined(CONFIG_SCHED_LPWORK)
/* The state of the kernel mode, low priority work queue(s). */

struct lp_wqueue_s g_lpwork =
{
  {},
  NXSEM_INITIALIZER(0, SEM_PRIO_NONE),
};

#endif /* CONFIG_SCHED_LPWORK */

Such usage is wrong as SEM_PRIO_NONE is defined to 0 in include/semaphore.h: #define SEM_PRIO_NONE 0 so NXSEM_INITIALIZER sets the flags member of struct sem_s to zero and what means SEM_PRIO_INHERIT if CONFIG_PRIORITY_INHERITANCE=y due to PRIOINHERIT_FLAGS_DISABLE is not set:

int nxsem_set_protocol(FAR sem_t *sem, int protocol)
{
  DEBUGASSERT(sem != NULL);

  switch (protocol)
    {
      case SEM_PRIO_NONE:

        /* Disable priority inheritance */

        sem->flags |= PRIOINHERIT_FLAGS_DISABLE;

        /* Remove any current holders */

        nxsem_destroyholder(sem);
        return OK;

      case SEM_PRIO_INHERIT:

        /* Enable priority inheritance (dangerous) */

        sem->flags &= ~PRIOINHERIT_FLAGS_DISABLE;
        return OK;

      case SEM_PRIO_PROTECT:

        /* Not yet supported */

        return -ENOTSUP;

      default:
        break;
    }

  return -EINVAL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant