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
43.96k stars 1.92k forks source link

[Bug] Bug in pattern leaves ImHex in corrupted state, program reload required, no pattern reload (with corrected pattern) can fix this. #1710

Closed qknight closed 4 months ago

qknight commented 4 months ago

Operating System

Windows

What's the issue you encountered?

ImHex 1.24.3 Windows I edit this into imhex-pe.hexpat

imhex-pe.hexpat

    else if (std::string::starts_with(sectionTable[currentSectionIndex].name, ".idata")) {// Imports section
        ImportsSection importsSection;
+        for (u8 i = 0, i < 10, i = i + 1) { // sizeof(importsSection)
+            std::print("fooooooooooooo");
+        }
    }

Then I load the pattern using the main menu and I get this evaluation error:

error[P0003]: Unknown type
  --> <Source Code>:7:1709
7 | #include <std/mem.pat>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ^
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Type T has not been declared yet.

hint: If this type is being declared further down in the code, consider forward declaring it with 'using T;'.

This error doesn't make sense. However, more important, if I use an external editor to fix the issue back to the original code, ImHex won't recover from this error and I need to close ImHex.exe and reopen it.

There seems to be some stuck state.

How can the issue be reproduced?

Described in the issue. Basically add the lines and parse a exe.

ImHex Version

1.24.3

ImHex Build Type

Installation type

msi using chocolatey

Additional context?

No response

jumanji144 commented 4 months ago

Can you try on the newest imhex version?

qknight commented 4 months ago

I've created this in ImHex v.1.33.2:

struct ImportsStructure {
    if (parent.importsDirectoryTable[std::core::array_index()].lookupTableRVA > 0) {
        ImportsLookup lookupTable[while(std::mem::read_unsigned($, wordsize()) != 0)] @ parent.importsDirectoryTable[std::core::array_index()].lookupTableRVA - relativeVirtualDifference();
    }
    if (parent.importsDirectoryTable[std::core::array_index()].addressTableRVA > 0) {
        ImportsAddress addressTable[while(std::mem::read_unsigned($, wordsize()) != 0)] @ parent.importsDirectoryTable[std::core::array_index()].addressTableRVA - relativeVirtualDifference();
    }
+       for (u8 i = 0, i < 10, i = i + 1) { // sizeof(importsSection)
+           std::print("fooooooooooooo");
+       }
    if (parent.importsDirectoryTable[std::core::array_index()].dllNameRVA > 0) {
        char dllName[] @ parent.importsDirectoryTable[std::core::array_index()].dllNameRVA - relativeVirtualDifference() [[format("formatNullTerminatedString")]];
    }
} [[inline]];

And it produced this error:

E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:8
E: 447 |        for (u8 i = 0, i < 10,
E:               ^^^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:12
E: 447 |        for (u8 i = 0, i < 10, i =
E:                   ^
E: error: Expected ';' at end of statement, got Separator (,).
E:   -->   in <Source Code>:447:20
E: 447 |        for (u8 i = 0, i < 10, i = i + 1)
E:                           ^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:21
E: 447 |       for (u8 i = 0, i < 10, i = i + 1) {
E:                           ^
E: error: Type i has not been declared yet.
E:   -->   in <Source Code>:447:23
E: 447 |     for (u8 i = 0, i < 10, i = i + 1) { 
E:                           ^

E: If this type is being declared further down in the code, consider forward declaring it with 'using i;'.
E: error: Expected ';' at end of statement, got Operator (<).
E:   -->   in <Source Code>:447:23
E: 447 |     for (u8 i = 0, i < 10, i = i + 1) { 
E:                           ^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:25
E: 447 |   for (u8 i = 0, i < 10, i = i + 1) { 
E:                           ^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:27
E: 447 |  for (u8 i = 0, i < 10, i = i + 1) { 
E:                            ^^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:29
E: 447 |  for (u8 i = 0, i < 10, i = i + 1) { 
E:                              ^
E: error: Expected ';' at end of statement, got Separator ()).
E:   -->   in <Source Code>:447:39
E: 447 |  0, i < 10, i = i + 1) { 
E:                            ^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:40
E: 447 |  0, i < 10, i = i + 1) { 
E:                             ^
E: error: Invalid struct member definition.
E:   -->   in <Source Code>:447:42
E: 447 |  i < 10, i = i + 1) { 
E:                            ^
E: error: Expected ';' at end of statement, got Keyword (if).
E:   -->   in <Source Code>:449:9
E: 449 |         }
E:               ^
I: Evaluation took 0.0303644s

From which ImHex recovers once the lines are removed and the pehex is reevaluated. Btw: I only installed this older version because I installed using choco. Thanks for your time!