galkahana / PDF-Writer

High performance library for creating, modiyfing and parsing PDF files in C++
http://www.pdfhummus.com
Apache License 2.0
901 stars 217 forks source link

This line : ` status = pdfWriter.StartPDF(completePathToEmptyPages, ePDFVersion13, logConfiguration)` causes : `Segmentation fault (core dumped)` #272

Open raphael10-collab opened 4 months ago

raphael10-collab commented 4 months ago

Code:

In class:

private:
                PDFWriter pdfWriter;
                EStatusCode status;

public:

    if (!std::filesystem::exists(PdfCreatedPath))
    {
        std::cout << PdfCreatedPath << " has to be created" << std::endl;
        std::filesystem::create_directory(PdfCreatedPath);
    }

    std::string PdfFileName = "output.pdf";
    std::string completePathToPdfFile = PdfCreatedPath + "/" + PdfFileName;
    // https://github.com/galkahana/PDF-Writer/blob/master/PDFWriterTesting/EmptyPagesPDF.cpp
    std::string completePathToEmptyPagesLog = PdfCreatedPath + "/" + "EmptyPagesLog.txt";
    std::string completePathToEmptyPages = PdfCreatedPath + "/" + "EmptyPages.pdf";
    LogConfiguration logConfiguration(true,true, completePathToEmptyPagesLog);
    EStatusCode status;
    do
    {
        status = pdfWriter.StartPDF(completePathToEmptyPages, ePDFVersion13, logConfiguration);
    }  while(false);

Output:

 Segmentation fault (core dumped)

What am I doing wrong? How to make it work?

galkahana commented 4 months ago

Not sure whats wrong. The example script seems simple. Maybe try running the tests?

raphael10-collab commented 4 months ago

The tests are all OK :

raphy@raohy:~/PDF-Writer$ ctest --test-dir ./builddir/
Internal ctest changing into directory: /home/raphy/PDF-Writer/builddir
Test project /home/raphy/PDF-Writer/builddir
      Start  1: AppendAndReplaceURLAnnotations
 1/78 Test  #1: AppendAndReplaceURLAnnotations .........   Passed    0.01 sec
      Start  2: AppendingAndReading
 2/78 Test  #2: AppendingAndReading ....................   Passed    0.00 sec
      Start  3: AppendPagesTest
 3/78 Test  #3: AppendPagesTest ........................   Passed    0.08 sec
      Start  4: AppendSpecialPagesTest
 4/78 Test  #4: AppendSpecialPagesTest .................   Passed    0.02 sec
      Start  5: AppendWithAnnotations
 5/78 Test  #5: AppendWithAnnotations ..................   Passed    0.00 sec
      Start  6: BasicModification
 6/78 Test  #6: BasicModification ......................   Passed    0.06 sec
      Start  7: BoxingBaseTest
 7/78 Test  #7: BoxingBaseTest .........................   Passed    0.00 sec
      Start  8: BufferedOutputStreamTest
 8/78 Test  #8: BufferedOutputStreamTest ...............   Passed    0.00 sec
      Start  9: CIDSetWritingCFF
 9/78 Test  #9: CIDSetWritingCFF .......................   Passed    0.02 sec
      Start 10: CIDSetWritingTrueType
10/78 Test #10: CIDSetWritingTrueType ..................   Passed    0.40 sec
      Start 11: CIDSetWritingTrueType2
11/78 Test #11: CIDSetWritingTrueType2 .................   Passed    0.09 sec
      Start 12: ColorEmojiColr
12/78 Test #12: ColorEmojiColr .........................   Passed    0.43 sec
      Start 13: ColorEmojiColrV1
13/78 Test #13: ColorEmojiColrV1 .......................   Passed    0.55 sec
      Start 14: CopyingAndMergingEmptyPages
14/78 Test #14: CopyingAndMergingEmptyPages ............   Passed    0.06 sec
      Start 15: CustomLogTest
15/78 Test #15: CustomLogTest ..........................   Passed    0.00 sec
      Start 16: DCTDecodeFilterTest
16/78 Test #16: DCTDecodeFilterTest ....................   Passed    0.06 sec
      Start 17: DFontTest
17/78 Test #17: DFontTest ..............................   Passed    0.09 sec
      Start 18: EmptyFileTest
18/78 Test #18: EmptyFileTest ..........................   Passed    0.00 sec
      Start 19: EmptyPagesPDF
19/78 Test #19: EmptyPagesPDF ..........................   Passed    0.00 sec
      Start 20: EncryptedPDF
