-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Segmentation fault #2684
Comments
Sorry to bump this, but I'm encountering the exact same issue with some html i'm generating and I couldn't find any answers. Did you manage to fix it, @alexch1700? |
Also getting a segmentation fault occasionally for certain HTML inputs. It's hard to find a common thread between those which fail and often just changing a handful of characters, or some properties of the conversion, will make it succeed. A reproduceable example input HTML here: https://gist.github.com/mikecmpbll/3dd488aa2cda1984111c788c358b110d Which I run like this:
This happens for me on 32 bit Ubuntu 12.04.5 and 64 bit Ubuntu 14.04.4 with wkhtmltopdf 0.12.3 (with patched qt), but it doesn't fail on OSX with 0.12.3. |
just posted a $500 bounty for this issue (my issue, if it's different) for anyone who's able to get to the bottom of this. https://www.bountysource.com/issues/28572614-segmentation-fault |
Not a solution to this issue but a workaround is to try to change margin_bottom in a loop. Context :
Python code sample :
It's not a good solution but it avoids errors waiting a better proposition. |
@alexch1700 : nice idea. at the moment we're falling back to rendering the HTML but this could be better, thanks. |
Hmm, it's possible that the issue is already fixed -- can you see if the patch I posted on another issue fixes it for you? Please refer to the comments below, so that I can confirm that everything is OK with this change. |
same error with that patch, for me. i notice mine seems somewhat different to these issues as i don't get the characteristic "Page 4 of 3" (e.g.) in my output as it fails earlier during counting pages .. |
@mikecmpbll: it looks like using Not sure what is exactly causing the problem, but you can try bisecting by removing half of the CSS, check if it works and repeat till you find the offender. Until the browser engine is upgraded, you should avoid certain CSS constructs. |
For the original reporters @alexch1700 and @mihaiscurtucb -- does the patch mentioned in the issue work for you? |
@mikecmpbll please check the pull request mentioned above. diff --git a/src/gui/painting/qprintengine_pdf.cpp b/src/gui/painting/qprintengine_pdf.cpp
index 751e4ce..e150a83 100644
--- a/src/gui/painting/qprintengine_pdf.cpp
+++ b/src/gui/painting/qprintengine_pdf.cpp
@@ -833,7 +833,29 @@ public:
};
+bool QPdfEnginePrivate::smartCompressDeflate(const char *source, int size, QByteArray& compressed, unsigned long maxSize)
+{
+ if (maxSize == 0) {
+ maxSize = ::compressBound(size);
+ }
+
+ unsigned long destLen = min(static_cast<unsigned long>(QPdfPage::chunkSize()), maxSize);
+
+ while (true) {
+ compressed.resize(destLen);
+ if (Z_OK == ::compress(reinterpret_cast<Bytef *>(compressed.data()), &destLen, reinterpret_cast<const Bytef *>(source), size)) {
+ compressed.truncate(destLen);
+ break;
+ }
+ if (static_cast<unsigned long>(compressed.size()) >= maxSize) {
+ compressed.clear();
+ return false;
+ }
+ destLen = min(static_cast<unsigned long>(compressed.size()) * 2, maxSize);
+ }
+ return true;
+}
/*!
* Adds an image to the pdf and return the pdf-object id. Returns -1 if adding the image failed.
@@ -907,33 +929,25 @@ int QPdfEnginePrivate::addImage(const QImage &img, bool *bitmap, qint64 serial_n
if (noneScaled && noneScaled->rect() != image.rect()) {
QByteArray imageData2;
convertImage(*noneScaled, imageData2);
- uLongf len = imageData2.size();
- uLongf destLen = len + len/100 + 13; // zlib requirement
- Bytef* dest = new Bytef[destLen];
- if (Z_OK == ::compress(dest, &destLen, (const Bytef*) imageData2.data(), (uLongf)len) &&
- (uLongf)destLen < target) {
- imageData=imageData2;
- target=destLen;
- dct=false;
- uns=true;
+ QByteArray compressed;
+ if (smartCompressDeflate(imageData2.data(), imageData2.size(), compressed, target)) {
+ imageData = imageData2;
+ target = compressed.size();
+ dct = false;
+ uns = true;
}
- delete[] dest;
}
{
QByteArray imageData2;
convertImage(image, imageData2);
- uLongf len = imageData2.size();
- uLongf destLen = len + len/100 + 13; // zlib requirement
- Bytef* dest = new Bytef[destLen];
- if (Z_OK == ::compress(dest, &destLen, (const Bytef*) imageData2.data(), (uLongf)len) &&
- (uLongf)destLen < target) {
- imageData=imageData2;
- target=destLen;
- dct=false;
- uns=false;
+ QByteArray compressed;
+ if (smartCompressDeflate(imageData2.data(), imageData2.size(), compressed, target)) {
+ imageData = imageData2;
+ target = compressed.size();
+ dct = false;
+ uns = false;
}
- delete[] dest;
}
@@ -1182,16 +1196,13 @@ int QPdfEnginePrivate::writeCompressed(const char *src, int len)
{
#ifndef QT_NO_COMPRESS
if(doCompress) {
- uLongf destLen = len + len/100 + 13; // zlib requirement
- Bytef* dest = new Bytef[destLen];
- if (Z_OK == ::compress(dest, &destLen, (const Bytef*) src, (uLongf)len)) {
- stream->writeRawData((const char*)dest, destLen);
+ QByteArray compressed;
+ if (smartCompressDeflate(src, len, compressed)) {
+ stream->writeRawData((const char*)compressed.data(), compressed.size());
} else {
qWarning("QPdfStream::writeCompressed: Error in compress()");
- destLen = 0;
}
- delete [] dest;
- len = destLen;
+ len = compressed.size();
} else
#endif
{
diff --git a/src/gui/painting/qprintengine_pdf_p.h b/src/gui/painting/qprintengine_pdf_p.h
index 773706d..7db5e54 100644
--- a/src/gui/painting/qprintengine_pdf_p.h
+++ b/src/gui/painting/qprintengine_pdf_p.h
@@ -233,6 +233,7 @@ private:
streampos += data.size();
}
+ bool smartCompressDeflate(const char *source, int size, QByteArray& compressed, unsigned long maxSize = 0);
int writeCompressed(const char *src, int len);
inline int writeCompressed(const QByteArray &data) { return writeCompressed(data.constData(), data.length()); }
int writeCompressed(QIODevice *dev); |
@pchk: what does the PR have to do with the crash? |
@pchk : i gave it a go but same error for me :) |
@mikecmpbll: your crash is linked to |
Marking as duplicate unless the original reporters @alexch1700 and @mihaiscurtucb can confirm otherwise -- in which case, please post a test case. |
@ashkulz : sorry, wasn't ignoring your earlier comment, i'm going to do the css bisection as suggested just haven't got around to it yet! thanks so much for pointing this out to me 😌 |
@ashkulz PR reduces memory consumption like a reason for that crash. |
@pchk: please address the comments made in the PR. |
@pchk : sure, if the pdf can be rendered without changes that'll save me some time and would certainly be eligible for the bounty. (i don't know that it's possible though for the reasons ashkulz says about WebKit?) |
Finally got it fixed. Updated the pull request. diff --git a/src/3rdparty/webkit/Source/WebCore/rendering/RenderBlock.cpp b/src/3rdparty/webkit/Source/WebCore/rendering/RenderBlock.cpp
index d8412db..5fd713c 100644
--- a/src/3rdparty/webkit/Source/WebCore/rendering/RenderBlock.cpp
+++ b/src/3rdparty/webkit/Source/WebCore/rendering/RenderBlock.cpp
@@ -3234,6 +3234,8 @@ void RenderBlock::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logi
floatingObjectSet.removeLast();
ASSERT(!curr->m_originatingLine);
delete curr;
+ if (floatingObjectSet.isEmpty())
+ break;
curr = floatingObjectSet.last();
}
} |
@mikecmpbll: verify if the crash gets fixed with this and no other backports are required. It feels really good to get code contributions from @pchk -- wish more people posted bounties 😄 |
wkhtmltopdf issue: wkhtmltopdf/wkhtmltopdf#2684 upstream report: https://bugs.webkit.org/show_bug.cgi?id=68550 upstream fix: https://trac.webkit.org/changeset/95654
In the case when original image comes to our code in format ARGB32_Premultiplied we convert it to format ARGB32. The conversion constructs new QImage object and allocates buffer for it. Conversion will be done for scaled and nonScaled QImage objects. If we support ARGB32_Premultiplied two unnecessary allocations will be skipped saving us a lot memory. Ex.: In test fail.html mentioned in wkhtmltopdf/wkhtmltopdf#2684 i saw that large nonScaled QImage consums memory amount about 300mb (8000px x 10000px 4 byte ARGB32_Premultiplied). And when we convert it into ARGB32 it allocated another ~ 300mb of memory for new QImage object. But we can work with that format wothout conversion.
In the case when original image comes to our code in format ARGB32_Premultiplied we convert it to format ARGB32. The conversion constructs new QImage object and allocates buffer for it. Conversion will be done for scaled and nonScaled QImage objects. If we support ARGB32_Premultiplied two unnecessary allocations will be skipped saving us a lot memory. Ex.: In test fail.html mentioned in wkhtmltopdf/wkhtmltopdf#2684 i saw that large nonScaled QImage consums memory amount about 300mb (8000px x 10000px 4-byte ARGB32_Premultiplied). And when we convert it into ARGB32 it allocates another ~ 300mb of memory for new QImage object. But we can work with that format wothout conversion.
Usally buffer size that fits all compressed data is much lesser that source uncompressed data size. This routine has initial size of buffer = 10mb. Usually is is enough to compress any image that comes to a printer. In the worst case the buffer will grow up to :;compressBound result thats guarantees to fit all compressed data. Ex: In pdf_fail.html(235kb) metioned in wkhtmltopdf/wkhtmltopdf#2684 nonScaled image ~300mb, real buffer size needed for compression will be ~3mb.
Just to confirm: I think you wouldn't get this issue with |
wkhtmltopdf issue: wkhtmltopdf/wkhtmltopdf#2684 upstream report: https://bugs.webkit.org/show_bug.cgi?id=68550 upstream fix: https://trac.webkit.org/changeset/95654
In the case when original image comes to our code in format ARGB32_Premultiplied we convert it to format ARGB32. The conversion constructs new QImage object and allocates buffer for it. Conversion will be done for scaled and nonScaled QImage objects. If we support ARGB32_Premultiplied two unnecessary allocations will be skipped saving us a lot me FE74 mory. Ex.: In test fail.html mentioned in wkhtmltopdf/wkhtmltopdf#2684 i saw that large nonScaled QImage consums memory amount about 300mb (8000px x 10000px 4-byte ARGB32_Premultiplied). And when we convert it into ARGB32 it allocates another ~ 300mb of memory for new QImage object. But we can work with that format wothout conversion.
fixes #2684 via wkhtmltopdf/qt@b643990 additional fix: wkhtmltopdf/qt@eced0bb fixes #2353 via wkhtmltopdf/qt@fe05fcd
|
@ashkulz I'm experiencing this same issue with 0.12.4 |
fixes wkhtmltopdf#2684 via wkhtmltopdf/qt@b643990 additional fix: wkhtmltopdf/qt@eced0bb fixes wkhtmltopdf#2353 via wkhtmltopdf/qt@fe05fcd
I'm using 0.12.4 and I have similar problems in the following issue #3366 I am however not using normalize.css, and my workaround so far is to add many |
I also had this problem with several documents and wkhtmltopdf 0.12.4 : "Page 4 of 3". I think this commit can solve this situation: 6f77c46. It is a part of wkhtmltox-0.12.5-rc. I've downloaded binary for CentOS6, I've tested this today and it passed! With the same input data 0.12.4 fails and 0.12.5 works fine. I keep my fingers crossed that this is the definitive fix! 🤞 |
Finally 0.12.5 resolved this for me as well! |
Still getting Segmentation fault with wkhtmltopdf 0.12.5 (with patched qt) Has someone any workaround for this version? Thanks |
I am also getting a Segmentation fault with wkhtmltopdf 0.12.5 (with patched qt) |
Can you open a new issue with a reproducible test case? Thanks! |
Unfortunately I am unable to provide a reproducible test case as the document in question contains sensitive data. |
@ETSJustin can you at least make a debug build using |
@ashkulz unfortunately I failed to do this as I did not understand the steps required to make a debug build as I lack the required skills. I did the following:
But I got:
TBH, I have no idea what arguments are required! |
Got that error too on Ubintu 16.04 (wkhtmltopdf 0.12.5 with patched qt). Huge source file (over 7M) Works correctly on Windows with same data |
Got the segfault error on Red Hat 7.6, wkhtmltopdf 0.12.4 (with patched qt). |
I'm having this same error on linux with version 0.12.6:
Locally on my MAC it works just fine, with the same version... |
When I execute the following command line
with following footer
and following body
a Segmentation error (core dumped) occure.
Here is the execution trace
where we can see Printing Page 4 of 3.
I work on Ubuntu 12.04 with wkhtmltopdf 0.12.2.1 (with patched qt).
Thank you for your help
The text was updated successfully, but these errors were encountered: