Skip to content

Commit

Permalink
lib: posix: Make statements evaluate boolean expressions
Browse files Browse the repository at this point in the history
MISRA-C requires that the if statement has essentially Boolean type.

MISRA-C rule 14.4

Signed-off-by: Flavio Ceolin <[email protected]>
  • Loading branch information
Flavio Ceolin authored and nashif committed Jan 7, 2019
1 parent 6a4a86e commit 0c4bb83
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
17 changes: 9 additions & 8 deletions lib/posix/mqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mqd_t mq_open(const char *name, int oflags, ...)
char *mq_desc_ptr, *mq_obj_ptr, *mq_buf_ptr, *mq_name_ptr;

va_start(va, oflags);
if (oflags & O_CREAT) {
if ((oflags & O_CREAT) != 0) {
mode = va_arg(va, mode_t);
attrs = va_arg(va, mq_attr*);
}
Expand All @@ -68,8 +68,8 @@ mqd_t mq_open(const char *name, int oflags, ...)
max_msgs = attrs->mq_maxmsg;
}

if (name == NULL || ((oflags & O_CREAT) && (msg_size <= 0 ||
max_msgs <= 0))) {
if ((name == NULL) || ((oflags & O_CREAT) != 0 && (msg_size <= 0 ||
max_msgs <= 0))) {
errno = EINVAL;
return (mqd_t)mqd;
}
Expand All @@ -84,13 +84,14 @@ mqd_t mq_open(const char *name, int oflags, ...)
msg_queue = find_in_list(name);
k_sem_give(&mq_sem);

if ((msg_queue != NULL) && (oflags & O_CREAT) && (oflags & O_EXCL)) {
if ((msg_queue != NULL) && (oflags & O_CREAT) != 0 &&
(oflags & O_EXCL) != 0) {
/* Message queue has alreadey been opened and O_EXCL is set */
errno = EEXIST;
return (mqd_t)mqd;
}

if (msg_queue == NULL && !(oflags & O_CREAT)) {
if ((msg_queue == NULL) && (oflags & O_CREAT) == 0) {
errno = ENOENT;
return (mqd_t)mqd;
}
Expand Down Expand Up @@ -157,7 +158,7 @@ mqd_t mq_open(const char *name, int oflags, ...)
}

msg_queue_desc->mqueue = msg_queue;
msg_queue_desc->flags = (oflags & O_NONBLOCK) ? O_NONBLOCK : 0;
msg_queue_desc->flags = (oflags & O_NONBLOCK) != 0 ? O_NONBLOCK : 0;
return (mqd_t)msg_queue_desc;

free_mq_buffer:
Expand Down Expand Up @@ -374,7 +375,7 @@ static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
return ret;
}

if (mqd->flags & O_NONBLOCK) {
if ((mqd->flags & O_NONBLOCK) != 0) {
timeout = K_NO_WAIT;
}

Expand Down Expand Up @@ -406,7 +407,7 @@ static s32_t receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
return ret;
}

if (mqd->flags & O_NONBLOCK) {
if ((mqd->flags & O_NONBLOCK) != 0) {
timeout = K_NO_WAIT;
}

Expand Down
27 changes: 14 additions & 13 deletions lib/posix/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr,
{
int priority = schedparam->priority;

if (!attr || !attr->initialized ||
if ((attr == NULL) || (attr->initialized == 0) ||
(is_posix_prio_valid(priority, attr->schedpolicy) == false)) {
return EINVAL;
}
Expand Down Expand Up @@ -140,7 +140,8 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
* pointer and stack size. So even though POSIX 1003.1 spec accepts
* attrib as NULL but zephyr needs it initialized with valid stack.
*/
if (!attr || !attr->initialized || !attr->stack || !attr->stacksize) {
if ((attr == NULL) || (attr->initialized == 0)
|| (attr->stack == NULL) || (attr->stacksize == 0)) {
return EINVAL;
}

Expand Down Expand Up @@ -216,7 +217,7 @@ int pthread_cancel(pthread_t pthread)
struct posix_thread *thread = (struct posix_thread *) pthread;
int cancel_state;

if (thread == NULL || thread->state == PTHREAD_TERMINATED) {
if ((thread == NULL) || (thread->state == PTHREAD_TERMINATED)) {
return ESRCH;
}

Expand Down Expand Up @@ -299,7 +300,7 @@ int pthread_getschedparam(pthread_t pthread, int *policy,
struct posix_thread *thread = (struct posix_thread *) pthread;
u32_t priority;

if (thread == NULL || thread->state == PTHREAD_TERMINATED) {
if ((thread == NULL) || (thread->state == PTHREAD_TERMINATED)) {
return ESRCH;
}

Expand Down Expand Up @@ -398,7 +399,7 @@ int pthread_join(pthread_t thread, void **status)
}

if (pthread->state == PTHREAD_EXITED) {
if (status) {
if (status != NULL) {
*status = pthread->retval;
}
} else if (pthread->state == PTHREAD_DETACHED) {
Expand Down Expand Up @@ -460,7 +461,7 @@ int pthread_detach(pthread_t thread)
*/
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
{
if (!attr || !attr->initialized) {
if ((attr == NULL) || (attr->initialized == 0)) {
return EINVAL;
}

Expand All @@ -475,7 +476,7 @@ int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
*/
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
{
if (!attr || !attr->initialized ||
if ((attr == NULL) || (attr->initialized == 0) ||
(detachstate != PTHREAD_CREATE_DETACHED &&
detachstate != PTHREAD_CREATE_JOINABLE)) {
return EINVAL;
Expand All @@ -493,7 +494,7 @@ int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
*/
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
{
if (!attr || !attr->initialized) {
if ((attr == NULL) || (attr->initialized == 0)) {
return EINVAL;
}

Expand All @@ -509,7 +510,7 @@ int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
*/
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
{
if (!attr || !attr->initialized ||
if ((attr == NULL) || (attr->initialized == 0) ||
(policy != SCHED_RR && policy != SCHED_FIFO)) {
return EINVAL;
}
Expand All @@ -525,7 +526,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
*/
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
{
if (!attr || !attr->initialized) {
if ((attr == NULL) || (attr->initialized == 0)) {
return EINVAL;
}

Expand All @@ -542,7 +543,7 @@ int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
int pthread_attr_getstack(const pthread_attr_t *attr,
void **stackaddr, size_t *stacksize)
{
if (!attr || !attr->initialized) {
if ((attr == NULL) || (attr->initialized == 0)) {
return EINVAL;
}

Expand All @@ -559,7 +560,7 @@ int pthread_attr_getstack(const pthread_attr_t *attr,
int pthread_attr_getschedparam(const pthread_attr_t *attr,
struct sched_param *schedparam)
{
if (!attr || !attr->initialized) {
if ((attr == NULL) || (attr->initialized == 0)) {
return EINVAL;
}

Expand All @@ -574,7 +575,7 @@ int pthread_attr_getschedparam(const pthread_attr_t *attr,
*/
int pthread_attr_destroy(pthread_attr_t *attr)
{
if (attr && attr->initialized) {
if ((attr != NULL) && (attr->initialized != 0)) {
attr->initialized = false;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/posix/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
}

/* Save time to expire and old reload value. */
if (ovalue) {
if (ovalue != NULL) {
timer_gettime(timerid, ovalue);
}

Expand All @@ -159,7 +159,7 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,

/* Calcaulte timer duration */
duration = _ts_to_ms(&(value->it_value));
if (flags & TIMER_ABSTIME) {
if ((flags & TIMER_ABSTIME) != 0) {
current = k_timer_remaining_get(&timer->ztimer);

if (current >= duration) {
Expand Down

0 comments on commit 0c4bb83

Please sign in to comment.