barty/D4J-Repair huggingface dataset:
File: src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
Issue: relational operations do not function properly when comparing a non-Iterator LHS to an Iterator RHS
I have a simple JXpathContext, with the following variables: var1=0, var2=0, var3=1. When I try to evaluate the following expression - "$var1 + $var2 <= $var3", it returns false.

Buggy code:
private boolean compute(Object left, Object right) {
        left = reduce(left);
        right = reduce(right);

        if (left instanceof InitialContext) {
            ((InitialContext) left).reset();
        }
        if (right instanceof InitialContext) {
            ((InitialContext) right).reset();
        }
        if (left instanceof Iterator && right instanceof Iterator) {
            return findMatch((Iterator) left, (Iterator) right);
        }
        if (left instanceof Iterator) {
            return containsMatch((Iterator) left, right);
        }
        if (right instanceof Iterator) {
            return containsMatch((Iterator) right, left);
        }
        double ld = InfoSetUtil.doubleValue(left);
        if (Double.isNaN(ld)) {
            return false;
        }
        double rd = InfoSetUtil.doubleValue(right);
        if (Double.isNaN(rd)) {
            return false;
        }
        return evaluateCompare(ld == rd ? 0 : ld < rd ? -1 : 1);
    }

Fixed code:
private boolean compute(Object left, Object right) {
        left = reduce(left);
        right = reduce(right);

        if (left instanceof InitialContext) {
            ((InitialContext) left).reset();
        }
        if (right instanceof InitialContext) {
            ((InitialContext) right).reset();
        }
        if (left instanceof Iterator && right instanceof Iterator) {
            return findMatch((Iterator) left, (Iterator) right);
        }
        if (left instanceof Iterator) {
            return containsMatch((Iterator) left, right);
        }
        if (right instanceof Iterator) {
            return containsMatch(left, (Iterator) right);
        }
        double ld = InfoSetUtil.doubleValue(left);
        if (Double.isNaN(ld)) {
            return false;
        }
        double rd = InfoSetUtil.doubleValue(right);
        if (Double.isNaN(rd)) {
            return false;
        }
        return evaluateCompare(ld == rd ? 0 : ld < rd ? -1 : 1);
    }
File: src/java/org/apache/commons/jxpath/ri/model/beans/PropertyPointer.java
Issue: null handling is inconsistent
Comparing a vaule to null using unequals (!=) yields false!

        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("a", 1);
        m.put("b", null);
        m.put("c", 1);
        JXPathContext c = JXPathContext.newContext(m);
        System.out.println(c.getValue("a != b") + " should be true");
        System.out.println(c.getValue("a != c") + " should be false");
        System.out.println(c.getValue("a = b") + " should be false");
        System.out.println(c.getValue("a = c") + " should be true");
        System.out.println(c.getValue("not(a = b)") + " should be true");
        System.out.println(c.getValue("not(a = c)") + " should be false");

Output using 1.3:
 false should be true
false should be false
false should be false
true should be true
true should be true
false should be false
In 1.2 it works correctly!

Buggy code:
public int getLength() {
    return ValueUtils.getLength(getBaseValue());
}

Fixed code:
public int getLength() {
    Object baseValue = getBaseValue();
    return baseValue == null ? 1 : ValueUtils.getLength(baseValue);
}
File: src/java/org/apache/commons/jxpath/ri/model/NodePointer.java
Issue: Cannot compare pointers that do not belong to the same tree
For XPath "$var | /MAIN/A" exception is thrown:
org.apache.commons.jxpath.JXPathException: Cannot compare pointers that do not belong to the same tree: '$var' and ''
	at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:665)
	at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:649)
	at org.apache.commons.jxpath.ri.model.NodePointer.compareNodePointers(NodePointer.java:649)
	at org.apache.commons.jxpath.ri.model.NodePointer.compareTo(NodePointer.java:639)
	at java.util.Arrays.mergeSort(Arrays.java:1152)
	at java.util.Arrays.sort(Arrays.java:1079)
	at java.util.Collections.sort(Collections.java:113)
	at org.apache.commons.jxpath.ri.EvalContext.constructIterator(EvalContext.java:176)
	at org.apache.commons.jxpath.ri.EvalContext.hasNext(EvalContext.java:100)
	at org.apache.commons.jxpath.JXPathContext.selectNodes(JXPathContext.java:648)
	at org.apache.commons.jxpath.ri.model.VariablePointerTestCase.testUnionOfVariableAndNode(VariablePointerTestCase.java:76)

