Skip to content

Commit 2718b81

Browse files
committed
fuzzer-api: Use environment variable TESSDATA_PREFIX if set
Clean also the code a little bit. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 7e9970b commit 2718b81

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

unittest/fuzzers/fuzzer-api.cpp

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include "baseapi.h"
22
#include "leptonica/allheaders.h"
33

4-
#include <libgen.h>
5-
#include <stddef.h>
6-
#include <stdint.h>
7-
#include <stdlib.h>
8-
#include <string.h>
9-
#include <string>
4+
#include <libgen.h> // for dirname
5+
#include <cstdio> // for printf
6+
#include <cstdlib> // for std::getenv, std::setenv
7+
#include <string> // for std::string
108

119
class BitReader {
1210
private:
@@ -36,21 +34,17 @@ class BitReader {
3634
}
3735
};
3836

39-
tesseract::TessBaseAPI* api = nullptr;
37+
static tesseract::TessBaseAPI* api = nullptr;
4038

41-
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
42-
(void)argc;
43-
(void)argv;
44-
45-
{
46-
char* binary_path = strdup(*argv[0]);
47-
const std::string filepath = dirname(binary_path);
48-
free(binary_path);
39+
extern "C" int LLVMFuzzerInitialize(int* /*pArgc*/, char*** pArgv) {
40+
if (std::getenv("TESSDATA_PREFIX") == nullptr) {
41+
std::string binary_path = *pArgv[0];
42+
const std::string filepath = dirname(&binary_path[0]);
4943

5044
const std::string tessdata_path = filepath + "/" + "tessdata";
5145
if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) {
5246
printf("Setenv failed\n");
53-
abort();
47+
std::abort();
5448
}
5549
}
5650

0 commit comments

Comments
 (0)