Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
langc
typedef struct {
  int balance;
  mtx_t balance_mutex;
} bank_account;

typedef struct {
  bank_account *from;
  bank_account *to;
  int amount;
} deposit_thr_args;

void create_bank_account(bank_account **ba, int initial_amount) {
  int result;
  bank_account *nba = malloc(sizeof(bank_account));
  if (nba == NULL) {
    /* Handle Error */
  }

  nba->balance = initial_amount;
  result = mtx_init(&nba->balance_mutex, mtx_plain);
  if (result == thrd_error) {
    /* Handle Errorerror */
  }

  *ba = nba;
}


void *deposit(void *ptr) {
  int result;
  deposit_thr_args *args = (deposit_thr_args *)ptr;

  if ((result = mtx_lock(&(args->from->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }

  /* not enough balance to transfer */
  if (args->from->balance < args->amount) {
    if ((result = mtx_unlock(&(args->from->balance_mutex))) != thrd_success) {
      /* Handle Errorerror  */
    }
    return NULL;
  }

  if ((result = mtx_lock(&(args->to->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }

  args->from->balance -= args->amount;
  args->to->balance += args->amount;

  if ((result = mtx_unlock(&(args->from->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }
  if ((result = mtx_unlock(&(args->to->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }


  free(ptr);
  return NULL;
}


int main(void) {

  pthread_t thr1, thr2;
  int result;

  bank_account *ba1;
  bank_account *ba2;
  create_bank_account(&ba1, 1000);
  create_bank_account(&ba2, 1000);

  deposit_thr_args *arg1 = malloc(sizeof(deposit_thr_args));
  if (arg1 == NULL) {
    /* Handle Errorerror */
  }
  deposit_thr_args *arg2 = malloc(sizeof(deposit_thr_args));
  if (arg2 == NULL) {
    /* Handle Errorerror */
  }

  arg1->from = ba1;
  arg1->to = ba2;
  arg1->amount = 100;

  arg2->from = ba2;
  arg2->to = ba1;
  arg2->amount = 100;

  /* perform the deposits */
  if ((result = thrd_create(&thr1, deposit, (void *)arg1)) != thrd_success) {
    /* Handle Errorerror */
  }
  if ((result = thrd_create(&thr2, deposit, (void *)arg2)) != thrd_success) {
    /* Handle Errorerror */
  }

  thrd_exit(NULL);
  return 0;
}

...

Code Block
bgColor#ccccff
langc
typedef struct {
  int balance;
  mtx_t balance_mutex;
  unsigned int id; /* shouldShould never be changed after initialized */
} bank_account;

unsigned int global_id = 1;

void create_bank_account(bank_account **ba, int initial_amount) {
  int result;
  bank_account *nba = malloc(sizeof(bank_account));
  if (nba == NULL) {
    /* Handle Errorerror */
  }

  nba->balance = initial_amount;
  result = mtx_init(&nba->balance_mutex, mtx_plain);
  if (result != thrd_success) {
    /* Handle Errorerror */
  }

  nba->id = global_id++;
  *ba = nba;
}


void *deposit(void *ptr) {
  deposit_thr_args *args = (deposit_thr_args *)ptr;
  int result;

  if (args->from->id == args->to->id)
		return;

  /* ensureEnsure proper ordering for locking */
  if (args->from->id < args->to->id) {
    if ((result = mtx_lock(&(args->from->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
    if ((result = mtx_lock(&(args->to->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
  } else {
    if ((result = mtx_lock(&(args->to->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
    if ((result = mtx_lock(&(args->from->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
  }

  /* not enough balance to transfer */
  if (args->from->balance < args->amount) {
    if ((result = mtx_unlock(&(args->from->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
    if ((result = mtx_unlock(&(args->to->balance_mutex))) != thrd_success) {
      /* Handle Errorerror */
    }
    return;
  }

  args->from->balance -= args->amount;
  args->to->balance += args->amount;

  if ((result = mtx_unlock(&(args->from->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }
  if ((result = mtx_unlock(&(args->to->balance_mutex))) != thrd_success) {
    /* Handle Errorerror */
  }


  free(ptr);
  return;
}

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

CON35-C

low

probable

medium

P4

L3

Automated Detection

ToolVersionCheckerDescription
Coverity6.5DEADLOCKFully Implemented

Related Guidelines

...

...

...

...

, Multiple locks of critical resources

Bibliography

[Barney 2010]pthread_mutex tutorial
[Bryant 2003]Chapter 13, "Concurrent Programming"

...