Versions Compared

Key

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

...

Code Block
bgColor#ccccff
#include <pthread<threads.h>

pthread_mutexmtx_t rand_lock = PTHREAD_MUTEX_INITIALIZER;; 

int get_secret() {

  int secret;

  pthread_mutexmtx_lock(&rand_lock) ;
  secret = (rand() % 100) + 100;
  pthread_mutexmtx_unlock(&rand_lock);

  return secret;

}


void init(){
  
  /* initialize a simple non-recursive mutex */
  if(mtx_init(&rand_lock, mtx_plain) == thrd_error){
    abort();
  }

  /* other initialization code */

}

Risk Assessment

Race conditions caused by multiple threads invoking the same library function can lead to abnormal termination of the application, data integrity violations or denial of service attack.

...