Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
langc
mtx_t mutex;

void thread_foo(void *ptr) {
  uint32_t num;
  int result;
  int sock;

  /* sock is a connected TCP socket */
  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }
  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0) {
    /* Handle Errorerror */
  }

  /* ... */

  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }
}

int main() {
  thrd_t thread;
  int result;

  if ((result = mtx_init(&mutex, mtx_plain)) != thrd_success) {
    /* Handle Errorerror */
  }

  if (thrd_create(&thread,(void *)& thread_foo, NULL) != thrd_success) {
    /* Handle Errorerror */
  }

  /* ... */

  thrd_join(thread, NULL);

  if ((result = mtx_destroy(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  return 0;
}

Compliant Solution (Block while Not Locked)

...

Code Block
bgColor#ccccff
langc
void thread_foo(void *ptr) {
  uint32_t num;
  int result;
  int sock;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0) {
    /* Handle Errorerror */
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  /* ... */

  if ((result = pthread_mutex_unlock(&mutex)) != 0) {
    /* Handle Errorerror */
  }
}

Compliant Solution (Use a Nonblocking Call)

...

Code Block
bgColor#ccccff
langc
void thread_foo(void *ptr) {
  uint32_t num;
  int result;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), O_NONBLOCK)) < 0) {
    /* Handle Errorerror */
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  /* ... */

  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }
}

Exceptions

CON36-EX1: A thread may block while holding one or more locks and waiting to acquire another lock. When acquiring multiple locks, the order of locking must avoid deadlock, as specified in CON35-C. Avoid deadlock by locking in predefined order.

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON36-C

low

probable

high

P2

L3

Related Guidelines

...

...

...

Bibliography

...