input
stringlengths 205
73.3k
| output
stringlengths 64
73.2k
| instruction
stringclasses 1
value |
---|---|---|
#vulnerable code
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new MappingCouchbaseConverter());
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
#location 7
#vulnerability type RESOURCE_LEAK | #fixed code
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// ReactiveCouchbaseTemplate personTemplate =
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new MappingCouchbaseConverter());
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
#location 7
#vulnerability type RESOURCE_LEAK | #fixed code
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new MappingCouchbaseConverter());
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// ReactiveCouchbaseTemplate personTemplate =
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new MappingCouchbaseConverter());
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@ManagedMetric(description = "Total RAM used")
public long getTotalRAMUsed() {
return (Long) parseStorageTotals().get("ram").get("used");
}
#location 3
#vulnerability type NULL_DEREFERENCE | #fixed code
@ManagedMetric(description = "Total RAM used")
public long getTotalRAMUsed() {
return convertPotentialLong(parseStorageTotals().get("ram").get("used"));
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testUnicodeEscape() throws Exception {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
String[] data = parser.iterator().next();
assertEquals(2, data.length);
assertEquals("abc", data[0]);
assertEquals("public", data[1]);
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
public void testUnicodeEscape() throws Exception {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
final Iterator<String[]> iterator = parser.iterator();
String[] data = iterator.next();
assertEquals(2, data.length);
assertEquals("abc", data[0]);
assertEquals("public", data[1]);
assertFalse("Should not have any more records", iterator.hasNext());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
String[] tmp = null;
for (int i = 0; i < res.length; i++) {
tmp = parser.getLine();
assertTrue(Arrays.equals(res[i], tmp));
}
tmp = parser.getLine();
assertTrue(tmp == null);
}
#location 7
#vulnerability type RESOURCE_LEAK | #fixed code
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
for (String[] re : res) {
assertTrue(Arrays.equals(re, parser.getLine()));
}
assertTrue(parser.getLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrinter5() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\nc");
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrinter5() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\nc");
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCarriageReturnLineFeedEndings() throws IOException {
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
final CSVParser parser = new CSVParser(new StringReader(code));
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCarriageReturnLineFeedEndings() throws IOException {
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
}
#location 17
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br.close();
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
br.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCarriageReturnEndings() throws IOException {
final String code = "foo\rbaar,\rhello,world\r,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCarriageReturnEndings() throws IOException {
final String code = "foo\rbaar,\rhello,world\r,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testMutators() {
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getQuoteChar().charValue());
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape().charValue());
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
}
#location 6
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testMutators() {
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withQuoteChar('?').getQuoteChar().charValue());
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape().charValue());
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLine() throws Exception {
br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLine() throws Exception {
br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
}
#location 9
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCSVFile() throws Exception {
String line = readTestData();
assertNotNull("file must contain config line", line);
final String[] split = line.split(" ");
assertTrue(testName+" require 1 param", split.length >= 1);
// first line starts with csv data file name
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
boolean checkComments = false;
for(int i=1; i < split.length; i++) {
final String option = split[i];
final String[] option_parts = option.split("=",2);
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
format = format.withCommentStart(option_parts[1].charAt(0));
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
checkComments = true;
} else {
fail(testName+" unexpected option: "+option);
}
}
line = readTestData(); // get string version of format
assertEquals(testName+" Expected format ", line, format.toString());
// Now parse the file and compare against the expected results
// We use a buffered reader internally so no need to create one here.
final CSVParser parser = CSVParser.parseFile(new File(BASE, split[0]), format);
for(final CSVRecord record : parser) {
String parsed = record.toString();
if (checkComments) {
final String comment = record.getComment().replace("\n", "\\n");
if (comment != null) {
parsed += "#" + comment;
}
}
final int count = record.size();
assertEquals(testName, readTestData(), count+":"+parsed);
}
}
#location 23
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCSVFile() throws Exception {
String line = readTestData();
assertNotNull("file must contain config line", line);
final String[] split = line.split(" ");
assertTrue(testName+" require 1 param", split.length >= 1);
// first line starts with csv data file name
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
boolean checkComments = false;
for(int i=1; i < split.length; i++) {
final String option = split[i];
final String[] option_parts = option.split("=",2);
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
format = format.withCommentStart(option_parts[1].charAt(0));
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
checkComments = true;
} else {
fail(testName+" unexpected option: "+option);
}
}
line = readTestData(); // get string version of format
assertEquals(testName+" Expected format ", line, format.toString());
// Now parse the file and compare against the expected results
// We use a buffered reader internally so no need to create one here.
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), format);
for(final CSVRecord record : parser) {
String parsed = record.toString();
if (checkComments) {
final String comment = record.getComment().replace("\n", "\\n");
if (comment != null) {
parsed += "#" + comment;
}
}
final int count = record.size();
assertEquals(testName, readTestData(), count+":"+parsed);
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testIgnoreEmptyLines() throws IOException {
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size());
}
#location 7
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testIgnoreEmptyLines() throws IOException {
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testRoundtrip() throws Exception {
final StringWriter out = new StringWriter();
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
for (final CSVRecord record : CSVParser.parseString(input, CSVFormat.DEFAULT)) {
printer.printRecord(record);
}
assertEquals(input, out.toString());
printer.close();
}
#location 9
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testRoundtrip() throws Exception {
final StringWriter out = new StringWriter();
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) {
printer.printRecord(record);
}
assertEquals(input, out.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelFormat2() throws Exception {
final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
final String[][] res = {
{"foo", "baar"},
{""},
{"hello", ""},
{""},
{"world", ""}
};
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
#location 12
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelFormat2() throws Exception {
final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
final String[][] res = {
{"foo", "baar"},
{""},
{"hello", ""},
{""},
{"world", ""}
};
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testConstructors() {
br = new ExtendedBufferedReader(new StringReader(""));
br = new ExtendedBufferedReader(new StringReader(""), 10);
}
#location 3
#vulnerability type RESOURCE_LEAK | #fixed code
public void testConstructors() {
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
br = new ExtendedBufferedReader(new StringReader(""), 10);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = new CSVParser(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = new CSVParser(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
#location 20
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parseString(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parseString(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
String[] tmp = null;
for (int i = 0; i < res.length; i++) {
tmp = parser.getLine();
assertTrue(Arrays.equals(res[i], tmp));
}
tmp = parser.getLine();
assertTrue(tmp == null);
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
for (String[] re : res) {
assertTrue(Arrays.equals(re, parser.getLine()));
}
assertTrue(parser.getLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testGetRecords() throws IOException {
final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final List<CSVRecord> records = parser.getRecords();
assertEquals(RESULT.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < RESULT.length; i++) {
assertArrayEquals(RESULT[i], records.get(i).values());
}
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testGetRecords() throws IOException {
final CSVParser parser = CSVParser.parse(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final List<CSVRecord> records = parser.getRecords();
assertEquals(RESULT.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < RESULT.length; i++) {
assertArrayEquals(RESULT[i], records.get(i).values());
}
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCSVUrl() throws Exception {
String line = readTestData();
assertNotNull("file must contain config line", line);
final String[] split = line.split(" ");
assertTrue(testName + " require 1 param", split.length >= 1);
// first line starts with csv data file name
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
boolean checkComments = false;
for (int i = 1; i < split.length; i++) {
final String option = split[i];
final String[] option_parts = option.split("=", 2);
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
format = format.withCommentStart(option_parts[1].charAt(0));
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
checkComments = true;
} else {
fail(testName + " unexpected option: " + option);
}
}
line = readTestData(); // get string version of format
assertEquals(testName + " Expected format ", line, format.toString());
// Now parse the file and compare against the expected results
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
for (final CSVRecord record : parser) {
String parsed = record.toString();
if (checkComments) {
final String comment = record.getComment().replace("\n", "\\n");
if (comment != null) {
parsed += "#" + comment;
}
}
final int count = record.size();
assertEquals(testName, readTestData(), count + ":" + parsed);
}
}
#location 23
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCSVUrl() throws Exception {
String line = readTestData();
assertNotNull("file must contain config line", line);
final String[] split = line.split(" ");
assertTrue(testName + " require 1 param", split.length >= 1);
// first line starts with csv data file name
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
boolean checkComments = false;
for (int i = 1; i < split.length; i++) {
final String option = split[i];
final String[] option_parts = option.split("=", 2);
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
format = format.withCommentStart(option_parts[1].charAt(0));
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
checkComments = true;
} else {
fail(testName + " unexpected option: " + option);
}
}
line = readTestData(); // get string version of format
assertEquals(testName + " Expected format ", line, format.toString());
// Now parse the file and compare against the expected results
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
for (final CSVRecord record : parser) {
String parsed = record.toString();
if (checkComments) {
final String comment = record.getComment().replace("\n", "\\n");
if (comment != null) {
parsed += "#" + comment;
}
}
final int count = record.size();
assertEquals(testName, readTestData(), count + ":" + parsed);
}
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" /\" / string\" "},
{"9", " \n "},
};
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parseString(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Records do not match expected result", res, records);
}
#location 38
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" /\" / string\" "},
{"9", " \n "},
};
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Records do not match expected result", res, records);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""}, // CSV format ignores empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 19
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""}, // CSV format ignores empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true, "\r\n");
CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
#location 38
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test // TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
public void testMultipleIterators() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT);
final Iterator<CSVRecord> itr1 = parser.iterator();
final Iterator<CSVRecord> itr2 = parser.iterator();
final CSVRecord first = itr1.next();
assertEquals("a", first.get(0));
assertEquals("b", first.get(1));
assertEquals("c", first.get(2));
final CSVRecord second = itr2.next();
assertEquals("d", second.get(0));
assertEquals("e", second.get(1));
assertEquals("f", second.get(2));
}
#location 13
#vulnerability type RESOURCE_LEAK | #fixed code
@Test // TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
public void testMultipleIterators() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT);
final Iterator<CSVRecord> itr1 = parser.iterator();
final Iterator<CSVRecord> itr2 = parser.iterator();
final CSVRecord first = itr1.next();
assertEquals("a", first.get(0));
assertEquals("b", first.get(1));
assertEquals("c", first.get(2));
final CSVRecord second = itr2.next();
assertEquals("d", second.get(0));
assertEquals("e", second.get(1));
assertEquals("f", second.get(2));
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();
final int nLines = r.nextInt(4) + 1;
final int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
final String[][] lines = new String[nLines][];
for (int i = 0; i < nLines; i++) {
final String[] line = new String[nCol];
lines[i] = line;
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, format);
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.printRecord((Object[])lines[i]);
}
printer.flush();
printer.close();
final String result = sw.toString();
// System.out.println("### :" + printable(result));
final CSVParser parser = CSVParser.parseString(result, format);
final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult);
}
#location 30
#vulnerability type RESOURCE_LEAK | #fixed code
public void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();
final int nLines = r.nextInt(4) + 1;
final int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
final String[][] lines = new String[nLines][];
for (int i = 0; i < nLines; i++) {
final String[] line = new String[nCol];
lines[i] = line;
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, format);
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.printRecord((Object[])lines[i]);
}
printer.flush();
printer.close();
final String result = sw.toString();
// System.out.println("### :" + printable(result));
final CSVParser parser = CSVParser.parse(result, format);
final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 16
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelPrintAllIterableOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelPrintAllIterableOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
#location 27
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
CSVParser parser = new CSVParser(new StringReader(code));
List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrinter7() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\\c");
assertEquals("a,b\\c" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrinter7() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\\c");
assertEquals("a,b\\c" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = CSVParser.parseString("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testMutators() {
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator().charValue());
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape().charValue());
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
}
#location 6
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testMutators() {
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getQuoteChar().charValue());
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape().charValue());
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testLineFeedEndings() throws IOException {
String code = "foo\nbaar,\nhello,world\n,kanu";
CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords();
assertEquals(4, data.length);
}
#location 6
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testLineFeedEndings() throws IOException {
String code = "foo\nbaar,\nhello,world\n,kanu";
CSVParser parser = new CSVParser(new StringReader(code));
List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyLineBehaviourCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""} // CSV format ignores empty lines
};
for (final String code : codes) {
final CSVParser parser = new CSVParser(new StringReader(code));
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 14
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyLineBehaviourCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""} // CSV format ignores empty lines
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parseString(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parseString(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
#location 20
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelPrinter2() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a,b", "b");
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelPrinter2() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a,b", "b");
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLine() throws Exception {
br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
}
#location 6
#vulnerability type NULL_DEREFERENCE | #fixed code
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withQuoteChar('\'').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
#location 38
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
final CSVFormat format = new CSVFormat(',').withQuoteChar('\'').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void test() throws UnsupportedEncodingException, IOException {
InputStream pointsOfReference = getClass().getResourceAsStream("/CSV-198/optd_por_public.csv");
Assert.assertNotNull(pointsOfReference);
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, "UTF-8"));
for (CSVRecord record : parser) {
String locationType = record.get("location_type");
Assert.assertNotNull(locationType);
}
}
#location 9
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void test() throws UnsupportedEncodingException, IOException {
InputStream pointsOfReference = getClass().getResourceAsStream("/CSV-198/optd_por_public.csv");
Assert.assertNotNull(pointsOfReference);
try (@SuppressWarnings("resource")
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, "UTF-8"))) {
for (CSVRecord record : parser) {
String locationType = record.get("location_type");
Assert.assertNotNull(locationType);
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrinter6() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\r\nc");
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrinter6() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\r\nc");
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadUntil() throws Exception {
br = getEBR("");
assertTrue(br.readUntil(';').equals(""));
br = getEBR("ABCDEF;GHL;;MN");
assertTrue(br.readUntil(';').equals("ABCDEF"));
assertTrue(br.readUntil(';').length() == 0);
br.skip(1);
assertTrue(br.readUntil(';').equals("GHL"));
br.skip(1);
assertTrue(br.readUntil(';').equals(""));
br.skip(1);
assertTrue(br.readUntil(',').equals("MN"));
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadUntil() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readUntil(';').equals(""));
br = getEBR("ABCDEF;GHL;;MN");
assertTrue(br.readUntil(';').equals("ABCDEF"));
assertTrue(br.readUntil(';').length() == 0);
br.skip(1);
assertTrue(br.readUntil(';').equals("GHL"));
br.skip(1);
assertTrue(br.readUntil(';').equals(""));
br.skip(1);
assertTrue(br.readUntil(',').equals("MN"));
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" /\" / string\" "},
{"9", " \n "},
};
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Records do not match expected result", res, records);
}
#location 38
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
final String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" /\" / string\" "},
{"9", " \n "},
};
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Records do not match expected result", res, records);
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
#location 32
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser.close();
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLookahead2() throws Exception {
char[] ref = new char[5];
char[] res = new char[5];
ExtendedBufferedReader br = getEBR("");
assertEquals(0, br.read(res, 0, 0));
assertTrue(Arrays.equals(res, ref));
br = getEBR("abcdefg");
ref[0] = 'a';
ref[1] = 'b';
ref[2] = 'c';
assertEquals(3, br.read(res, 0, 3));
assertTrue(Arrays.equals(res, ref));
assertEquals('c', br.readAgain());
assertEquals('d', br.lookAhead());
ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1));
assertTrue(Arrays.equals(res, ref));
assertEquals('d', br.readAgain());
}
#location 6
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadLookahead2() throws Exception {
char[] ref = new char[5];
char[] res = new char[5];
ExtendedBufferedReader br = getBufferedReader("abcdefg");
ref[0] = 'a';
ref[1] = 'b';
ref[2] = 'c';
assertEquals(3, br.read(res, 0, 3));
assertTrue(Arrays.equals(res, ref));
assertEquals('c', br.readAgain());
assertEquals('d', br.lookAhead());
ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1));
assertTrue(Arrays.equals(res, ref));
assertEquals('d', br.readAgain());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true, "\r\n");
CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0);
if (!CSVPrinterTest.equals(res, tmp)) {
assertTrue(false);
}
}
#location 24
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
assertTrue(CSVPrinterTest.equals(res, records));
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testEndOfFileBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
String code;
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
code = codes[codeIndex];
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
String[][] tmp = parser.getAllValues();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
}
#location 21
#vulnerability type RESOURCE_LEAK | #fixed code
public void testEndOfFileBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
String[][] tmp = parser.getAllValues();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testIgnoreEmptyLines() throws IOException {
String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
CSVParser parser = new CSVParser(new StringReader(code));
String[][] data = parser.getRecords();
assertEquals(3, data.length);
}
#location 8
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testIgnoreEmptyLines() throws IOException {
String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
CSVParser parser = new CSVParser(new StringReader(code));
List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelFormat1() throws IOException {
String code =
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
String[][] res = {
{"value1", "value2", "value3", "value4"},
{"a", "b", "c", "d"},
{" x", "", "", ""},
{""},
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
#location 15
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testExcelFormat1() throws IOException {
String code =
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
String[][] res = {
{"value1", "value2", "value3", "value4"},
{"a", "b", "c", "d"},
{" x", "", "", ""},
{""},
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testIterator() {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext());
iterator.remove();
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("NoSuchElementException expected");
} catch (NoSuchElementException e) {
// expected
}
}
#location 21
#vulnerability type RESOURCE_LEAK | #fixed code
public void testIterator() {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext());
try {
iterator.remove();
fail("expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("NoSuchElementException expected");
} catch (NoSuchElementException e) {
// expected
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testIgnoreEmptyLines() throws IOException {
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
final CSVParser parser = new CSVParser(new StringReader(code));
final List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size());
}
#location 7
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testIgnoreEmptyLines() throws IOException {
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
//String code = "world\r\n\n";
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
final CSVParser parser = CSVParser.parseString(code);
final List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testQuoteAll() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.ALL).build());
printer.printRecord("a", "b\nc", "d");
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testQuoteAll() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.ALL).build());
printer.printRecord("a", "b\nc", "d");
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
private void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();
final int nLines = r.nextInt(4) + 1;
final int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
final String[][] lines = new String[nLines][];
for (int i = 0; i < nLines; i++) {
final String[] line = new String[nCol];
lines[i] = line;
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, format);
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.printRecord((Object[])lines[i]);
}
printer.flush();
printer.close();
final String result = sw.toString();
// System.out.println("### :" + printable(result));
final CSVParser parser = CSVParser.parse(result, format);
final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult);
}
#location 30
#vulnerability type RESOURCE_LEAK | #fixed code
private void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();
final int nLines = r.nextInt(4) + 1;
final int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
final String[][] lines = new String[nLines][];
for (int i = 0; i < nLines; i++) {
final String[] line = new String[nCol];
lines[i] = line;
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, format);
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.printRecord((Object[])lines[i]);
}
printer.flush();
printer.close();
final String result = sw.toString();
// System.out.println("### :" + printable(result));
final CSVParser parser = CSVParser.parse(result, format);
final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult);
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
final String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("", res, records);
}
#location 24
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
final String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
final CSVFormat format = new CSVFormat(',').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("", res, records);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testIterator() {
String code = "a,b,c\n1,2,3\nx,y,z";
Iterator<String[]> iterator = new CSVParser(new StringReader(code)).iterator();
assertTrue(iterator.hasNext());
iterator.remove();
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("NoSuchElementException expected");
} catch (NoSuchElementException e) {
// expected
}
}
#location 20
#vulnerability type RESOURCE_LEAK | #fixed code
public void testIterator() {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext());
iterator.remove();
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, iterator.next()));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, iterator.next()));
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, iterator.next()));
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("NoSuchElementException expected");
} catch (NoSuchElementException e) {
// expected
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testLineFeedEndings() throws IOException {
final String code = "foo\nbaar,\nhello,world\n,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testLineFeedEndings() throws IOException {
final String code = "foo\nbaar,\nhello,world\n,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testSkip0() throws Exception {
br = getEBR("");
assertEquals(0, br.skip(0));
assertEquals(0, br.skip(1));
br = getEBR("");
assertEquals(0, br.skip(1));
br = getEBR("abcdefg");
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(1, br.skip(1));
assertEquals('b', br.lookAhead());
assertEquals('b', br.read());
assertEquals(3, br.skip(3));
assertEquals('f', br.lookAhead());
assertEquals(2, br.skip(5));
assertTrue(br.readLine() == null);
br = getEBR("12345");
assertEquals(5, br.skip(5));
assertTrue (br.lookAhead() == ExtendedBufferedReader.END_OF_STREAM);
}
#location 3
#vulnerability type RESOURCE_LEAK | #fixed code
public void testSkip0() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertEquals(0, br.skip(0));
assertEquals(0, br.skip(1));
br = getEBR("");
assertEquals(0, br.skip(1));
br = getEBR("abcdefg");
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(0, br.skip(0));
assertEquals('a', br.lookAhead());
assertEquals(1, br.skip(1));
assertEquals('b', br.lookAhead());
assertEquals('b', br.read());
assertEquals(3, br.skip(3));
assertEquals('f', br.lookAhead());
assertEquals(2, br.skip(5));
assertTrue(br.readLine() == null);
br = getEBR("12345");
assertEquals(5, br.skip(5));
assertTrue (br.lookAhead() == ExtendedBufferedReader.END_OF_STREAM);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
}
#location 22
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 16
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
parser.close();
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = CSVParser.parseString("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 21
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parseString(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parseString(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
#location 32
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test // TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
public void testMultipleIterators() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT);
final Iterator<CSVRecord> itr1 = parser.iterator();
final Iterator<CSVRecord> itr2 = parser.iterator();
final CSVRecord first = itr1.next();
assertEquals("a", first.get(0));
assertEquals("b", first.get(1));
assertEquals("c", first.get(2));
final CSVRecord second = itr2.next();
assertEquals("d", second.get(0));
assertEquals("e", second.get(1));
assertEquals("f", second.get(2));
}
#location 17
#vulnerability type RESOURCE_LEAK | #fixed code
@Test // TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
public void testMultipleIterators() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT);
final Iterator<CSVRecord> itr1 = parser.iterator();
final Iterator<CSVRecord> itr2 = parser.iterator();
final CSVRecord first = itr1.next();
assertEquals("a", first.get(0));
assertEquals("b", first.get(1));
assertEquals("c", first.get(2));
final CSVRecord second = itr2.next();
assertEquals("d", second.get(0));
assertEquals("e", second.get(1));
assertEquals("f", second.get(2));
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testMultiLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment\non multiple lines");
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testMultiLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment\non multiple lines");
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertTrue(br.readLine() == null);
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertTrue(br.readLine() == null);
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertTrue(br.readLine() == null);
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
final String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
final String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
#location 26
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
final String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
final String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testNoHeaderMap() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
Assert.assertNull(parser.getHeaderMap());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testNoHeaderMap() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
Assert.assertNull(parser.getHeaderMap());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
}
#location 3
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertTrue(br.readLine() == null);
br = getBufferedReader("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDisabledComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment");
assertEquals("", sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDisabledComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment");
assertEquals("", sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
final String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
final String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
final CSVParser parser = new CSVParser(new StringReader(code));
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
#location 26
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
@Ignore
public void testBackslashEscapingOld() throws IOException {
final String code =
"one,two,three\n"
+ "on\\\"e,two\n"
+ "on\"e,two\n"
+ "one,\"tw\\\"o\"\n"
+ "one,\"t\\,wo\"\n"
+ "one,two,\"th,ree\"\n"
+ "\"a\\\\\"\n"
+ "a\\,b\n"
+ "\"a\\\\,b\"";
final String[][] res = {
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
final CSVParser parser = CSVParser.parseString(code);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrinter3() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a, b", "b ");
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrinter3() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a, b", "b ");
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testUnicodeEscape() throws IOException {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
String[] data = parser.getLine();
assertEquals(2, data.length);
assertEquals("abc", data[0]);
assertEquals("public", data[1]);
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
public void testUnicodeEscape() throws IOException {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
String[] data = parser.iterator().next();
assertEquals(2, data.length);
assertEquals("abc", data[0]);
assertEquals("public", data[1]);
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCarriageReturnEndings() throws IOException {
final String code = "foo\rbaar,\rhello,world\r,kanu";
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCarriageReturnEndings() throws IOException {
final String code = "foo\rbaar,\rhello,world\r,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], tmp[i]));
}
}
}
#location 17
#vulnerability type NULL_DEREFERENCE | #fixed code
@Test
public void testEmptyLineBehaviourExcel() throws Exception {
String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br.close();
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
br.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testLineFeedEndings() throws IOException {
final String code = "foo\nbaar,\nhello,world\n,kanu";
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testLineFeedEndings() throws IOException {
final String code = "foo\nbaar,\nhello,world\n,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyFile() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
assertNull(parser.nextRecord());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyFile() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
assertNull(parser.nextRecord());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
#location 37
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
String code =
"one,two,three\n" // 0
+ "'',''\n" // 1) empty encapsulators
+ "/',/'\n" // 2) single encapsulators
+ "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ "/,,/,\n" // 5) separator escaped
+ "//,//\n" // 6) escape escaped
+ "'//','//'\n" // 7) escape escaped in encapsulation
+ " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ "9, /\n \n" // escaped newline
+ "";
String[][] res = {
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testGetRecords() throws IOException {
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final List<CSVRecord> records = parser.getRecords();
assertEquals(RESULT.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < RESULT.length; i++) {
assertArrayEquals(RESULT[i], records.get(i).values());
}
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testGetRecords() throws IOException {
final CSVParser parser = CSVParser.parseString(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final List<CSVRecord> records = parser.getRecords();
assertEquals(RESULT.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < RESULT.length; i++) {
assertArrayEquals(RESULT[i], records.get(i).values());
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testSingleLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment");
assertEquals("# This is a comment" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testSingleLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment");
assertEquals("# This is a comment" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 21
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEndOfFileBehaviourExcel() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""},
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
parser.close();
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine());
br.close();
br = getBufferedReader("\n");
assertEquals("",br.readLine());
assertNull(br.readLine());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals("hello",br.readLine());
assertEquals(3, br.getCurrentLineNumber());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertEquals("oo",br.readLine());
assertEquals(1, br.getCurrentLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals("",br.readLine());
assertEquals(2, br.getCurrentLineNumber());
assertEquals('h', br.lookAhead());
assertEquals("hello",br.readLine());
assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead());
assertEquals("baar",br.readLine());
assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine());
assertNull(br.readLine());
br.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""}, // CSV format ignores empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = new CSVParser(new StringReader(code));
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 19
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\nworld,\r\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\r\n",
"hello,\r\n\r\nworld,\"\"",
"hello,\r\n\r\nworld,\n",
"hello,\r\n\r\nworld,",
"hello,\r\n\r\nworld,\"\"\n",
"hello,\r\n\r\nworld,\"\""
};
final String[][] res = {
{"hello", ""}, // CSV format ignores empty lines
{"world", ""}
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parseString(code);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelPrintAllArrayOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelPrintAllArrayOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
final StringWriter sw = new StringWriter();
Class.forName("org.h2.Driver");
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
try {
final Statement stmt = connection.createStatement();
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
stmt.execute("insert into TEST values(1, 'r1')");
stmt.execute("insert into TEST values(2, 'r2')");
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
} finally {
connection.close();
}
}
#location 12
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
final StringWriter sw = new StringWriter();
Class.forName("org.h2.Driver");
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
try {
final Statement stmt = connection.createStatement();
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
stmt.execute("insert into TEST values(1, 'r1')");
stmt.execute("insert into TEST values(2, 'r2')");
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
printer.close();
} finally {
connection.close();
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testQuoteNonNumeric() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.NON_NUMERIC).build());
printer.printRecord("a", "b\nc", Integer.valueOf(1));
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testQuoteNonNumeric() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.NON_NUMERIC).build());
printer.printRecord("a", "b\nc", Integer.valueOf(1));
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testReadLookahead1() throws Exception {
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
assertEquals('1', br.lookAhead());
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertEquals('1', br.read());
assertEquals('1', br.readAgain());
assertEquals(0, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals(0, br.getLineNumber());
assertEquals('1', br.readAgain());
assertEquals('\n', br.read());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.read());
assertEquals('2', br.readAgain());
assertEquals('\r', br.lookAhead());
assertEquals('2', br.readAgain());
assertEquals('\r', br.read());
assertEquals('\r', br.readAgain());
assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain());
assertEquals('3', br.read());
assertEquals('3', br.readAgain());
assertEquals('\n', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('3', br.readAgain());
assertEquals('\n', br.read());
assertEquals(2, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(2, br.getLineNumber());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('\n', br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
}
#location 3
#vulnerability type RESOURCE_LEAK | #fixed code
public void testReadLookahead1() throws Exception {
ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
assertEquals('1', br.lookAhead());
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertEquals('1', br.read());
assertEquals('1', br.readAgain());
assertEquals(0, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertEquals(0, br.getLineNumber());
assertEquals('1', br.readAgain());
assertEquals('\n', br.read());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.read());
assertEquals('2', br.readAgain());
assertEquals('\r', br.lookAhead());
assertEquals('2', br.readAgain());
assertEquals('\r', br.read());
assertEquals('\r', br.readAgain());
assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain());
assertEquals('3', br.read());
assertEquals('3', br.readAgain());
assertEquals('\n', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('3', br.readAgain());
assertEquals('\n', br.read());
assertEquals(2, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(2, br.getLineNumber());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('\n', br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyLineBehaviourCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""} // CSV format ignores empty lines
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
}
#location 14
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyLineBehaviourCSV() throws Exception {
final String[] codes = {
"hello,\r\n\r\n\r\n",
"hello,\n\n\n",
"hello,\"\"\r\n\r\n\r\n",
"hello,\"\"\n\n\n"
};
final String[][] res = {
{"hello", ""} // CSV format ignores empty lines
};
for (final String code : codes) {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
parser.close();
}
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
public void testSkipUntil() throws Exception {
br = getEBR("");
assertEquals(0, br.skipUntil(';'));
br = getEBR("ABCDEF,GHL,,MN");
assertEquals(6, br.skipUntil(','));
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(3, br.skipUntil(','));
br.skip(1);
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(2, br.skipUntil(','));
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
public void testSkipUntil() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertEquals(0, br.skipUntil(';'));
br = getEBR("ABCDEF,GHL,,MN");
assertEquals(6, br.skipUntil(','));
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(3, br.skipUntil(','));
br.skip(1);
assertEquals(0, br.skipUntil(','));
br.skip(1);
assertEquals(2, br.skipUntil(','));
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCSV57() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
final List<CSVRecord> list = parser.getRecords();
assertNotNull(list);
assertEquals(0, list.size());
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testExcelFormat1() throws IOException {
final String code =
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
final String[][] res = {
{"value1", "value2", "value3", "value4"},
{"a", "b", "c", "d"},
{" x", "", "", ""},
{""},
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
}
#location 14
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testExcelFormat1() throws IOException {
final String code =
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
final String[][] res = {
{"value1", "value2", "value3", "value4"},
{"a", "b", "c", "d"},
{" x", "", "", ""},
{""},
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
final List<CSVRecord> records = parser.getRecords();
assertEquals(res.length, records.size());
assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values());
}
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testEmptyFile() throws Exception {
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
assertNull(parser.nextRecord());
}
#location 4
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testEmptyFile() throws Exception {
final CSVParser parser = CSVParser.parseString("", CSVFormat.DEFAULT);
assertNull(parser.nextRecord());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testPrintNullValues() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", null, "b");
assertEquals("a,,b" + recordSeparator, sw.toString());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testPrintNullValues() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", null, "b");
assertEquals("a,,b" + recordSeparator, sw.toString());
printer.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
final String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
final CSVFormat format = CSVFormat.newFormat(',')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("", res, records);
}
#location 24
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testBackslashEscaping2() throws IOException {
// To avoid confusion over the need for escaping chars in java code,
// We will test with a forward slash as the escape char, and a single
// quote as the encapsulator.
final String code = ""
+ " , , \n" // 1)
+ " \t , , \n" // 2)
+ " // , /, , /,\n" // 3)
+ "";
final String[][] res = {
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, // 3
};
final CSVFormat format = CSVFormat.newFormat(',')
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("", res, records);
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testCarriageReturnLineFeedEndings() throws IOException {
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
}
#location 5
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testCarriageReturnLineFeedEndings() throws IOException {
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size());
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
#location 20
#vulnerability type RESOURCE_LEAK | #fixed code
@Test
public void testDefaultFormat() throws IOException {
final String code = ""
+ "a,b#\n" // 1)
+ "\"\n\",\" \",#\n" // 2)
+ "#,\"\"\n" // 3)
+ "# Final comment\n"// 4)
;
final String[][] res = {
{"a", "b#"},
{"\n", " ", "#"},
{"#", ""},
{"# Final comment"}
};
CSVFormat format = CSVFormat.DEFAULT;
assertFalse(format.isCommentingEnabled());
CSVParser parser = CSVParser.parse(code, format);
List<CSVRecord> records = parser.getRecords();
assertTrue(records.size() > 0);
Utils.compare("Failed to parse without comments", res, records);
final String[][] res_comments = {
{"a", "b#"},
{"\n", " ", "#"},
};
format = CSVFormat.DEFAULT.withCommentStart('#');
parser.close();
parser = CSVParser.parse(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
parser.close();
} | Below is the vulnerable code, please generate the patch based on the following information. |
#vulnerable code
private void validateRecordNumbers(final String lineSeparator) throws IOException {
final CSVParser parser = CSVParser.parseString("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
CSVRecord record;
assertEquals(0, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(1, record.getRecordNumber());
assertEquals(1, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(2, record.getRecordNumber());
assertEquals(2, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(3, record.getRecordNumber());
assertEquals(3, parser.getRecordNumber());
assertNull(record = parser.nextRecord());
assertEquals(3, parser.getRecordNumber());
}
#location 13
#vulnerability type RESOURCE_LEAK | #fixed code
private void validateRecordNumbers(final String lineSeparator) throws IOException {
final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
CSVRecord record;
assertEquals(0, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(1, record.getRecordNumber());
assertEquals(1, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(2, record.getRecordNumber());
assertEquals(2, parser.getRecordNumber());
assertNotNull(record = parser.nextRecord());
assertEquals(3, record.getRecordNumber());
assertEquals(3, parser.getRecordNumber());
assertNull(record = parser.nextRecord());
assertEquals(3, parser.getRecordNumber());
} | Below is the vulnerable code, please generate the patch based on the following information. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.