From f6ccf2745c5a919b7cb312faed5c697fdc10a542 Mon Sep 17 00:00:00 2001 From: tschechlovdev Date: Wed, 3 Aug 2016 13:38:41 +0200 Subject: [PATCH] Fix OutOfBoundsException when importing multiple entries in medline format (#1611) * add test for invalid format and null reader * change encoding to utf-8 --- .../fileformat/MedlinePlainImporter.java | 8 +++++- .../fileformat/MedlinePlainImporterTest.java | 26 +++++++++++++++++++ .../MedlinePlainImporterStringOutOfBounds.bib | 5 ++++ .../MedlinePlainImporterStringOutOfBounds.txt | 8 ++++++ .../MedlinePlainImporterTestInvalidFormat.xml | 5 ++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib create mode 100644 src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.txt create mode 100644 src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestInvalidFormat.xml diff --git a/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java index 2d8881d2fb2..b3a03ccda0a 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java @@ -127,6 +127,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } String entry = current.toString(); + if (!checkLineValidity(entry)) { + continue; + } String label = entry.substring(0, entry.indexOf('-')).trim(); String value = entry.substring(entry.indexOf('-') + 1).trim(); @@ -227,13 +230,16 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { // create one here b.setField(fields); bibitems.add(b); - } return new ParserResult(bibitems); } + private boolean checkLineValidity(String line) { + return (line.length() >= 5) && (line.charAt(4) == '-'); + } + private String addSourceType(String value, String type) { String val = value.toLowerCase(Locale.ENGLISH); String theType = type; diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java index 8c7aed816e9..42ccccf4866 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java @@ -19,11 +19,13 @@ import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; +import org.apache.commons.codec.Charsets; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class MedlinePlainImporterTest { @@ -197,6 +199,30 @@ public void testWithNbibFile() throws IOException, URISyntaxException { BibEntryAssert.assertEquals(MedlinePlainImporter.class, "NbibImporterTest.bib", entries); } + @Test + public void testWithMultipleEntries() throws IOException, URISyntaxException { + Path file = Paths + .get(MedlinePlainImporter.class.getResource("MedlinePlainImporterStringOutOfBounds.txt").toURI()); + List entries = importer.importDatabase(file, Charsets.UTF_8).getDatabase().getEntries(); + BibEntryAssert.assertEquals(MedlinePlainImporter.class, "MedlinePlainImporterStringOutOfBounds.bib", entries); + } + + @Test + public void testInvalidFormat() throws URISyntaxException, IOException { + Path file = Paths + .get(MedlinePlainImporter.class.getResource("MedlinePlainImporterTestInvalidFormat.xml").toURI()); + List entries = importer.importDatabase(file, Charsets.UTF_8).getDatabase().getEntries(); + assertEquals(Collections.emptyList(), entries); + } + + @Test(expected = NullPointerException.class) + public void testNullReader() throws IOException { + try (BufferedReader reader = null) { + importer.importDatabase(reader); + } + fail(); + } + @Test public void testAllArticleTypes() throws IOException { try (BufferedReader reader = readerForString("PMID-22664795" + "\n" + diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib new file mode 100644 index 00000000000..005a90f4969 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib @@ -0,0 +1,5 @@ +@misc{, + title = {This is a test title} +}, @misc{, + title = {This is also a test title} +} diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.txt b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.txt new file mode 100644 index 00000000000..3337997b30f --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterStringOutOfBounds.txt @@ -0,0 +1,8 @@ + +PMID- 27433151 +SO invalid line +TI - This is a test title + +PMID- 27394443 +SO- another invalid line +TI -This is also a test title diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestInvalidFormat.xml b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestInvalidFormat.xml new file mode 100644 index 00000000000..62c549fdaf5 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestInvalidFormat.xml @@ -0,0 +1,5 @@ +PMC some PMC + PMID + + +SO-something stands here \ No newline at end of file