Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TagsEdit code improvements and crash fix #11346

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

libklein
Copy link
Contributor

Fix bug where pressing home on empty tag field crashes the program.

Bug is caused by https://github.com/libklein/keepassxc/blob/develop/src/gui/tag/TagsEdit.cpp#L340. The currentText().isEmpty() allows to invalidate the invariant if the current (and only) tag is empty. In this case, tags is resized to 0 (the only tag is erased) and the program crashes as currentText() accessed in https://github.com/libklein/keepassxc/blob/develop/src/gui/tag/TagsEdit.cpp#L504 fails as the tags list is now empty (https://github.com/libklein/keepassxc/blob/develop/src/gui/tag/TagsEdit.cpp#L387).

Fixes: #11344

  • ✅ Bug fix (non-breaking change that fixes an issue)

src/gui/tag/TagsEdit.cpp Outdated Show resolved Hide resolved
@libklein
Copy link
Contributor Author

I've attempted to do some basic refactoring of the class. I've moved responsibility for keeping track of the current item, as well as maintaining the invariant into a dedicated "TagManager" class. This class avoids the index-tracking issues by using a QLinkedList rather than a QList, that we way never invalidate any iterators.

I've further attempted to have a clear split in responsibility between Impl and TagsEdit. Impl contains the tag handling logic, TagsEdit listens to events and calls Impl functions accordingly. I suggest merging Impl and TagsEdit, the only reason for having Impl in the first place was, I assume, to have a stable header API (PImpl).

I've further fixed a few minor bugs in the implementation:

  • There was a corner case where an empty tag could be rendered when TagsEdit went out of focus with an empty tag selected
  • Scrollbars were not updated after removing a tag
  • Tags were not re-rendered (and re-layouted!) when ";" is pressed on an empty tag.

There is still a bug with rendering - the height hint of the textbox is wrong on first render - and a bit of cleanup needs to be done. I'd however like to get feedback on the general approach before polishing.

Another open question is the behavior of typing ";", which currently finishes editing the current tag. I think it's a bit unintuitive that this always finishes the current tag, regardless of cursor position. I'd expect the tag to be split if I type ";" in the middle of a tag.

What do you think? Any idea why the initial height hint may be off?

@droidmonkey
Copy link
Member

droidmonkey commented Nov 27, 2024

Thank you for this work! I do agree that typing a ; or , should cause the tags to be split instead of ending typing immediately. But what about pressing space, that also ends tag typing. That could also help with pasting tag strings.

@droidmonkey droidmonkey self-requested a review December 21, 2024 20:22
@droidmonkey droidmonkey force-pushed the fix/home-pressed-on-empty-tag-field branch 3 times, most recently from 4016df4 to 19b60bd Compare December 21, 2024 21:41
@droidmonkey
Copy link
Member

@libklein sorry just coming back to this. I made a bunch of code improvements to align with our standards. Also noticed that now the tags view when editing an entry gets cut off when first shown:

image

The view expands vertically when focused to not be cut off. Not sure where in the list of changes this happened.

@droidmonkey droidmonkey force-pushed the fix/home-pressed-on-empty-tag-field branch 2 times, most recently from c09cea8 to a2c1f7a Compare December 22, 2024 05:17
@michaelk83
Copy link

michaelk83 commented Dec 22, 2024

  1. Looks like too much vertical padding on the tag labels (inside the green rectangles).
  2. Could reduce at least the vertical padding of the outer text box (outside the green rectangles).
  3. The text box is shifted inward from its gray border, which makes the text box less tall, and shifts it sideways. (On closer look, the other textboxes have that border too, but the black area is not shifted.)

@droidmonkey droidmonkey changed the title Fix bug where pressing home on empty tag field crashes the program. TagsEdit code improvements and crash fix Dec 22, 2024
@droidmonkey
Copy link
Member

droidmonkey commented Dec 22, 2024

@michaelk83 those are not the causes. I put a bunch of time into diagnosing this and we were basically fighting Qt for sizing. I have a WIP commit added that improves the situation substantially and allows for external styling of the tags display instead of overriding everything internally.

image

image

Note: This still needs work because editing and adding tags is now broken. The fix is to separate actual painting of the widget from height estimation, the two are stuck together now and the logic is broken. FIXED

Fix crash when pressing home on empty tag field

Move completer to TagsEdit.
Move cursor blink status to TagsEdit.
Move paint implementation to impl.
Simplify calcRect and drawTag.

Hide editing_index and cursor position.
Fix bug where an empty tag was shown if the tag edit was unfocused.
Fix a bug where the scollbar was not updated when a tag was removed.

Hide remaining TextEdit internal fields.
Refactor to use QLinkedList.
Remove obsolete EmptyTagIterator.
Encapsulate tags and selected index in tags manager.
@droidmonkey droidmonkey force-pushed the fix/home-pressed-on-empty-tag-field branch 2 times, most recently from 9f0dee3 to efd4f08 Compare December 23, 2024 04:07
@droidmonkey
Copy link
Member

droidmonkey commented Dec 23, 2024

TODO:

  • Combine the TagsEdit::Impl directly into TagsEdit. There is no reason to have these separate and it creates ugly code
  • TagsManager should do all things non-gui related to the tags, including non-gui tag editing functions
  • Make a proper viewport height calculation function

@droidmonkey droidmonkey force-pushed the fix/home-pressed-on-empty-tag-field branch from efd4f08 to e7553ea Compare December 23, 2024 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeePassXC 2.7.9 crashes when pressing Home/End keyboard keys in the tag field on Linux
3 participants