20/78 Test #20: EncryptedPDF ...........................   Passed    0.01 sec
      Start 21: FlateEncryptionTest
21/78 Test #21: FlateEncryptionTest ....................   Passed    0.00 sec
      Start 22: FlateObjectDecodeTest
22/78 Test #22: FlateObjectDecodeTest ..................   Passed    0.00 sec
      Start 23: FormXObjectTest
23/78 Test #23: FormXObjectTest ........................   Passed    0.00 sec
      Start 24: FreeTypeInitializationTest
24/78 Test #24: FreeTypeInitializationTest .............   Passed    0.00 sec
      Start 25: HighLevelContentContext
25/78 Test #25: HighLevelContentContext ................   Passed    0.06 sec
      Start 26: HighLevelImages
26/78 Test #26: HighLevelImages ........................   Passed    0.04 sec
      Start 27: ImagesAndFormsForwardReferenceTest
27/78 Test #27: ImagesAndFormsForwardReferenceTest .....   Passed    0.01 sec
      Start 28: InputFlateDecodeTester
28/78 Test #28: InputFlateDecodeTester .................   Passed    0.00 sec
      Start 29: InputImagesAsStreamsTest
29/78 Test #29: InputImagesAsStreamsTest ...............   Passed    0.01 sec
      Start 30: JpegLibTest
30/78 Test #30: JpegLibTest ............................   Passed    0.00 sec
      Start 31: JPGImageTest
31/78 Test #31: JPGImageTest ...........................   Passed    0.00 sec
      Start 32: LinksTest
32/78 Test #32: LinksTest ..............................   Passed    0.05 sec
      Start 33: LogTest
33/78 Test #33: LogTest ................................   Passed    0.00 sec
      Start 34: MergePDFPages
34/78 Test #34: MergePDFPages ..........................   Passed    0.19 sec
      Start 35: MergeToPDFForm
35/78 Test #35: MergeToPDFForm .........................   Passed    0.00 sec
      Start 36: ModifyingEncryptedFile
36/78 Test #36: ModifyingEncryptedFile .................   Passed    0.12 sec
      Start 37: ModifyingExistingFileContent
37/78 Test #37: ModifyingExistingFileContent ...........   Passed    0.00 sec
      Start 38: OpenTypeTest
38/78 Test #38: OpenTypeTest ...........................   Passed    0.00 sec
      Start 39: OutputFileStreamTest
39/78 Test #39: OutputFileStreamTest ...................   Passed    0.00 sec
      Start 40: PageModifierTest
40/78 Test #40: PageModifierTest .......................   Passed    0.06 sec
      Start 41: PageOrderModification
41/78 Test #41: PageOrderModification ..................   Passed    0.00 sec
      Start 42: ParsingBadXref
42/78 Test #42: ParsingBadXref .........................   Passed    0.00 sec
      Start 43: ParsingFaulty
43/78 Test #43: ParsingFaulty ..........................   Passed    0.07 sec
      Start 44: PDFCopyingContextTest
44/78 Test #44: PDFCopyingContextTest ..................   Passed    0.01 sec
      Start 45: PDFDateTest
45/78 Test #45: PDFDateTest ............................   Passed    0.00 sec
      Start 46: PDFEmbedTest
46/78 Test #46: PDFEmbedTest ...........................   Passed    0.00 sec
      Start 47: PDFObjectCastTest
47/78 Test #47: PDFObjectCastTest ......................   Passed    0.00 sec
      Start 48: PDFObjectParserTest
48/78 Test #48: PDFObjectParserTest ....................   Passed    0.00 sec
      Start 49: PDFParserTest
49/78 Test #49: PDFParserTest ..........................   Passed    0.00 sec
      Start 50: PDFWithPassword
50/78 Test #50: PDFWithPassword ........................   Passed    0.11 sec
      Start 51: PFBStreamTest
51/78 Test #51: PFBStreamTest ..........................   Passed    0.00 sec
      Start 52: PNGImageTest
52/78 Test #52: PNGImageTest ...........................   Passed    0.47 sec
      Start 53: RecryptPDF
53/78 Test #53: RecryptPDF .............................   Passed    1.16 sec
      Start 54: RefCountTest
54/78 Test #54: RefCountTest ...........................   Passed    0.00 sec
      Start 55: RotatedPagesPDF
55/78 Test #55: RotatedPagesPDF ........................   Passed    0.06 sec
      Start 56: ShutDownRestartTest
