ImHex icon indicating copy to clipboard operation
ImHex copied to clipboard

[Feature] Some ways to soft-fail

Open ershov opened this issue 1 year ago • 2 comments

What feature would you like to see?

I would like to be able to handle the errors in a soft way.

For example, in a file there is a data length, I specify u8 data[data_size] but the file is shorter than that. In this case the whole pattern will fail despite it's the last part of the file. I would like to be able to produce all items that were parsed successfully by that point.

Or another example, I specify a struct but the file is not long enough.

Also, I wasn't able to find a way to query the current cursor position to calculate min(pos, size) for data[...].

How will this feature be useful to you and others?

Easier pattern creation.

Request Type

  • [ ] I can provide a PoC for this feature or am willing to work on it myself and submit a PR

Additional context?

No response

ershov avatar Aug 05 '23 11:08 ershov

Hey You can use try-catch statements to catch errors like this and gracefully exit. They should be described in the documentation.

To query the current cursor position you can use the $ operator. Simply read from it like from a normal variable to get the current address

WerWolv avatar Aug 05 '23 12:08 WerWolv

I tried try-catch. While it works, it's extremely bulky and not helping much in aborting on error while keeping everything up to the error point.

In order to make try-catch work for this purpose, one'd have to wrap every field of every struct in try-catch. That's not a viable solution.

Also, when I try to do

try {
    u8 data[... calculated size ...];
} catch {
    return;
}

it doesn't terminate the pattern execution as expected but rather tries to continue parsing the pattern which leads to overflow. The construct that does work is

try {
    u8 data[... calculated size ...];
} catch {
    u8 ERROR[std::mem::size() - $];
}

@WerWolv is there currently a way to do it better?

Maybe, some sort of a #pragma could do it better.

The mode that I would like to use the pattern is: Whenever an error happens, do the following:

  1. Abort the pattern execution.
  2. Keep everything that has been parsed up until the point.
  3. At the end of the pattern add a field with indication of the error (maybe, at each level).

ershov avatar Aug 07 '23 05:08 ershov

This issue is marked stale as it has been open for 11 months without activity. Please try the latest ImHex version. (Avaiable here: https://imhex.download/ for release and https://imhex.download/#nightly for development version) If the issue persists on the latest version, please make a comment on this issue again

Without response, this issue will be closed in one month.

github-actions[bot] avatar Aug 03 '24 16:08 github-actions[bot]