Program: 5532
Project: 5532
Buggy source:
public void dataReceived(USARTSource source,int data){
  int oldStatus=status;
  if (DEBUG) {
    log("String_Node_Str" + Utils.hex8(data) + "String_Node_Str"+ ((data >= ' ' && data <= 'Z') ? (char)data : '.')+ ')'+ "String_Node_Str"+ chipSelect+ "String_Node_Str"+ state+ "String_Node_Str"+ stateMachine);
  }
  if (!chipSelect) {
  }
 else   if (stateMachine != RadioState.VREG_OFF) {
switch (state) {
case WAITING:
      if ((data & FLAG_READ) != 0) {
        state=SpiState.READ_REGISTER;
      }
 else {
        state=SpiState.WRITE_REGISTER;
      }
    if ((data & FLAG_RAM) != 0) {
      state=SpiState.RAM_ACCESS;
      address=data & 0x7f;
    }
 else {
      address=data & 0x3f;
      if (address == REG_RXFIFO) {
        state=SpiState.READ_RXFIFO;
      }
 else       if (address == REG_TXFIFO) {
        state=SpiState.WRITE_TXFIFO;
      }
    }
  if (data < 0x0f) {
    strobe(data);
    state=SpiState.WAITING;
  }
pos=0;
break;
case WRITE_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
registers[address]=(registers[address] & 0xff) | (data << 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
registers[address]=(registers[address] & 0xff00) | data;
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
data=registers[address];
setReg(address,data);
state=SpiState.WAITING;
}
break;
case READ_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
state=SpiState.WAITING;
}
return;
case READ_RXFIFO:
{
int fifoData=rxFIFO.read();
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
source.byteReceived(fifoData);
if (fifoP && !overflow) {
if (rxFIFO.length() <= fifopThr) {
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
setFIFOP(false);
}
}
if (rxfifoReadLeft == 0) {
rxfifoReadLeft=fifoData;
if (DEBUG) log("String_Node_Str" + rxfifoReadLeft + "String_Node_Str"+ rxFIFO.stateToString());
}
 else if (--rxfifoReadLeft == 0) {
if (rxFIFO.length() > 0) {
if (rxFIFO.length() > rxFIFO.peek(0) || (rxFIFO.length() > fifopThr && !decodeAddress && !frameRejected)) {
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
if (!overflow) setFIFOP(true);
}
}
}
if (rxFIFO.length() == 0) {
if (DEBUG) log("String_Node_Str");
setFIFO(false);
}
}
return;
case WRITE_TXFIFO:
if (txfifoFlush) {
txCursor=0;
txfifoFlush=false;
}
if (DEBUG) log("String_Node_Str" + data + "String_Node_Str"+ txCursor);
if (txCursor == 0) {
if ((data & 0xff) > 127) {
logger.warning(this,"String_Node_Str" + (data & 0xff));
}
}
 else if (txCursor > 127) {
logger.warning(this,"String_Node_Str");
txCursor=0;
}
memory[RAM_TXFIFO + txCursor]=data & 0xff;
txCursor++;
if (sendEvents) {
sendEvent("String_Node_Str",null);
}
break;
case RAM_ACCESS:
if (pos == 0) {
address|=(data << 1) & 0x180;
ramRead=(data & 0x20) != 0;
if (DEBUG) {
log("String_Node_Str" + Utils.hex16(address) + "String_Node_Str"+ ramRead);
}
pos++;
}
 else {
if (!ramRead) {
memory[address++]=data;
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
if (DEBUG && address == RAM_PANID + 2) {
log("String_Node_Str" + Utils.hex8(memory[RAM_PANID]) + Utils.hex8(memory[RAM_PANID + 1]));
}
}
 else {
source.byteReceived(memory[address++]);
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
return;
}
}
break;
}
source.byteReceived(oldStatus);
}
 else {
source.byteReceived(0);
logw("String_Node_Str");
}
}

