[Bug] hex::core::add_virtual_file does not accept char[]
Operating System
Linux
What's the issue you encountered?
import hex.core;
import std.mem;
char array[while(!std::mem::eof())] @ 0x0;
hex::core::add_virtual_file("/", array);
E: [ Stack Trace ]
E: /usr/share/imhex/includes/hex/core.pat:47:59
E: 47 | dd_virtual_file(path, pattern);
E: ^
E: <Source Code>:6:39
E: 6 | hex::core::add_virtual_file("/", array);
E: ^
E: runtime error: Cannot cast value to type 'pattern'
How can the issue be reproduced?
Pass a char[] into it
ImHex Version
1.36.0
ImHex Build Type
- [ ] Nightly or built from sources
Installation type
AUR imhex-bin
Additional context?
Is char[] not a pattern? A u8[] works.
Hey You're right, this is a bit confusing. The thing is, char arrays create a string pattern when they're created so they can show up as strings in the pattern data view. When passing those strings to functions, they automatically decay to actual string values and lose their pattern information such as the location where they were in your data. I believe this is sadly pretty hard to fix without breaking many other patterns in the process. I can however document this a bit better in the documentation.
I believe this is sadly pretty hard to fix without breaking many other patterns in the process.
In that case, is it possible to come up with a patch solution, to pass char arrays as-is?
Or, is it possible to cast a char array to u8[] or something else?
The easiest solution is to just not use the raw string arrays directly but to wrap them in a struct first.
import hex.core;
import std.mem;
struct String {
char array[while(!std::mem::eof())];
};
String string @ 0x0;
hex::core::add_virtual_file("/", string); // This will work just fine
This workaround works, but I am working with char arrays with different extent (including while() and even sections), therefore it becomes ugly and sometimes unusable.
Simply we had better have a better solution rather than it.
How about this then?
import hex.core;
import std.mem;
struct Char {
char ch;
};
Char string[while(!std::mem::eof())]@0;
hex::core::add_virtual_file("/", string); // This will work just fine
@WerWolv ~Hey I just came up with an idea, I think that the second argument of the function should be auto, then it will accept a variety of data, just like hex::dec functions.~
Edit: hex::dec functions do not work with char[].
from the docs:
fn add_virtual_file(str path, auto pattern);
Yeah, the conversion happens when accessing the variable, not when passing it to the function. It might be possible to post-pone that decaying though, I'll need to see if I can do that without breaking existing patterns though. I'll look into it 👍
A workaround:
import hex.core;
import std.mem;
char array[while(!std::mem::eof())] @ 0x0;
u8 pattern[sizeof(array)] @ addressof(array) in 0 [[hidden]];
hex::core::add_virtual_file("/", pattern);
in 0 means placing it in the main section, without it Imhex will try to place it in heap section when in a function.