Program: 31477
Project: 31477
Buggy source:
public boolean parse(long ptr,int len,MultipartListener listener) throws InvalidMultipartHeader, HeadersTooLargeException {
  long hi=ptr + len;
  long _lo=Long.MAX_VALUE;
  char b;
  while (ptr < hi) {
switch (state) {
case BODY_BROKEN:
      _lo=ptr;
    state=State.BODY_CONTINUED;
  break;
case START_BOUNDARY:
boundaryPtr=1;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
state=State.PARTIAL_START_BOUNDARY;
return false;
case MATCH:
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
default :
throw InvalidMultipartHeader.INSTANCE;
}
break;
case PARTIAL_START_BOUNDARY:
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
return false;
case MATCH:
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
default :
throw InvalidMultipartHeader.INSTANCE;
}
break;
case PRE_HEADERS:
switch (Unsafe.getUnsafe().getByte(ptr)) {
case '\r':
ptr++;
break;
case '\n':
state=State.HEADERS;
ptr++;
break;
case '-':
return false;
default :
_lo=ptr;
state=State.BODY_CONTINUED;
}
break;
case HEADERS:
hb.clear();
case PARTIAL_HEADERS:
ptr=hb.write(ptr,(int)(hi - ptr),false);
if (hb.isIncomplete()) {
state=State.PARTIAL_HEADERS;
return false;
}
_lo=ptr;
state=State.BODY;
break;
case BODY_CONTINUED:
case BODY:
b=(char)Unsafe.getUnsafe().getByte(ptr++);
if (b == boundary.charAt(0)) {
boundaryPtr=1;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
listener.onChunk(hb,bytes.of(_lo,ptr - 1),state == State.BODY_CONTINUED);
state=State.POTENTIAL_BOUNDARY;
return false;
case MATCH:
listener.onChunk(hb,bytes.of(_lo,ptr - 1),state == State.BODY_CONTINUED);
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
}
}
break;
case POTENTIAL_BOUNDARY:
int p=boundaryPtr;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
return false;
case MATCH:
ptr+=consumedBoundaryLen;
state=State.PRE_HEADERS;
break;
case NO_MATCH:
listener.onChunk(hb,chars.of(boundary,0,p),true);
state=State.BODY_BROKEN;
break;
}
break;
}
}
if (state == State.BODY || state == State.BODY_CONTINUED) {
listener.onChunk(hb,bytes.of(_lo,ptr),state == State.BODY_CONTINUED);
state=State.BODY_BROKEN;
}
return false;
}

Fixed version (ground truth):
public boolean parse(long ptr,int len,MultipartListener listener) throws InvalidMultipartHeader, HeadersTooLargeException {
  long hi=ptr + len;
  long _lo=Long.MAX_VALUE;
  char b;
  while (ptr < hi) {
switch (state) {
case BODY_BROKEN:
      _lo=ptr;
    state=State.BODY_CONTINUED;
  break;
case START_BOUNDARY:
boundaryPtr=1;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
state=State.PARTIAL_START_BOUNDARY;
return false;
case MATCH:
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
default :
throw InvalidMultipartHeader.INSTANCE;
}
break;
case PARTIAL_START_BOUNDARY:
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
return false;
case MATCH:
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
default :
throw InvalidMultipartHeader.INSTANCE;
}
break;
case PRE_HEADERS:
switch (Unsafe.getUnsafe().getByte(ptr)) {
case '\r':
ptr++;
break;
case '\n':
state=State.HEADERS;
ptr++;
break;
case '-':
return true;
default :
_lo=ptr;
state=State.BODY_CONTINUED;
}
break;
case HEADERS:
hb.clear();
case PARTIAL_HEADERS:
ptr=hb.write(ptr,(int)(hi - ptr),false);
if (hb.isIncomplete()) {
state=State.PARTIAL_HEADERS;
return false;
}
_lo=ptr;
state=State.BODY;
break;
case BODY_CONTINUED:
case BODY:
b=(char)Unsafe.getUnsafe().getByte(ptr++);
if (b == boundary.charAt(0)) {
boundaryPtr=1;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
listener.onChunk(hb,bytes.of(_lo,ptr - 1),state == State.BODY_CONTINUED);
state=State.POTENTIAL_BOUNDARY;
return false;
case MATCH:
listener.onChunk(hb,bytes.of(_lo,ptr - 1),state == State.BODY_CONTINUED);
state=State.PRE_HEADERS;
ptr+=consumedBoundaryLen;
break;
}
}
break;
case POTENTIAL_BOUNDARY:
int p=boundaryPtr;
switch (matchBoundary(ptr,hi)) {
case INCOMPLETE:
return false;
case MATCH:
ptr+=consumedBoundaryLen;
state=State.PRE_HEADERS;
break;
case NO_MATCH:
listener.onChunk(hb,chars.of(boundary,0,p),true);
state=State.BODY_BROKEN;
break;
}
break;
}
}
if (state == State.BODY || state == State.BODY_CONTINUED) {
listener.onChunk(hb,bytes.of(_lo,ptr),state == State.BODY_CONTINUED);
state=State.BODY_BROKEN;
}
return false;
}