Bug 486951 - Crash when opening GIT repository with lightweight tags
Summary: Crash when opening GIT repository with lightweight tags
Status: RESOLVED FIXED
Alias: None
Product: kommit
Classification: Applications
Component: general (other bugs)
Version First Reported In: unspecified
Platform: Fedora RPMs Linux
: NOR normal
Target Milestone: ---
Assignee: Hamed Masafi
URL: https://bugzilla.redhat.com/show_bug....
Keywords:
Depends on:
Blocks:
 
Reported: 2024-05-13 11:24 UTC by Yaroslav Sidlovsky
Modified: 2024-05-29 13:52 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
GDB backtrace (4.64 KB, text/x-log)
2024-05-13 11:26 UTC, Yaroslav Sidlovsky
Details
kommit-fix-crash.patch (723 bytes, patch)
2024-05-13 11:31 UTC, Yaroslav Sidlovsky
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yaroslav Sidlovsky 2024-05-13 11:24:52 UTC
SUMMARY


STEPS TO REPRODUCE
1. Run `kommit ~/git/repo` in the console

OBSERVED RESULT
Crash

EXPECTED RESULT
No crash

SOFTWARE/OS VERSIONS
Operating System: Fedora Linux 40
KDE Plasma Version: 6.0.4
KDE Frameworks Version: 6.2.0
Qt Version: 6.7.0
Kernel Version: 6.8.9-300.fc40.x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 12 × 11th Gen Intel® Core™ i5-11600K @ 3.90GHz
Memory: 31.1 ГиБ of RAM
Graphics Processor: Mesa Intel® Graphics
Manufacturer: Gigabyte Technology Co., Ltd.
Product Name: B560M DS3H V2

ADDITIONAL INFORMATION
Comment 1 Yaroslav Sidlovsky 2024-05-13 11:26:37 UTC
Created attachment 169430 [details]
GDB backtrace
Comment 2 Yaroslav Sidlovsky 2024-05-13 11:29:44 UTC
After some debugging it turns out that crash happens because return value from function `git_tag_lookup` is ignored it is non zero.

I'm talking about this line of code: https://invent.kde.org/sdk/kommit/-/blob/5954f99cf5e5db96c13a73fd1752e4ad2211fa1e/src/libkommit/gitmanager.cpp?page=2#L1266

Proposed patch:
```
diff --git a/src/libkommit/gitmanager.cpp b/src/libkommit/gitmanager.cpp
index c1b4244..8f8ce9f 100644
--- a/src/libkommit/gitmanager.cpp
+++ b/src/libkommit/gitmanager.cpp
@@ -1241,7 +1241,14 @@ void Manager::forEachTags(std::function<void(Tag *)> cb)
         Q_UNUSED(name)
         auto w = reinterpret_cast<wrapper *>(payload);
         git_tag *t;
-        git_tag_lookup(&t, w->repo, oid_c);
+        int ret = git_tag_lookup(&t, w->repo, oid_c);
+        if (ret) {
+            const auto err = git_error_last();
+            if (err) {
+                qWarning().noquote().nospace() << "libgit2 error: " << err->message;
+            }
+            return 0;
+        }
 
         if (!t)
             return 0;
```

Macros PRINT_ERROR won't work here.
Comment 3 Yaroslav Sidlovsky 2024-05-13 11:31:41 UTC
Created attachment 169431 [details]
kommit-fix-crash.patch
Comment 4 Yaroslav Sidlovsky 2024-05-13 11:50:43 UTC
P.S. After applying the patch console shows this errors:
```
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
libgit2 error: the requested type does not match the type in the ODB
```
Comment 5 Yaroslav Sidlovsky 2024-05-13 12:32:42 UTC
Some light on situation that `git_tag_lookup` is not working for all tags:

git_tag_lookup only works for annotated tags: https://github.com/libgit2/libgit2/issues/5586 
> git_tag_lookup to emphasise that this will not work for all tags. that it only works for annotated tags and not for lightweight tags
Comment 6 Hamed Masafi 2024-05-15 14:10:49 UTC
Thank you for this in-depth review. For now I've applied your proposed. But in next commits I'll work more on this, of course, with the help of the clues you gave.
Comment 7 Yaroslav Sidlovsky 2024-05-15 14:25:51 UTC
If `git_tag_lookup` fails to find tag - you can use `git_commit_lookup` to get commit info.
And later create `Tag` class instance from the commit, something like that:
```
Tag::Tag(const char *refName, git_commit *commit)
{
    mName = QString(refName).remove(QRegularExpression("^refs/tags/"));
    mMessage = git_commit_message(commit);
    const auto author = git_commit_author(commit);
    mTagger.reset(new Signature{author});
}
```
Comment 8 Hamed Masafi 2024-05-29 09:19:49 UTC
Hi Yaroslav
In new commit light tags are detected and the information are taken from commit. Please check it out and let me know the result.
Comment 9 Yaroslav Sidlovsky 2024-05-29 10:58:21 UTC
Almost good but tag names is not displayed.
You can test it with this git repo:
```
1. mkdir /tmp/git && cd /tmp/git
2. git init
3. touch README.md && git add README.md && git commit -m 1
4. git tag some_tag

kommit /tmp/git
```
Comment 10 Hamed Masafi 2024-05-29 11:43:26 UTC
Yes, because it doesn't has name, isn't logical?
Comment 11 Yaroslav Sidlovsky 2024-05-29 11:57:11 UTC
But it has name and displayed for example with `git log` or `tig`.
Comment 12 Yaroslav Sidlovsky 2024-05-29 12:03:48 UTC
Also in my example it's this is taken into account and name is taken from ref name `refs/tags/<tag>`:
```
mName = QString(refName).remove(QRegularExpression("^refs/tags/"));
```
Comment 13 Hamed Masafi 2024-05-29 13:42:30 UTC
Ok; you're right. please check out new commit
Comment 14 Yaroslav Sidlovsky 2024-05-29 13:52:41 UTC
Looks good. Thanks!