Xournalpp Dev Crashes When Writing Empty Text Nodes
Introduction
Xournal++ is a popular note-taking application that allows users to create and edit documents with ease. However, some users have reported that the dev version of Xournal++ crashes when writing empty text nodes. In this article, we will delve into the issue and provide a solution to fix the problem.
Operating System and Environment
The issue was reported on Arch Linux with the sway desktop environment and Wayland display server. The user was using the AUR (Arch User Repository) to install Xournal++.
Xournal++ Version and libgtk Version
The user was using the dev version of Xournal++ (1.2.6+dev) with the commit hash ae01d6da. The libgtk version was 3.24.49.
Bug Description
The user reported that Xournal++ crashes when saving a file with an empty text node. The file was created using the dev version of Xournal++ and had the following XML structure:
<?xml version="1.0" standalone="no"?>
<xournal creator="xournalpp 1.2.6" fileversion="4">
<title>Xournal++ document - see https://xournalpp.github.io/</title>
<page width="595.27559" height="841.88976">
<background type="solid" color="#ffffffff" style="lined"/>
<layer>
<text font="Sans" size="12" x="297.5" y="412.5" color="#000000ff"></text>
</layer>
</page>
</xournal>
Stacktrace
The user provided a stacktrace that showed the crash occurred when writing to an OutputStream:
(com.github.xournalpp.xournalpp:167389): xopp-CRITICAL **: 12:00:17.917: Assertion failed: len != 0 && this->fp
in function write
at line 42 of /usr/src/debug/xournalpp-git/xournalpp/src/util/OutputStream.cpp
Thread 13 "XournalSchedule" received signal SIGABRT, Aborted.
[Switching to Thread 0xffffece4eec0 (LWP 167405)]
0x0000fffff62970f8 in ?? () from /usr/lib/libc.so.6
(gdb) bt
quit
#0 0x0000fffff62970f8 in ?? () from /usr/lib/libc.so.6
#1 0x0000fffff6246f9c in raise () from /usr/lib/libc.so.6
#2 0x0000fffff6231938 in abort () from /usr/lib/libc.so.6
#3 0x0000aaaaaad709d0 in xoj::util::assertFailure (expr=expr@entry=0xaaaaaadb0458 "len != 0 && this->fp", msg="",
fileName=fileName@entry=0xaaaaaadb0410 "/usr/src/debug/xournalpp-git/xournalpp/src/util/OutputStream.cpp", line=line@entry=42,
funcName=funcName@entry=0xaaaaaadb0408 "write") at /usr/src/debug/xournalpp-git/xournalpp/src/util/Assert.cpp:12
#4 0x0000aaaaaad77e00 in GzOutputStream::write (this=0xffffece4e230, data=<optimized out>, len=<optimized out>)
at /usr/src/debug/xournalpp-git/xournalpp/src/util/OutputStream.cpp:42
#5 0x0000aaaaaac8d830 in XmlTextNode::writeOut (this=0xffffcc1ebaa0, out=0xffffece4e230)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlTextNode.cpp:25
#6 0x0000aaaaaab72c18 in XmlNode::writeOut (this=<optimized out>, out=0xffffece4e230, listener=0x0)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlNode.cpp:61
#7 0x0000aaaaaab72ba8 in XmlNode::writeOut (this=<optimized out>, out=0xffffece4e230)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlNode.h:39
#8 XmlNode::writeOut (this=0xffffcc1f79c0, out=0xffffece4e230, listener=0x0)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlNode.cpp:61
#9 0x0000aaaaaab72ba8 in XmlNode::writeOut (this=<optimized out>, out=0xffffece4e230)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlNode.h:39
#10 XmlNode::writeOut (this=0xffffcc1ebe60, out=0xffffece4e230, listener=0xaaaaaaf972c0)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xml/XmlNode.cpp:61
#11 0x0000aaaaaab98e5c in SaveHandler::saveTo (this=this@entry=0xffffece4e3e0, out=out@entry=0xffffece4e230,
filepath=filesystem::path "/home/user/sud.xopp" = {...}, listener=listener@entry=0xaaaaaaf972c0)
at /usr/include/c++/14.2.1/bits/unique_ptr.h:193
#12 0x0000aaaaaab9ae60 in SaveHandler::saveTo (this=this@entry=0xffffece4e3e0, filepath=filesystem::path "/home/user/sud.xopp" = {...},
listener=0xaaaaaaf972c0) at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/xojfile/SaveHandler.cpp:326
#13 0x0000aaaaaac61694 in SaveJob::save (this=this@entry=0xaaaaaaed7af0)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/jobs/SaveJob.cpp:133
#14 0x0000aaaaaac623d8 in SaveJob::run (this=0xaaaaaaed7af0) at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/jobs/SaveJob.cpp:30
#15 0x0000aaaaaac59d7c in BlockingJob::execute (this=0xaaaaaaed7af0)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/jobs/BlockingJob.cpp:18
#16 0x0000aaaaaab5ff24 in Scheduler::jobThreadCallback (scheduler=0xaaaaab049f50)
at /usr/src/debug/xournalpp-git/xournalpp/src/core/control/jobs/Scheduler.cpp:247
#17 0x0000fffff69c64e4 in ?? () from /usr/lib/libglib-2.0.so.0
#18 0x0000fffff6295300 in ?? () from /usr/lib/libc.so.6
#19 0x0000fffff62faa0c in ?? () from /usr/lib/libc.so.6
Solution
The issue is caused by an assertion failure in the OutputStream class. The assertion checks if the length of the data to be written is not zero and if the file pointer is valid. However, in the case of an empty text node, the length of the data is zero, causing the assertion to fail.
To fix the issue, we need to modify the OutputStream class to handle empty text nodes correctly. We can do this by adding a check for empty text nodes before writing to the OutputStream.
Here is the modified code:
void OutputStream::write(const char* data, size_t len) {
if (len == 0) {
// Handle empty text nodes
return;
}
// Write data to OutputStream
}
Conclusion
In this article, we have identified the issue of Xournal++ dev crashing when writing empty text nodes. We have also provided a solution to fix the issue by modifying the OutputStream class to handle empty text nodes correctly. The modified code checks for empty text nodes before writing to the OutputStream, preventing the assertion failure and the subsequent crash.
Additional Information
- The issue was reported on Arch Linux with the sway desktop environment and Wayland display server.
- The user was using the dev version of Xournal++ (1.2.6+dev) with the commit hash ae01d6da.
- The libgtk version was 3.24.49.
- The file was created using the dev version of Xournal++ and had the following XML structure:
<?xml version="1.0" standalone="no"?>
<xournal creator="xournalpp 1.2.6" fileversion="4">
<title>Xournal++ document - see https://xournalpp.github.io/</title>
<page width="595.27559" height="841.88976">
<background type="solid" color="#ffffffff" style="lined"/>
<layer>
<text font="Sans" size="12" x="297.5" y="412.5" color<br/>
**Xournal++ Dev Crashes When Writing Empty Text Nodes: Q&A**
=====================================================
**Q: What is the issue with Xournal++ dev crashing when writing empty text nodes?**
--------------------------------------------------------------------------------
A: The issue is caused by an assertion failure in the OutputStream class. The assertion checks if the length of the data to be written is not zero and if the file pointer is valid. However, in the case of an empty text node, the length of the data is zero, causing the assertion to fail.
**Q: What is the solution to fix the issue?**
------------------------------------------------
A: The solution is to modify the OutputStream class to handle empty text nodes correctly. We can do this by adding a check for empty text nodes before writing to the OutputStream.
**Q: How do I modify the OutputStream class to handle empty text nodes?**
-------------------------------------------------------------------
A: You can modify the OutputStream class by adding a check for empty text nodes before writing to the OutputStream. Here is the modified code:
```cpp
void OutputStream::write(const char* data, size_t len) {
if (len == 0) {
// Handle empty text nodes
return;
}
// Write data to OutputStream
}
Q: What is the XML structure of the file that causes the crash?
A: The file has the following XML structure:
<?xml version="1.0" standalone="no"?>
<xournal creator="xournalpp 1.2.6" fileversion="4">
<title>Xournal++ document - see https://xournalpp.github.io/</title>
<page width="595.27559" height="841.88976">
<background type="solid" color="#ffffffff" style="lined"/>
<layer>
<text font="Sans" size="12" x="297.5" y="412.5" color="#000000ff"></text>
</layer>
</page>
</xournal>
Q: What is the libgtk version that is affected by the issue?
A: The libgtk version that is affected by the issue is 3.24.49.
Q: What is the Xournal++ version that is affected by the issue?
A: The Xournal++ version that is affected by the issue is 1.2.6+dev with the commit hash ae01d6da.
Q: What is the operating system and environment that is affected by the issue?
A: The issue is reported on Arch Linux with the sway desktop environment and Wayland display server.
Q: How can I prevent the crash when writing empty text nodes?
A: You can prevent the crash by modifying the OutputStream class to handle empty text nodes correctly. You can also try updating to the latest version of Xournal++.
Q: Where can I find more information about the issue?
A: You can find more information about the issue on the Xournal++ GitHub page or by searching for the issue on the Xournal++ forums.
Q: How can I report the issue to the Xournal++ developers?
A: You can report the issue to the Xournal++ developers by creating a new issue on the Xournal++ GitHub page or by sending an email to the Xournal++ mailing list.