Buggy code:
private int compareNodePointers(
    NodePointer p1,
    int depth1,
    NodePointer p2,
    int depth2) 
{
    if (depth1 < depth2) {
        int r = compareNodePointers(p1, depth1, p2.parent, depth2 - 1);
        return r == 0 ? -1 : r;
    }
    if (depth1 > depth2) {
        int r = compareNodePointers(p1.parent, depth1 - 1, p2, depth2);
        return r == 0 ? 1 : r;
    }
    if (p1 == null && p2 == null) {
        return 0;
    }

    if (p1 != null && p1.equals(p2)) {
        return 0;
    }

    if (depth1 == 1) {
        throw new JXPathException(
                "Cannot compare pointers that do not belong to the same tree: '"
                        + p1 + "' and '" + p2 + "'");
    }
    int r = compareNodePointers(p1.parent, depth1 - 1, p2.parent, depth2 - 1);
    if (r != 0) {
        return r;
    }

    return p1.parent.compareChildNodePointers(p1, p2);
}

Fixed code:
private int compareNodePointers(
    NodePointer p1,
    int depth1,
    NodePointer p2,
    int depth2) 
{
    if (depth1 < depth2) {
        int r = compareNodePointers(p1, depth1, p2.parent, depth2 - 1);
        return r == 0 ? -1 : r;
    }
    if (depth1 > depth2) {
        int r = compareNodePointers(p1.parent, depth1 - 1, p2, depth2);
        return r == 0 ? 1 : r;
    }
    if (p1 == null && p2 == null) {
        return 0;
    }

    if (p1 != null && p1.equals(p2)) {
        return 0;
    }

    if (depth1 == 1) {
        return 0;
    }
    int r = compareNodePointers(p1.parent, depth1 - 1, p2.parent, depth2 - 1);
    if (r != 0) {
        return r;
    }

    return p1.parent.compareChildNodePointers(p1, p2);
}
File: src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java
Issue: equality test for multi-valued variables does not conform to spec
given e.g. variable d=
{"a", "b"}
, the spec implies that "$d = 'a'" and that "$d = 'b'".  Instead of iterating the variable's components its immediate content (here, the String[]) is compared, causing the aforementioned assertions to fail.

Buggy code:
protected boolean equal(
    EvalContext context,
    Expression left,
    Expression right) 
{
    Object l = left.compute(context);
    Object r = right.compute(context);

      System.err.println("COMPARING: " +
          (l == null ? "null" : l.getClass().getName()) + " " +
          (r == null ? "null" : r.getClass().getName()));

    if (l instanceof InitialContext || l instanceof SelfContext) {
        l = ((EvalContext) l).getSingleNodePointer();
    }

    if (r instanceof InitialContext || r instanceof SelfContext) {
        r = ((EvalContext) r).getSingleNodePointer();
    }

    if (l instanceof Collection) {
        l = ((Collection) l).iterator();
    }

    if (r instanceof Collection) {
        r = ((Collection) r).iterator();
    }

    if ((l instanceof Iterator) && !(r instanceof Iterator)) {
        return contains((Iterator) l, r);
    }
    if (!(l instanceof Iterator) && (r instanceof Iterator)) {
        return contains((Iterator) r, l);
    }
    if (l instanceof Iterator && r instanceof Iterator) {
        return findMatch((Iterator) l, (Iterator) r);
    }
    return equal(l, r);
}

