Bug 447553 - Kate crashes when opening TypeScript (.ts) file with LSP plugin enabled
Summary: Kate crashes when opening TypeScript (.ts) file with LSP plugin enabled
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: general (show other bugs)
Version: 21.12.0
Platform: Arch Linux Linux
: NOR crash
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords: drkonqi
Depends on:
Blocks:
 
Reported: 2021-12-26 21:13 UTC by qewer
Modified: 2021-12-28 12:47 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description qewer 2021-12-26 21:13:28 UTC
Application: kate (21.12.0)

Qt Version: 5.15.2
Frameworks Version: 5.89.0
Operating System: Linux 5.15.10-arch1-1 x86_64
Windowing System: X11
Distribution: "Arch Linux"
DrKonqi: 5.23.4 [KCrashBackend]

-- Information about the crash:
- What I was doing when the application crashed:
Opening a random TypeScript (.ts) file.

- Custom settings of the application:
LSP Client plugin is enabled (with default configuration file which includes the TypeScript language server).
Semantic Highlighting inside LSP Client plugin settings is enabled, does not crash when this setting is disabled.

The crash can be reproduced every time.

-- Backtrace:
Application: Kate (kate), signal: Aborted

[KCrash Handler]
#4  0x00007ff89cdb0d22 in raise () from /usr/lib/libc.so.6
#5  0x00007ff89cd9a862 in abort () from /usr/lib/libc.so.6
#6  0x00007ff89cfd9802 in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#7  0x00007ff89cfe5c8a in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#8  0x00007ff89cfe5cf7 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#9  0x00007ff89cfe5f8e in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x7ff89d148208 <typeinfo for std::out_of_range>, dest=0x7ff89cffc010 <std::out_of_range::~out_of_range()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#10 0x00007ff89cfdc514 in std::__throw_out_of_range_fmt (__fmt=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/src/c++11/functexcept.cc:100
#11 0x00007ff895ea20c4 in ?? () from /usr/lib/qt/plugins/ktexteditor/lspclientplugin.so
#12 0x00007ff895e73dee in ?? () from /usr/lib/qt/plugins/ktexteditor/lspclientplugin.so
#13 0x00007ff895e8ad87 in ?? () from /usr/lib/qt/plugins/ktexteditor/lspclientplugin.so
#14 0x00007ff89d400ad3 in ?? () from /usr/lib/libQt5Core.so.5
#15 0x00007ff89d3563b6 in ?? () from /usr/lib/libQt5Core.so.5
#16 0x00007ff89d35cfa5 in ?? () from /usr/lib/libQt5Core.so.5
#17 0x00007ff89d400b1f in ?? () from /usr/lib/libQt5Core.so.5
#18 0x00007ff89d402794 in QSocketNotifier::activated(QSocketDescriptor, QSocketNotifier::Type, QSocketNotifier::QPrivateSignal) () from /usr/lib/libQt5Core.so.5
#19 0x00007ff89d4028df in QSocketNotifier::event(QEvent*) () from /usr/lib/libQt5Core.so.5
#20 0x00007ff89de691a6 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt5Widgets.so.5
#21 0x00007ff89d3cff8a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
#22 0x00007ff89d419fb6 in ?? () from /usr/lib/libQt5Core.so.5
#23 0x00007ff89b58952c in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#24 0x00007ff89b5dd7b9 in ?? () from /usr/lib/libglib-2.0.so.0
#25 0x00007ff89b586c11 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
#26 0x00007ff89d41b18a in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
#27 0x00007ff89d3c826b in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt5Core.so.5
#28 0x00007ff89d3d39b7 in QCoreApplication::exec() () from /usr/lib/libQt5Core.so.5
#29 0x00005628a732a3ca in ?? ()
#30 0x00007ff89cd9bb25 in __libc_start_main () from /usr/lib/libc.so.6
#31 0x00005628a732bafe in _start ()
[Inferior 1 (process 121027) detached]

Possible duplicates by query: bug 443387, bug 417547, bug 403730, bug 394178, bug 393853.

Reported using DrKonqi
Comment 1 Waqar Ahmed 2021-12-27 11:20:35 UTC
Hi, thanks for the report.

The backtrace is useless, but seeing that you are on Arch, maybe you can compile kate and get a better backtrace?

You need to do the following in sequence:

```
git clone https://invent.kde.org/utilities/kate.git
cd kate
mkdir build && cd build
cmake ..
make -j8
QT_PLUGIN_PATH=./bin ./bin/kate
```

The above assumes that you have the following packages installed:
- gcc
- gdb
- cmake
- git

Should take a few minutes. After the last command kate will open with plugins loaded from the "./bin" directory inside the "build" directory you created. Now open your project via "File -> Open Folder". Make it crash. Then

```
coredumpctl debug
```

once the above gives you a (gdb) prompt, type

bt

and copy paste the backtrace here.
Comment 2 Waqar Ahmed 2021-12-27 11:42:32 UTC
Failed to reproduce with typescript-language-server 0.8.1 on a small repo and kate from master branch.
Comment 3 qewer 2021-12-27 16:04:25 UTC
(In reply to Waqar Ahmed from comment #1)
> Hi, thanks for the report.
> 
> The backtrace is useless, but seeing that you are on Arch, maybe you can
> compile kate and get a better backtrace?
> 
> You need to do the following in sequence:
> 
> ```
> git clone https://invent.kde.org/utilities/kate.git
> cd kate
> mkdir build && cd build
> cmake ..
> make -j8
> QT_PLUGIN_PATH=./bin ./bin/kate
> ```
> 
> The above assumes that you have the following packages installed:
> - gcc
> - gdb
> - cmake
> - git
> 
> Should take a few minutes. After the last command kate will open with
> plugins loaded from the "./bin" directory inside the "build" directory you
> created. Now open your project via "File -> Open Folder". Make it crash. Then
> 
> ```
> coredumpctl debug
> ```
> 
> once the above gives you a (gdb) prompt, type
> 
> bt
> 
> and copy paste the backtrace here.

I followed the instructions, this is the ourput backtrace:
```
#0  0x00007f95a59a4d22 in raise () from /usr/lib/libc.so.6
#1  0x00007f95a7653540 in KCrash::defaultCrashHandler(int) () from /usr/lib/libKF5Crash.so.5
#2  <signal handler called>
#3  0x00007f95a59a4d22 in raise () from /usr/lib/libc.so.6
#4  0x00007f95a598e862 in abort () from /usr/lib/libc.so.6
#5  0x00007f95a5d2c802 in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#6  0x00007f95a5d38c8a in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#7  0x00007f95a5d38cf7 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#8  0x00007f95a5d38f8e in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x7f95a5e9b208 <typeinfo for std::out_of_range>, 
    dest=0x7f95a5d4f010 <std::out_of_range::~out_of_range()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#9  0x00007f95a5d2f514 in std::__throw_out_of_range_fmt (__fmt=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/src/c++11/functexcept.cc:100
#10 0x00007f9593ee6559 in std::vector<QExplicitlySharedDataPointer<KTextEditor::Attribute>, std::allocator<QExplicitlySharedDataPointer<KTextEditor::Attribute> > >::_M_range_check (this=0x55f99a28ab20, __n=18446744073709551615) at /usr/include/c++/11.1.0/bits/stl_vector.h:1073
#11 0x00007f9593ee5969 in std::vector<QExplicitlySharedDataPointer<KTextEditor::Attribute>, std::allocator<QExplicitlySharedDataPointer<KTextEditor::Attribute> > >::at (
    this=0x55f99a28ab20, __n=18446744073709551615) at /usr/include/c++/11.1.0/bits/stl_vector.h:1112
#12 0x00007f9593ee5779 in SemanticTokensLegend::attributeForTokenType (this=0x55f99a28ab08, idx=-1)
    at /home/qewer33/Desktop/Git/kate/addons/lspclient/semantic_tokens_legend.h:59
#13 0x00007f9593ee4cc0 in SemanticHighlighter::highlight (this=0x55f999d7b998, view=0x55f999d72010, legend=0x55f99a28ab08)
    at /home/qewer33/Desktop/Git/kate/addons/lspclient/lspsemantichighlighting.cpp:212
#14 0x00007f9593ee4607 in SemanticHighlighter::processTokens (this=0x55f999d7b998, tokens=..., view=0x55f999d72010, legend=0x55f99a28ab08)
    at /home/qewer33/Desktop/Git/kate/addons/lspclient/lspsemantichighlighting.cpp:129
#15 0x00007f9593ee3cc7 in operator() (__closure=0x55f99969d120, st=...) at /home/qewer33/Desktop/Git/kate/addons/lspclient/lspsemantichighlighting.cpp:90
#16 0x00007f9593ee562b in std::__invoke_impl<void, SemanticHighlighter::doSemanticHighlighting_impl(KTextEditor::View*)::<lambda(const LSPSemanticTokensDelta&)>&, const LSPSemanticTokensDelta&>(std::__invoke_other, struct {...} &) (__f=...) at /usr/include/c++/11.1.0/bits/invoke.h:61
#17 0x00007f9593ee54e4 in std::__invoke_r<void, SemanticHighlighter::doSemanticHighlighting_impl(KTextEditor::View*)::<lambda(const LSPSemanticTokensDelta&)>&, const LSPSemanticTokensDelta&>(struct {...} &) (__fn=...) at /usr/include/c++/11.1.0/bits/invoke.h:111
#18 0x00007f9593ee52d9 in std::_Function_handler<void(const LSPSemanticTokensDelta&), SemanticHighlighter::doSemanticHighlighting_impl(KTextEditor::View*)::<lambda(const LSPSemanticTokensDelta&)> >::_M_invoke(const std::_Any_data &, const LSPSemanticTokensDelta &) (__functor=..., __args#0=...) at /usr/include/c++/11.1.0/bits/std_function.h:291
#19 0x00007f9593eb76ab in std::function<void (LSPSemanticTokensDelta const&)>::operator()(LSPSemanticTokensDelta const&) const (this=0x55f99a063f50, __args#0=...)
    at /usr/include/c++/11.1.0/bits/std_function.h:560
#20 0x00007f9593e9dd0e in operator() (__closure=0x55f99a063f40, m=...) at /home/qewer33/Desktop/Git/kate/addons/lspclient/lspclientserver.cpp:1603
#21 0x00007f9593ea2eb9 in std::__invoke_impl<void, make_handler<LSPSemanticTokensDelta>(ReplyHandler<LSPSemanticTokensDelta>&, const QObject*, utils::identity<std::function<LSPSemanticTokensDelta(const QJsonValue&)> >::type)::<lambda(const GenericReplyType&)>&, const QJsonValue&>(std::__invoke_other, struct {...} &) (__f=...)
    at /usr/include/c++/11.1.0/bits/invoke.h:61
#22 0x00007f9593ea1745 in std::__invoke_r<void, make_handler<LSPSemanticTokensDelta>(ReplyHandler<LSPSemanticTokensDelta>&, const QObject*, utils::identity<std::function<LSPSemanticTokensDelta(const QJsonValue&)> >::type)::<lambda(const GenericReplyType&)>&, const QJsonValue&>(struct {...} &) (__fn=...) at /usr/include/c++/11.1.0/bits/invoke.h:111
#23 0x00007f9593e9fad2 in std::_Function_handler<void(const QJsonValue&), make_handler<LSPSemanticTokensDelta>(ReplyHandler<LSPSemanticTokensDelta>&, const QObject*, utils::identity<std::function<LSPSemanticTokensDelta(const QJsonValue&)> >::type)::<lambda(const GenericReplyType&)> >::_M_invoke(const std::_Any_data &, const QJsonValue &) (
    __functor=..., __args#0=...) at /usr/include/c++/11.1.0/bits/std_function.h:291
#24 0x00007f9593eb022b in std::function<void (QJsonValue const&)>::operator()(QJsonValue const&) const (this=0x7ffe745eafa0, __args#0=...)
```
Comment 4 Waqar Ahmed 2021-12-28 10:23:09 UTC
Git commit 6193824fbb2fd0d4a3ae829ee258c9d3268829cf by Waqar Ahmed.
Committed on 28/12/2021 at 10:20.
Pushed by waqar into branch 'master'.

SemanticTokens: Use size_t as argument for type

Should fix the case of idx = -1 being rightfully out of bounds.

M  +2    -2    addons/lspclient/semantic_tokens_legend.h

https://invent.kde.org/utilities/kate/commit/6193824fbb2fd0d4a3ae829ee258c9d3268829cf
Comment 5 Waqar Ahmed 2021-12-28 10:24:45 UTC
Thanks once again.

Can you try with latest master and confirm if it fixes the crash?
Comment 6 qewer 2021-12-28 10:58:33 UTC
(In reply to Waqar Ahmed from comment #5)
> Thanks once again.
> 
> Can you try with latest master and confirm if it fixes the crash?

Yep, can confirm it doesn't crash anymore. Thanks for the quick fix!
Comment 7 Waqar Ahmed 2021-12-28 11:13:35 UTC
Thanks, will backport to 21.12 soon. 

Also, if the repo that you are working on is open source, then it would be good to fix this in the server itself (assuming the real bug is there). So if you want, you can report it here: https://github.com/typescript-language-server/typescript-language-server . That's the repo of the language server you are using. And since I was one of the persons behind the semantic tokens feature in that server, I will try to look into it from the server's side too.

The bug is basically that "type" in SemanticToken is -1, and hence leads to => array[-1] == out of bound == crash.
Comment 8 Waqar Ahmed 2021-12-28 12:47:24 UTC
Git commit 87aff4f47f079d32aa286f074cb0ecce8b1304a7 by Waqar Ahmed.
Committed on 28/12/2021 at 12:45.
Pushed by waqar into branch 'release/21.12'.

SemanticTokens: Use size_t as argument for type

Should fix the case of idx = -1 being rightfully out of bounds.
(cherry picked from commit 6193824fbb2fd0d4a3ae829ee258c9d3268829cf)

M  +2    -2    addons/lspclient/semantic_tokens_legend.h

https://invent.kde.org/utilities/kate/commit/87aff4f47f079d32aa286f074cb0ecce8b1304a7