Program: 2821
Project: 2821
Buggy source:
/** 
 * Returns the directionality of the first character with strong directionality in the string, or Dir.UNKNOWN if none was encountered. Treats a non-BN character between an LRE/RLE/LRO/RLO and its matching PDF as a strong character, LTR after LRE/LRO, and RTL after RLE/RLO. The results are undefined for a string containing unbalanced LRE/RLE/LRO/RLO/PDF characters.
 */
Dir getEntryDir(){
  charIndex=0;
  int embeddingLevel=0;
  Dir embeddingLevelDir=Dir.UNKNOWN;
  int firstNonEmptyEmbeddingLevel=0;
  while (charIndex < length && firstNonEmptyEmbeddingLevel == 0) {
switch (dirTypeForward()) {
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
      ++embeddingLevel;
    embeddingLevelDir=Dir.LTR;
  break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
++embeddingLevel;
embeddingLevelDir=Dir.RTL;
break;
case Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT:
--embeddingLevel;
embeddingLevelDir=Dir.UNKNOWN;
break;
case Character.DIRECTIONALITY_BOUNDARY_NEUTRAL:
break;
case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
if (embeddingLevel == 0) {
return Dir.LTR;
}
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
if (embeddingLevel == 0) {
return Dir.RTL;
}
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
default :
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
}
}
if (firstNonEmptyEmbeddingLevel == 0) {
return Dir.UNKNOWN;
}
if (embeddingLevelDir != Dir.UNKNOWN) {
return embeddingLevelDir;
}
while (charIndex > 0) {
switch (dirTypeBackward()) {
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
if (firstNonEmptyEmbeddingLevel == embeddingLevel) {
return Dir.LTR;
}
--embeddingLevel;
break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
if (firstNonEmptyEmbeddingLevel == embeddingLevel) {
return Dir.RTL;
}
--embeddingLevel;
break;
case Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT:
++embeddingLevel;
break;
}
}
return Dir.UNKNOWN;
}

Fixed version (ground truth):
/** 
 * Returns the directionality of the first character with strong directionality in the string, or DIR_UNKNOWN if none was encountered. Treats a non-BN character between an LRE/RLE/LRO/RLO and its matching PDF as a strong character, LTR after LRE/LRO, and RTL after RLE/RLO. The results are undefined for a string containing unbalanced LRE/RLE/LRO/RLO/PDF characters.
 */
int getEntryDir(){
  charIndex=0;
  int embeddingLevel=0;
  int embeddingLevelDir=DIR_UNKNOWN;
  int firstNonEmptyEmbeddingLevel=0;
  while (charIndex < length && firstNonEmptyEmbeddingLevel == 0) {
switch (dirTypeForward()) {
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
      ++embeddingLevel;
    embeddingLevelDir=DIR_LTR;
  break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
++embeddingLevel;
embeddingLevelDir=DIR_RTL;
break;
case Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT:
--embeddingLevel;
embeddingLevelDir=DIR_UNKNOWN;
break;
case Character.DIRECTIONALITY_BOUNDARY_NEUTRAL:
break;
case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
if (embeddingLevel == 0) {
return DIR_LTR;
}
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
if (embeddingLevel == 0) {
return DIR_RTL;
}
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
default :
firstNonEmptyEmbeddingLevel=embeddingLevel;
break;
}
}
if (firstNonEmptyEmbeddingLevel == 0) {
return DIR_UNKNOWN;
}
if (embeddingLevelDir != DIR_UNKNOWN) {
return embeddingLevelDir;
}
while (charIndex > 0) {
switch (dirTypeBackward()) {
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
case Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
if (firstNonEmptyEmbeddingLevel == embeddingLevel) {
return DIR_LTR;
}
--embeddingLevel;
break;
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
if (firstNonEmptyEmbeddingLevel == embeddingLevel) {
return DIR_RTL;
}
--embeddingLevel;
break;
case Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT:
++embeddingLevel;
break;
}
}
return DIR_UNKNOWN;
}