NShiftKey-Rule-Guide logo NShiftKey-Rule-Guide

1. Vulnerability Description

2. Vulnerability Countermeasure

3. Sample Code

import com.google.errorprone.annotations.concurrent.GuardedBy;

class Account {
  @GuardedBy(""this"")
  private int balance;

  public synchronized int getBalance() {
    return balance; // OK: implicit 'this' lock is held.
  }

  public synchronized void withdraw(int amount) {
    setBalance(balance - amount); // OK: implicit 'this' lock is held.
  }

  public void deposit(int amount) {
    setBalance(balance + amount); // ERROR: access to 'balance' not guarded by 'this'.
  }

  @GuardedBy(""this"")
  private void setBalance(int newBalance) {
    checkState(newBalance >= 0, ""Balance cannot be negative."");
    balance = newBalance; // OK: 'this' must be held by caller of 'setBalance'.
  }
}
import com.google.errorprone.annotations.concurrent.GuardedBy;

class Account {
  @GuardedBy(""this"")
  private int balance;

  public synchronized int getBalance() {
    return balance; // OK: implicit 'this' lock is held.
  }

  public synchronized void withdraw(int amount) {
    setBalance(balance - amount); // OK: implicit 'this' lock is held.
  }

  public synchronized void deposit(int amount) {
    setBalance(balance + amount);
  }

  @GuardedBy(""this"")
  private void setBalance(int newBalance) {
    checkState(newBalance >= 0, ""Balance cannot be negative."");
    balance = newBalance; // OK: 'this' must be held by caller of 'setBalance'.
  }
}