Program: 62606
Project: 62606
Buggy source:
/** 
 * Get a string from the serial message
 * @param serialMessage
 * @param offset
 * @return String
 * @throws ZWaveSerialMessageException
 */
protected String getString(SerialMessage serialMessage,int offset) throws ZWaveSerialMessageException {
  int charPresentation=serialMessage.getMessagePayloadByte(offset + 1);
  charPresentation=0x07 & charPresentation;
switch (charPresentation) {
case ENCODING_ASCII:
    logger.debug("String_Node_Str",this.getNode().getNodeId());
  break;
case ENCODING_EXTENDED_ASCII:
logger.debug("String_Node_Str",this.getNode().getNodeId());
break;
case ENCODING_UTF16:
logger.debug("String_Node_Str",this.getNode().getNodeId());
break;
default :
logger.error("String_Node_Str",this.getNode().getNodeId(),charPresentation);
return null;
}
int numBytes=serialMessage.getMessagePayload().length - (offset + 2);
if (numBytes < 0) {
logger.error("String_Node_Str",this.getNode().getNodeId(),serialMessage.getMessagePayload().length);
return null;
}
if (numBytes == 0) {
return new String();
}
if (numBytes > MAX_STRING_LENGTH) {
logger.warn("String_Node_Str",this.getNode().getNodeId(),MAX_STRING_LENGTH,numBytes);
numBytes=MAX_STRING_LENGTH;
}
if (charPresentation != ENCODING_ASCII) {
logger.debug("String_Node_Str",getNode().getNodeId());
charPresentation=ENCODING_ASCII;
}
ByteArrayOutputStream baos=new ByteArrayOutputStream();
for (int c=0; c < numBytes; c++) {
if (serialMessage.getMessagePayloadByte(c + offset + 2) > 32 && serialMessage.getMessagePayloadByte(c + offset + 2) < 127) {
baos.write((byte)(serialMessage.getMessagePayloadByte(c + offset + 2)));
}
}
try {
return new String(baos.toByteArray(),"String_Node_Str");
}
 catch (UnsupportedEncodingException e) {
return null;
}
}

Fixed version (ground truth):
/** 
 * Get a string from the serial message
 * @param serialMessage
 * @param offset
 * @return String
 * @throws ZWaveSerialMessageException
 */
protected String getString(SerialMessage serialMessage,int offset) throws ZWaveSerialMessageException {
  if (serialMessage.getMessagePayload().length <= offset + 1) {
    return new String();
  }
  int charPresentation=serialMessage.getMessagePayloadByte(offset + 1);
  charPresentation=0x07 & charPresentation;
switch (charPresentation) {
case ENCODING_ASCII:
    logger.debug("String_Node_Str",getNode().getNodeId());
  break;
case ENCODING_EXTENDED_ASCII:
logger.debug("String_Node_Str",getNode().getNodeId());
break;
case ENCODING_UTF16:
logger.debug("String_Node_Str",getNode().getNodeId());
break;
default :
logger.error("String_Node_Str",getNode().getNodeId(),charPresentation);
return null;
}
int numBytes=serialMessage.getMessagePayload().length - (offset + 2);
if (numBytes < 0) {
logger.error("String_Node_Str",getNode().getNodeId(),serialMessage.getMessagePayload().length);
return null;
}
if (numBytes == 0) {
return new String();
}
if (numBytes > MAX_STRING_LENGTH) {
logger.warn("String_Node_Str",getNode().getNodeId(),MAX_STRING_LENGTH,numBytes);
numBytes=MAX_STRING_LENGTH;
}
if (charPresentation != ENCODING_ASCII) {
logger.debug("String_Node_Str",getNode().getNodeId());
charPresentation=ENCODING_ASCII;
}
ByteArrayOutputStream baos=new ByteArrayOutputStream();
for (int c=0; c < numBytes; c++) {
if (serialMessage.getMessagePayloadByte(c + offset + 2) > 32 && serialMessage.getMessagePayloadByte(c + offset + 2) < 127) {
baos.write((byte)(serialMessage.getMessagePayloadByte(c + offset + 2)));
}
}
try {
return new String(baos.toByteArray(),"String_Node_Str");
}
 catch (UnsupportedEncodingException e) {
return null;
}
}