Skip to content

Commit 07d2728

Browse files
Issue #265: Prevent JVM crash when wrong datapath given
1 parent c8386ea commit 07d2728

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/main/java/net/sourceforge/tess4j/ITesseract.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ default String doOCR(BufferedImage bi, String filename, List<Rectangle> rects) t
136136
* Sets tessdata path.
137137
*
138138
* @param datapath the tessdata path to set
139+
* @throws IllegalArgumentException if the given datapath is not an existing directory
139140
*/
140141
void setDatapath(String datapath);
141142

src/main/java/net/sourceforge/tess4j/Tesseract.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public Tesseract() {
8787
datapath = "./";
8888
}
8989
}
90+
File datapathFile = new File(datapath);
91+
if (!datapathFile.exists() || !datapathFile.isDirectory()) {
92+
throw new IllegalArgumentException("Given datapath " + datapath + " is not an existing directory.");
93+
}
9094
}
9195

9296
/**
@@ -114,6 +118,10 @@ protected TessBaseAPI getHandle() {
114118
*/
115119
@Override
116120
public void setDatapath(String datapath) {
121+
File datapathFile = new File(datapath);
122+
if (!datapathFile.exists() || !datapathFile.isDirectory()) {
123+
throw new IllegalArgumentException("Given datapath " + datapath + " is not an existing directory.");
124+
}
117125
this.datapath = datapath;
118126
}
119127

src/test/java/net/sourceforge/tess4j/TesseractTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,11 @@ public void testGetOSD() {
407407
assertEquals("Latin", result.getScriptName());
408408
assertTrue(result.getScriptConf() > 0);
409409
}
410+
411+
@Test
412+
public void testSetDatapath() {
413+
logger.info("testSetDatapath");
414+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> instance.setDatapath("non-existent-datapath"));
415+
assertEquals("Given datapath non-existent-datapath is not an existing directory.", e.getMessage());
416+
}
410417
}

0 commit comments

Comments
 (0)