Skip to content

Conversation

@stokito
Copy link
Contributor

@stokito stokito commented Aug 2, 2025

I started the Psi with Valgrind (valgrind --track-origins=yes psi) and on receiving MUC messages I got a lot of such warnings:

  Uninitialised value was created by a heap allocation
    at 0x4851E10: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x4D9FC68: QArrayData::reallocateUnaligned(QArrayData*, unsigned long, unsigned long, QFlags<QArrayData::AllocationOption>) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.15.15)
    by 0x4E1FF21: QString::reallocData(unsigned int, bool) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.15.15)
    by 0x65A315: TextUtil::resolveEntities(QStringView const&) (in /usr/local/bin/psi)
    by 0x63872D: RTParse::next() (in /usr/local/bin/psi)
    by 0x65A58F: TextUtil::emoticonify(QString const&) (in /usr/local/bin/psi)
    by 0x4F4804: MessageView::formattedText() const (in /usr/local/bin/psi)
    by 0x69E8FB: ChatView::renderMucMessage(MessageView const&, QTextCursor&) (in /usr/local/bin/psi)
    by 0x6A603D: ChatView::dispatchMessage(MessageView const&) (in /usr/local/bin/psi)
    by 0x485D14: GCMainDlg::dispatchMessage(MessageView const&) (in /usr/local/bin/psi)
    by 0x488E82: GCMainDlg::appendMessage(XMPP::Message const&, bool) (in /usr/local/bin/psi)
    by 0x49457D: GCMainDlg::message(XMPP::Message const&, QSharedPointer<PsiEvent> const&) (in /usr/local/bin/psi)

I have no idea how to fix this but I checked the TextUtil::resolveEntities and it looks like it just tries to unescape XML sequences i.e. &amp; to &.
The same class has the method unescape() that does the same:

    QString plain = escaped;
    plain.replace("&lt;", "<");
    plain.replace("&gt;", ">");
    plain.replace("&quot;", "\"");
    plain.replace("&amp;", "&");
    return plain;

If my guess is correct then the resolveEntities() has a bug: it won't work when the & doesn't mean an escape sequence e.g. hello & bye, it will return hello . So in the PR I fixed the bug and slightly improved its performance.

But instead we can just call that unescape() method:

QString TextUtil::resolveEntities(const QStringView &in)
{
    QString out = in.toString();
    unescape(out);
    return out;
}

In fact it may work even faster but needs to be benchmarked. This is a critical for performance place (especially memory usage). So here is another branch with a simpler change https://github.com/stokito/psi/tree/resolveEntities_replace

Please select any variant.

But the Valgrind warning remains even with the change so the problem is probably somewhere in the RTParse::next() or TextUtil::emoticonify().

@Neustradamus
Copy link
Contributor

@stokito: Linked to this bug?

@stokito
Copy link
Contributor Author

stokito commented Aug 15, 2025

I debugged more. The text inside is always escaped so in practice there is no such situations when we have escaped entity other that &lt;, &gt, &apos; etc. So the PR can be ignored, while it still has some minor performance improvement. I'll try to check later more if it worth it.

@Neustradamus
Copy link
Contributor

With initial default theme, when we received a quote, we receive, for example (1:1 here):

<message from="[email protected]/client" to="[email protected]" id="XXXXXXXXXXXXXXXXXXXXXXX" xml:lang="en" type="chat">
<body>Main quote was
&gt; test</body>
<request xmlns="urn:xmpp:receipts"/>
<markable xmlns="urn:xmpp:chat-markers:0"/>
<origin-id xmlns="urn:xmpp:sid:0" id="XXXXXXXXXXXXXXXXXXXXXXX"/>
<html xmlns="http://jabber.org/protocol/xhtml-im">
<body xmlns="http://www.w3.org/1999/xhtml">Main quote was<br/>
<blockquote>test</blockquote>
</body>
</html>
</message>

The next setence is not at the border.

Screenshot example:

@stokito stokito marked this pull request as draft August 23, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants