WerWolv / ImHex

🔍 A Hex Editor for Reverse Engineers, Programmers and people who value their retinas when working at 3 AM.
https://imhex.werwolv.net
GNU General Public License v2.0
44.81k stars 1.95k forks source link

[Bug] Shift+Tab crashes on empty lines #1621

Open ZwipZwapZapony opened 7 months ago

ZwipZwapZapony commented 7 months ago

Operating System

Windows

What's the issue you encountered?

In the Pattern Editor, pressing Tab inserts (up to) four spaces to indent a line, and pressing Shift+Tab removes (up to) four spaces to un-indent a line. If the text cursor is on a completely empty line, pressing Shift+Tab crashes ImHex.

How can the issue be reproduced?

In the Pattern Editor, move the text cursor to an empty line, and press Shift+Tab.

ImHex Version

v1.33.2

ImHex Build Type

Installation type

Portable zip from the GitHub Releases page

Additional context?

No response

Calcoph commented 6 months ago

This doesn't happen on empty lines that are empty because the previous content was deleted. It only happens on newly created (unedited) empty lines

paxcut commented 6 months ago

I traced the problem to this code in TextEditor.cpp

        auto spacesToRemove = (cindex % mTabSize);
            if (spacesToRemove == 0) spacesToRemove = 4;
            for (int32_t j = 0; j < spacesToRemove; j++) {
                if ((line.begin() + cindex - 1)->mChar == ' ') {
                    line.erase(line.begin() + cindex - 1);
                    cindex -= 1;
                }
            }

spacesToRemove is 4 and cindex is 0. line is size 0 so (line.begin()-1)->mChar segvs. A simple fix is to add this line before for loop

           spacesToRemove = std::min(spacesToRemove, (int32_t) line.size());

I tested it with lines with 0,1,2,3 and 4 spaces and it deletes the spaces if there are any and does nothing if line is empty. This will be included in my next text editor enhancements pr.