tesseract icon indicating copy to clipboard operation
tesseract copied to clipboard

Segmentation fault when initializing with null language

Open ccouzens opened this issue 1 year ago • 17 comments

Basic Information

tesseract 5.2.0 leptonica-1.82.0 libgif 5.2.1 : libjpeg 6b (libjpeg-turbo 2.1.3) : libpng 1.6.37 : libtiff 4.4.0 : zlib 1.2.12 : libwebp 1.3.0 Found AVX2 Found AVX Found FMA Found SSE4.1

Operating System

No response

Other Operating System

Fedora Linux 37

But this was originally reported to me from a user on a Mac M1 (presumably macOS 13 Ventura).

uname -a

Linux fedora-desktop 6.1.14-200.fc37.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Feb 26 00:13:26 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Compiler

gcc version 12.2.1 20221121 (Red Hat 12.2.1-4) (GCC)

Virtualization / Containers

No response

CPU

13th Gen Intel® Core™ i7-13700K

Current Behavior

When using TessBaseAPIInit3(cube, NULL, NULL) the language isn't set to a sensible default, thus later causing a segmentation fault when TessBaseAPIRecognize is called.

Expected Behavior

Given that the documentation says:

The language is (usually) an ISO 639-3 string or nullptr will default to eng.

I would expect a NULL to work the same way as "eng" (not segmentation fault at the Recognize step).

Suggested Fix

Null pointer defaults to "eng".

Other Information

Test case program

#include <tesseract/capi.h>
#include <leptonica/allheaders.h>

int main(int argc, char *argv[]) {
    TessBaseAPI *cube = TessBaseAPICreate();
    TessBaseAPIInit3(cube, NULL, NULL); // change this 2nd `NULL` to "eng" for success

    PIX *image = pixRead("img.png");
    TessBaseAPISetImage2(cube, image);
    TessBaseAPIRecognize(cube, NULL);
    char *text = TessBaseAPIGetUTF8Text(cube);
    printf("%s\n", text);
    TessDeleteText(text);
    pixFreeData(image);
    TessBaseAPIDelete(cube);
}

run using gcc $(pkg-config --cflags --libs tesseract) $(pkg-config --cflags --libs lept) test.c && ./a.out.

This was originally reported against a Rust wrapper: https://github.com/antimatter15/tesseract-rs/issues/34

ccouzens avatar Mar 05 '23 17:03 ccouzens

The API was changed by my commit f5d22d0bc ("Don't set a default language in TessBaseAPI::Init"). The reason for that commit was that Tesseract required (and loaded) eng.traineddata even for tasks which did not require a model file.

So the documentation should be updated, and of course the code should not crash.

stweil avatar Mar 05 '23 17:03 stweil

#include <leptonica/allheaders.h>

The right form is #include <allheaders.h>.

stweil avatar Mar 05 '23 21:03 stweil

The right form is #include <allheaders.h>.

Why?

zdenop avatar Mar 06 '23 06:03 zdenop

Including leptonica/allheaders.h simply does not work in many cases, for example on MacOS with Homebrew.

Leptonica tells the compiler to look for header files in the leptonica directory which provides allheaders.h, but not leptonica/allheaders.h:

% pkg-config --cflags lept
-I/opt/homebrew/Cellar/leptonica/1.82.0_1/include/leptonica

Including leptonica/allheaders.h works on typical Linux installations , because the compiler searchs /usr/include and /usr/local/include, too. It won't work on Linux if Leptonica was installed in a non standard location like $HOME.

stweil avatar Mar 06 '23 06:03 stweil

The PR has more details: https://github.com/tesseract-ocr/tesseract/pull/3610

amitdo avatar Mar 06 '23 08:03 amitdo

@stweil : Who leptonica is build on MacOS with Homebrew? With autotool or cmake? On Windows cmake build with custom instalation I got this:

>SET PKG_CONFIG_PATH=f:\win64\lib\pkgconfig
>F:\vcpkg\downloads\tools\msys2\9a1ec3f33446b195\mingw32\bin\pkg-config.exe --cflags lept
-If:/win64/include -If:/win64/include/leptonica

IMO this is right approach, so developer could use both variants. Personally I prefer leptonica/allheaders.h, because allheaders.h is too general and unclear to which library it belongs.

zdenop avatar Mar 22 '23 06:03 zdenop

Homebrew uses autotools for all platforms in its tesseract formula.

I am surprised that pkgconfig gives two include paths for the Leptonica headers. That's strange and unusual.

stweil avatar Mar 22 '23 06:03 stweil

@DanBloomberg, there seem to exist two different conventions how people include the header file for Leptonica:

  1. #include <allheaders.h> /* works with compiler flags from pkg-config generated by autotools of cmake */
  2. #include <leptonica/allheaders.h> /* works with compiler flags from pkg-config generated by cmake */

Which of the two variants would you prefer? We currently have the problem that it can depend on the build type (autotools or cmake) whether both variants work or only the first one. We could enforce that only one of both variants is supported, or we could support both variants.

stweil avatar Mar 23 '23 14:03 stweil

What do you means by pkg-config generated by autotools of cmake?

amitdo avatar Mar 23 '23 14:03 amitdo

CMake and Autotools should behave similarly, otherwise you make supporting the software more difficult.

amitdo avatar Mar 23 '23 14:03 amitdo

Due to the differences between the builds, for Tesseract at least I would prefer that we support just one build system for Linux, macOS,, and the BSDs.

amitdo avatar Mar 23 '23 15:03 amitdo

#4026 is one more example for a different behavior of the Autotools build vs. the CMake build.

amitdo avatar Mar 23 '23 15:03 amitdo

I have always used variant 1. Both in the library and for the 300 or so programs in the prog/ directory.

Never considered variant 2, which wouldn't work with any of my code because I'm using specific local builds (not installed software) when developing and testing.

DanBloomberg avatar Mar 23 '23 16:03 DanBloomberg

CMake and Autotools should behave similarly, otherwise you make supporting the software more difficult.

Currently they use different templates for lept.pc which results in different compiler flags for the include path. See template for CMake and template for Autotools.

The lept.pc template for CMake should be fixed to fit the template for Autotools.

stweil avatar Mar 23 '23 16:03 stweil

What do you means by pkg-config generated by autotools of cmake?

I meant lept.pc which is used for pkg-config and which is generated by autotools or cmake.

stweil avatar Mar 23 '23 16:03 stweil

IMO leptonica has problem with naming conventions: "lept.pc" is one of examples (why not leptonica.pc?) allheaders.h, PIX, BOX etc are others problems when you try to read the code and you are not familiar. There are too general. That's why I prefer <tesseract/baseapi.h> and <leptonica/allheaders.h>.

If I understand it correctly: <leptonica/allheaders.h> is problem on MacOS with autotools build and pkgconfig only . On rest of platforms developers has freedom to choose if they will use <leptonica/allheaders.h> or just <allheaders.h>. IMO we should give this freedom also for autotools users...

zdenop avatar Mar 24 '23 07:03 zdenop

<leptonica/allheaders.h> is a problem on any host if the build used lept.pc.in and did not use a common installation prefix like /usr or /usr/local. And therefore we should not propagate it, but tell people to use <allheaders.h>.

stweil avatar Mar 24 '23 09:03 stweil