56/78 Test #56: ShutDownRestartTest ....................   Passed    0.06 sec
      Start 57: SimpleContentPageTest
57/78 Test #57: SimpleContentPageTest ..................   Passed    0.00 sec
      Start 58: SimpleTextUsage
58/78 Test #58: SimpleTextUsage ........................   Passed    0.08 sec
      Start 59: TestMeasurementsTest
59/78 Test #59: TestMeasurementsTest ...................   Passed    0.05 sec
      Start 60: TextUsageBugs
60/78 Test #60: TextUsageBugs ..........................   Passed    0.20 sec
      Start 61: TIFFImageTest
61/78 Test #61: TIFFImageTest ..........................   Passed    1.11 sec
      Start 62: TiffSpecialsTest
62/78 Test #62: TiffSpecialsTest .......................   Passed    0.07 sec
      Start 63: TimerTest
63/78 Test #63: TimerTest ..............................   Passed    2.80 sec
      Start 64: TrueTypeAnsiWriteBug
64/78 Test #64: TrueTypeAnsiWriteBug ...................   Passed    0.04 sec
      Start 65: TrueTypeTest
65/78 Test #65: TrueTypeTest ...........................   Passed    0.05 sec
      Start 66: TTCTest
66/78 Test #66: TTCTest ................................   Passed    0.10 sec
      Start 67: Type1Test
67/78 Test #67: Type1Test ..............................   Passed    0.00 sec
      Start 68: UnicodeTextUsage
68/78 Test #68: UnicodeTextUsage .......................   Passed    0.05 sec
      Start 69: UppercaseSequanceTest
69/78 Test #69: UppercaseSequanceTest ..................   Passed    0.00 sec
      Start 70: WatermarkTest
70/78 Test #70: WatermarkTest ..........................   Passed    0.05 sec
      Start 71: WatermarkWithContextOpacityTest
71/78 Test #71: WatermarkWithContextOpacityTest ........   Passed    0.05 sec
      Start 72: FuzzTest_BrokenPageIDs.pdf
72/78 Test #72: FuzzTest_BrokenPageIDs.pdf .............   Passed    0.00 sec
      Start 73: FuzzTest_HeapUAF.pdf
73/78 Test #73: FuzzTest_HeapUAF.pdf ...................   Passed    0.00 sec
      Start 74: FuzzTest_InvalidEncryptionLength.pdf
74/78 Test #74: FuzzTest_InvalidEncryptionLength.pdf ...   Passed    0.00 sec
      Start 75: FuzzTest_InvalidObjectLoop.pdf
75/78 Test #75: FuzzTest_InvalidObjectLoop.pdf .........   Passed    0.00 sec
      Start 76: FuzzTest_InvalidPNGColumns.pdf
76/78 Test #76: FuzzTest_InvalidPNGColumns.pdf .........   Passed    0.00 sec
      Start 77: FuzzTest_NegativePrevValue.pdf
77/78 Test #77: FuzzTest_NegativePrevValue.pdf .........   Passed    0.00 sec
      Start 78: FuzzTest_PDFParserHighCount.pdf
78/78 Test #78: FuzzTest_PDFParserHighCount.pdf ........   Passed    0.00 sec

100% tests passed, 0 tests failed out of 78

Total Test time (real) =   9.14 sec
raphy@raohy:~/PDF-Writer$ 
galkahana commented 4 months ago