Fixed code:
protected boolean equal(
    EvalContext context,
    Expression left,
    Expression right) 
{
    Object l = left.compute(context);
    Object r = right.compute(context);

      System.err.println("COMPARING: " +
          (l == null ? "null" : l.getClass().getName()) + " " +
          (r == null ? "null" : r.getClass().getName()));

    if (l instanceof InitialContext) {
        ((EvalContext) l).reset();
    }

    if (l instanceof SelfContext) {
        l = ((EvalContext) l).getSingleNodePointer();
    }

    if (r instanceof InitialContext) {
        ((EvalContext) r).reset();
    }

    if (r instanceof SelfContext) {
        r = ((EvalContext) r).getSingleNodePointer();
    }

    if (l instanceof Collection) {
        l = ((Collection) l).iterator();
    }

    if (r instanceof Collection) {
        r = ((Collection) r).iterator();
    }

    if ((l instanceof Iterator) && !(r instanceof Iterator)) {
        return contains((Iterator) l, r);
    }
    if (!(l instanceof Iterator) && (r instanceof Iterator)) {
        return contains((Iterator) r, l);
    }
    if (l instanceof Iterator && r instanceof Iterator) {
        return findMatch((Iterator) l, (Iterator) r);
    }
    return equal(l, r);
}
File: src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationRelationalExpression.java
Issue: Comparing with NaN is incorrect
'NaN' > 'NaN' is true, but should be FALSE

Buggy code:
private boolean compute(Object left, Object right) {
    left = reduce(left);
    right = reduce(right);

    if (left instanceof InitialContext) {
        ((InitialContext) left).reset();
    }
    if (right instanceof InitialContext) {
        ((InitialContext) right).reset();
    }
    if (left instanceof Iterator && right instanceof Iterator) {
        return findMatch((Iterator) left, (Iterator) right);
    }
    if (left instanceof Iterator) {
        return containsMatch((Iterator) left, right);
    }
    if (right instanceof Iterator) {
        return containsMatch((Iterator) right, left);
    }
    double ld = InfoSetUtil.doubleValue(left);
    double rd = InfoSetUtil.doubleValue(right);
    return evaluateCompare(ld == rd ? 0 : ld < rd ? -1 : 1);
}

Fixed code:
private boolean compute(Object left, Object right) {
    left = reduce(left);
    right = reduce(right);

    if (left instanceof InitialContext) {
        ((InitialContext) left).reset();
    }
    if (right instanceof InitialContext) {
        ((InitialContext) right).reset();
    }
    if (left instanceof Iterator && right instanceof Iterator) {
        return findMatch((Iterator) left, (Iterator) right);
    }
    if (left instanceof Iterator) {
        return containsMatch((Iterator) left, right);
    }
    if (right instanceof Iterator) {
        return containsMatch((Iterator) right, left);
    }
    double ld = InfoSetUtil.doubleValue(left);
    if (Double.isNaN(ld)) {
        return false;
    }
    double rd = InfoSetUtil.doubleValue(right);
    if (Double.isNaN(rd)) {
        return false;
    }
    return evaluateCompare(ld == rd ? 0 : ld < rd ? -1 : 1);
}
File: src/main/java/org/apache/commons/lang3/time/FastDateParser.java
Issue: FastDateParser does not handle white-space properly
The SimpleDateFormat Javadoc does not treat white-space specially, however FastDateParser treats a single white-space as being any number of white-space characters.
This means that FDP will parse dates that fail when parsed by SDP.

Buggy code:
private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) {
    boolean wasWhite= false;
    for(int i= 0; i<value.length(); ++i) {
        char c= value.charAt(i);
        if(Character.isWhitespace(c)) {
            if(!wasWhite) {
                wasWhite= true;
                regex.append("\\s*+");
            }
            continue;
        }
        wasWhite= false;
        switch(c) {
        case '\'':
            if(unquote) {
                if(++i==value.length()) {
                    return regex;
                }
                c= value.charAt(i);
            }
            break;
        case '?':
        case '[':
        case ']':
        case '(':
        case ')':
        case '{':
        case '}':
        case '\\':
        case '|':
        case '*':
        case '+':
        case '^':
        case '$':
        case '.':
            regex.append('\\');
        }
        regex.append(c);
    }
    return regex;
}

Fixed code:
private static StringBuilder escapeRegex(StringBuilder regex, String value, boolean unquote) {
    for(int i= 0; i<value.length(); ++i) {
        char c= value.charAt(i);
        switch(c) {
        case '\'':
            if(unquote) {
                if(++i==value.length()) {
                    return regex;
                }
                c= value.charAt(i);
            }
            break;
        case '?':
        case '[':
        case ']':
        case '(':
        case ')':
        case '{':
        case '}':
        case '\\':
        case '|':
        case '*':
        case '+':
        case '^':
        case '$':
        case '.':
            regex.append('\\');
        }
        regex.append(c);
    }
    return regex;
}
File: src/main/java/org/apache/commons/lang3/StringUtils.java
Issue: StringUtils equals() relies on undefined behavior
Since the java.lang.CharSequence class was first introduced in 1.4, the JavaDoc block has contained the following note:

This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other.
When the signature of the StringUtils equals() method was changed from equals(String, String) to equals(CharSequence, CharSequence) in R920543, the implementation still relied on calling CharSequence#equals(Object) even though, in general, the result is undefined.
One example where equals(Object) returns false even though, as CharSequences, two objects represent equal sequences is when one object is an instance of javax.lang.model.element.Name and the other object is a String.

Buggy code:
public static boolean equals(CharSequence cs1, CharSequence cs2) {
    if (cs1 == cs2) {
        return true;
    }
    if (cs1 == null || cs2 == null) {
        return false;
    }
        return cs1.equals(cs2);
}

Fixed code:
public static boolean equals(CharSequence cs1, CharSequence cs2) {
    if (cs1 == cs2) {
        return true;
    }
    if (cs1 == null || cs2 == null) {
        return false;
    }
    if (cs1 instanceof String && cs2 instanceof String) {
        return cs1.equals(cs2);
    }
    return CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
}
File: src/main/java/org/apache/commons/lang3/text/translate/CharSequenceTranslator.java
Issue: StringEscapeUtils.escapeXml(input) outputs wrong results when an input contains characters in Supplementary Planes.
Hello.
I use StringEscapeUtils.escapeXml(input) to escape special characters for XML.
This method outputs wrong results when input contains characters in Supplementary Planes.
String str1 = "\uD842\uDFB7" + "A";
String str2 = StringEscapeUtils.escapeXml(str1);
// The value of str2 must be equal to the one of str1,
// because str1 does not contain characters to be escaped.
// However, str2 is diffrent from str1.
System.out.println(URLEncoder.encode(str1, "UTF-16BE")); //%D8%42%DF%B7A
System.out.println(URLEncoder.encode(str2, "UTF-16BE")); //%D8%42%DF%B7%FF%FD
The cause of this problem is that the loop to translate input character by character is wrong.
In CharSequenceTranslator.translate(CharSequence input, Writer out),
loop counter "i" moves from 0 to Character.codePointCount(input, 0, input.length()),
but it should move from 0 to input.length().

Buggy code:
public final void translate(CharSequence input, Writer out) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (input == null) {
        return;
    }
    int pos = 0;
    int len = Character.codePointCount(input, 0, input.length());
    while (pos < len) {
        int consumed = translate(input, pos, out);
        if (consumed == 0) {
            char[] c = Character.toChars(Character.codePointAt(input, pos));
            out.write(c);
        }
        else {
        // contract with translators is that they have to understand codepoints 
        // and they just took care of a surrogate pair
        for (int pt = 0; pt < consumed; pt++) {
                if (pos < len - 2) {
            pos += Character.charCount(Character.codePointAt(input, pos));
                } else {
                    pos++;
                }
            }
            pos--;
        }
        pos++;
    }
}

Fixed code:
public final void translate(CharSequence input, Writer out) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (input == null) {
        return;
    }
    int pos = 0;
    int len = input.length();
    while (pos < len) {
        int consumed = translate(input, pos, out);
        if (consumed == 0) {
            char[] c = Character.toChars(Character.codePointAt(input, pos));
            out.write(c);
            pos+= c.length;
            continue;
        }
        // contract with translators is that they have to understand codepoints 
        // and they just took care of a surrogate pair
        for (int pt = 0; pt < consumed; pt++) {
            pos += Character.charCount(Character.codePointAt(input, pos));
        }
    }
}
File: src/main/java/org/apache/commons/lang3/time/DateUtils.java
Issue: DateUtils.isSameLocalTime does not work correct
Hi, I think I found a bug in the DateUtils class in the method isSameLocalTime.
Example: 
Calendar a = Calendar.getInstance();
a.setTimeInMillis(1297364400000L);
Calendar b = Calendar.getInstance();
b.setTimeInMillis(1297321200000L);
Assert.assertFalse(DateUtils.isSameLocalTime(a, b));
This is because the method compares 
cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR) 
but I think it has to be 
cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY)