Fixed version (ground truth):
public void dataReceived(USARTSource source,int data){
  int oldStatus=status;
  if (DEBUG) {
    log("String_Node_Str" + Utils.hex8(data) + "String_Node_Str"+ ((data >= ' ' && data <= 'Z') ? (char)data : '.')+ ')'+ "String_Node_Str"+ chipSelect+ "String_Node_Str"+ state+ "String_Node_Str"+ stateMachine);
  }
  if (!chipSelect) {
  }
 else   if (stateMachine != RadioState.VREG_OFF) {
switch (state) {
case WAITING:
      if ((data & FLAG_READ) != 0) {
        state=SpiState.READ_REGISTER;
      }
 else {
        state=SpiState.WRITE_REGISTER;
      }
    if ((data & FLAG_RAM) != 0) {
      state=SpiState.RAM_ACCESS;
      usartDataAddress=data & 0x7f;
    }
 else {
      usartDataAddress=data & 0x3f;
      if (usartDataAddress == REG_RXFIFO) {
        state=SpiState.READ_RXFIFO;
      }
 else       if (usartDataAddress == REG_TXFIFO) {
        state=SpiState.WRITE_TXFIFO;
      }
    }
  if (data < 0x0f) {
    strobe(data);
    state=SpiState.WAITING;
  }
usartDataPos=0;
break;
case WRITE_REGISTER:
if (usartDataPos == 0) {
source.byteReceived(registers[usartDataAddress] >> 8);
usartDataValue=data << 8;
usartDataPos=1;
}
 else {
source.byteReceived(registers[usartDataAddress] & 0xff);
usartDataValue|=data;
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(usartDataAddress) + "String_Node_Str"+ usartDataValue);
}
setReg(usartDataAddress,usartDataValue);
state=SpiState.WAITING;
}
break;
case READ_REGISTER:
if (usartDataPos == 0) {
source.byteReceived(registers[usartDataAddress] >> 8);
usartDataPos=1;
}
 else {
source.byteReceived(registers[usartDataAddress] & 0xff);
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(usartDataAddress) + "String_Node_Str"+ registers[usartDataAddress]);
}
state=SpiState.WAITING;
}
return;
case READ_RXFIFO:
{
int fifoData=rxFIFO.read();
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
source.byteReceived(fifoData);
if (currentFIFOP && !overflow) {
if (rxFIFO.length() <= fifopThr) {
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
setFIFOP(false);
}
}
if (rxfifoReadLeft == 0) {
rxfifoReadLeft=fifoData;
if (DEBUG) log("String_Node_Str" + rxfifoReadLeft + "String_Node_Str"+ rxFIFO.stateToString());
}
 else if (--rxfifoReadLeft == 0) {
if (rxFIFO.length() > 0) {
if (rxFIFO.length() > rxFIFO.peek(0) || (rxFIFO.length() > fifopThr && !decodeAddress && !frameRejected)) {
if (DEBUG) log("String_Node_Str" + rxFIFO.stateToString());
if (!overflow) setFIFOP(true);
}
}
}
if (rxFIFO.length() == 0) {
if (DEBUG) log("String_Node_Str");
setFIFO(false);
}
}
return;
case WRITE_TXFIFO:
if (txfifoFlush) {
txCursor=0;
txfifoFlush=false;
}
if (DEBUG) log("String_Node_Str" + data + "String_Node_Str"+ txCursor);
if (txCursor == 0) {
if ((data & 0xff) > 127) {
logger.warning(this,"String_Node_Str" + (data & 0xff));
}
}
 else if (txCursor > 127) {
logger.warning(this,"String_Node_Str");
txCursor=0;
}
memory[RAM_TXFIFO + txCursor]=data & 0xff;
txCursor++;
if (sendEvents) {
sendEvent("String_Node_Str",null);
}
break;
case RAM_ACCESS:
if (usartDataPos == 0) {
usartDataAddress|=(data << 1) & 0x180;
ramRead=(data & FLAG_RAM_READ) != 0;
if (DEBUG) {
log("String_Node_Str" + Utils.hex16(usartDataAddress) + "String_Node_Str"+ ramRead);
}
usartDataPos++;
}
 else {
if (!ramRead) {
memory[usartDataAddress++]=data;
if (usartDataAddress >= 0x180) {
logger.warning(this,"String_Node_Str");
usartDataAddress=0;
}
if (DEBUG && usartDataAddress == RAM_PANID + 2) {
log("String_Node_Str" + Utils.hex8(memory[RAM_PANID]) + Utils.hex8(memory[RAM_PANID + 1]));
}
}
 else {
source.byteReceived(memory[usartDataAddress++]);
if (usartDataAddress >= 0x180) {
logger.warning(this,"String_Node_Str");
usartDataAddress=0;
}
return;
}
}
break;
}
source.byteReceived(oldStatus);
}
 else {
source.byteReceived(0);
logw("String_Node_Str");
}
}