Hmm. Can you tell whars the diff between the emptypages test and your attenpt? Either try to figure this out by modifying emptypages test to yoyr scenario, step by step to realize whats the problem, or send me the soyrce code to try to figure this out myself (im guessing what you quoted aint all the story, not sure what that code block inside private declaration means

raphael10-collab commented 4 months ago

Taking this https://github.com/galkahana/PDF-Writer/blob/master/PDFWriterTesting/EmptyFileTest.cpp as reference,

In main.cpp :

#include "PDFWriter.h"

using namespace PDFHummus;

EStatusCode status;
PDFWriter pdfWriter;

Output : Segmentation fault (core dumped)

The line that causes the crash is the last one: PDFWriter pdfWriter;

In CmakeLists.txt file :

FetchContent_Declare(
  PDFHummus
  URL https://github.com/galkahana/PDF-Writer/archive/refs/tags/v4.6.2.tar.gz
  URL_HASH SHA256=0a36815ccc9d207028567f90039785c824b211169ba5da68de84d0c15455ab62
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
  FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(PDFHummus)

target_link_libraries (${PROJECT_NAME} PUBLIC
    PDFHummus::PDFWriter
)
galkahana commented 4 months ago

i honestly have no idea. this sounds like a very basic setup, and the way you fetch the project is the same thing i do for https://github.com/galkahana/pdf-text-extraction.

raphael10-collab commented 3 months ago

I actually took the FetchContent_Declare from https://github.com/galkahana/pdf-text-extraction/blob/master/CMakeLists.txt The only difference I see, but may be there are others, is in the cpack declaration : https://github.com/galkahana/pdf-text-extraction/blob/master/CMakeLists.txt#L33

Do I need to install it as well?

galkahana commented 3 months ago

No, you dont have to. Its super weird you van run the tests...id expect a similar failure. Do you mind sharing the relevant parts of your project so i can attenpt to recreate? Maybe also some details on what system your are attempting to run this on

raphael10-collab commented 3 months ago

I've made an interesting test

I removed the previous references to PDFWriter in the source code, and I moved all the PDFWriter-related code into an .h file and an .cpp file, in order to avoid any possible interference by the rest of code

./src/PDFWriter/EmptyFileTest.h :

#include "PDFWriter.h"
using namespace PDFHummus;

namespace Grasp {
    void EmptyFileTest();
}

./src/PDFWriter/EmptyFileTest.cpp :

#include "EmptyFileTest.h"

namespace Grasp {

    void EmptyFileTest()
    {
        EStatusCode status;
        PDFWriter pdfWriter;
    }
}

Without including EmptyFilteTest.h into main.cpp, I get the crash: Segmentation fault (core dumped) , and the code yielding to the crash is : PDFWriter pdfWriter;

This is the CMakeLists.txt file :

cmake_minimum_required(VERSION 3.5)
project(Grasp)

find_package(wxWidgets REQUIRED COMPONENTS base core webview aui stc net xml html richtext)
include(${wxWidgets_USE_FILE})

find_package(CURL)
find_package(OpenSSL)
find_package(Threads)
find_package(nng CONFIG REQUIRED)
find_package(json-c)

include(FetchContent)

find_package(PkgConfig REQUIRED)

FetchContent_Declare(
  PDFHummus
  URL https://github.com/galkahana/PDF-Writer/archive/refs/tags/v4.6.2.tar.gz
  URL_HASH SHA256=0a36815ccc9d207028567f90039785c824b211169ba5da68de84d0c15455ab62
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
  FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(PDFHummus)

pkg_check_modules(tesseract REQUIRED IMPORTED_TARGET tesseract)
set_property(TARGET PkgConfig::tesseract APPEND PROPERTY INTERFACE_LINK_LIBRARIES curl archive)
pkg_check_modules(leptonica REQUIRED IMPORTED_TARGET lept)
pkg_check_modules(libcurl REQUIRED IMPORTED_TARGET libcurl)
pkg_check_modules(json-c REQUIRED IMPORTED_TARGET json-c)
pkg_check_modules(sqlite3 REQUIRED IMPORTED_TARGET sqlite3)
pkg_check_modules(pdfio REQUIRED IMPORTED_TARGET pdfio)

link_libraries(${wxWidgets_LIBRARIES})

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_CXX_FLAGS "-fexceptions")

if (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    add_executable(${PROJECT_NAME}
        src/main.cpp
        // a list of other files
        src/PDFWriter/EmptyFileTest.cpp
)

elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    add_executable(${PROJECT_NAME}
        src/main.cpp
        // a list of other files
        src/PDFWriter/EmptyFileTest.cpp
    )
endif()

target_link_libraries (${PROJECT_NAME} PUBLIC
    PkgConfig::leptonica
    PkgConfig::tesseract
    PDFHummus::PDFWriter
    wxWidgets::wxWidgets
    Threads::Threads
    OpenSSL::SSL
    OpenSSL::Crypto
    curl
    nng::nng
    xml2
    json-c::json-c
    icui18n
    icuuc
    stfl
    ncursesw
    sqlite3
    pdfio
)

target_compile_definitions(${PROJECT_NAME} PRIVATE NNG_ELIDE_DEPRECATED)

Other info :

O.S.: Ubuntu 23.10
gcc version: 13.2.0

I've tried to use the same settings (the same CMakeLists.txt apart from the source files) with a toy project, and there are no issues at all Here you can find the code: https://github.com/raphael10-collab/context-wxwebview.git

The only difference between this toy-code and the "real" project is in the source code files. But these source code files do not import EmptyFileTest.h and therefore does not use EmptyFileTest.cpp source code

@galkahana What else should I try to spot the root cause and solve the issue?