Buggy code:
public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) &&
            cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) &&
            cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE) &&
            cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR) &&
            cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
            cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
            cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
            cal1.getClass() == cal2.getClass());
}

Fixed code:
public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) &&
            cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) &&
            cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE) &&
            cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY) &&
            cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
            cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
            cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
            cal1.getClass() == cal2.getClass());
}
File: src/main/java/org/apache/commons/lang3/time/FastDateFormat.java
Issue: FastDateFormat.format() outputs incorrect week of year because locale isn't respected
FastDateFormat apparently doesn't respect the locale it was sent on creation when outputting week in year (e.g. "ww") in format(). It seems to use the settings of the system locale for firstDayOfWeek and minimalDaysInFirstWeek, which (depending on the year) may result in the incorrect week number being output.
Here is a simple test program to demonstrate the problem by comparing with SimpleDateFormat, which gets the week number right:

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;

import org.apache.commons.lang.time.FastDateFormat;

public class FastDateFormatWeekBugDemo {
    public static void main(String[] args) {
        Locale.setDefault(new Locale("en", "US"));
        Locale locale = new Locale("sv", "SE");

        Calendar cal = Calendar.getInstance(); // setting locale here doesn't change outcome
        cal.set(2010, 0, 1, 12, 0, 0);
        Date d = cal.getTime();
        System.out.println("Target date: " + d);

        FastDateFormat fdf = FastDateFormat.getInstance("EEEE', week 'ww", locale);
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE', week 'ww", locale);
        System.out.println("FastDateFormat:   " + fdf.format(d)); // will output "FastDateFormat:   fredag, week 01"
        System.out.println("SimpleDateFormat: " + sdf.format(d)); // will output "SimpleDateFormat: fredag, week 53"
    }
}

If sv/SE is passed to Locale.setDefault() instead of en/US, both FastDateFormat and SimpleDateFormat output the correct week number.

Buggy code:
public String format(Date date) {
    Calendar c = new GregorianCalendar(mTimeZone);
    c.setTime(date);
    return applyRules(c, new StringBuffer(mMaxLengthEstimate)).toString();
}

Fixed code:
public String format(Date date) {
    Calendar c = new GregorianCalendar(mTimeZone, mLocale);
    c.setTime(date);
    return applyRules(c, new StringBuffer(mMaxLengthEstimate)).toString();
}
File: src/main/java/org/apache/commons/lang3/SystemUtils.java
Issue: SystemUtils.getJavaVersionAsFloat throws StringIndexOutOfBoundsException on Android runtime/Dalvik VM
Can be replicated in the Android emulator quite easily.
Stack trace:

at org.apache.commons.lang.builder.ToStringBuilder.<clinit>(ToStringBuilder.java:98)
E/AndroidRuntime( 1681): 	... 17 more
E/AndroidRuntime( 1681): Caused by: java.lang.ExceptionInInitializerError
E/AndroidRuntime( 1681): 	at org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.<init>(ToStringStyle.java:2276)
E/AndroidRuntime( 1681): 	at org.apache.commons.lang.builder.ToStringStyle.<clinit>(ToStringStyle.java:94)
E/AndroidRuntime( 1681): 	... 18 more
E/AndroidRuntime( 1681): Caused by: java.lang.StringIndexOutOfBoundsException
E/AndroidRuntime( 1681): 	at java.lang.String.substring(String.java:1571)
E/AndroidRuntime( 1681): 	at org.apache.commons.lang.SystemUtils.getJavaVersionAsFloat(SystemUtils.java:1153)
E/AndroidRuntime( 1681): 	at org.apache.commons.lang.SystemUtils.<clinit>(SystemUtils.java:818)

Buggy code:
static float toJavaVersionInt(String version) {
    return toVersionInt(toJavaVersionIntArray(version, JAVA_VERSION_TRIM_SIZE));
}

Fixed code:
static int toJavaVersionInt(String version) {
    return toVersionInt(toJavaVersionIntArray(version, JAVA_VERSION_TRIM_SIZE));
}