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

[Bug] hex::core::add_virtual_file does not accept char[] #2032

Open
1 task
rsp4jack opened this issue Dec 27, 2024 · 8 comments
Open
1 task

[Bug] hex::core::add_virtual_file does not accept char[] #2032

rsp4jack opened this issue Dec 27, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@rsp4jack
Copy link

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.

@rsp4jack rsp4jack added the bug Something isn't working label Dec 27, 2024
@WerWolv
Copy link
Owner

WerWolv commented Dec 27, 2024

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.

@rsp4jack
Copy link
Author

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?

@WerWolv
Copy link
Owner

WerWolv commented Dec 31, 2024

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

@rsp4jack
Copy link
Author

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.

@paxcut
Copy link
Contributor

paxcut commented Dec 31, 2024

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

@rsp4jack
Copy link
Author

rsp4jack commented Dec 31, 2024

@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[].

@paxcut
Copy link
Contributor

paxcut commented Dec 31, 2024

from the docs:

fn add_virtual_file(str path, auto pattern);

@WerWolv
Copy link
Owner

WerWolv commented Dec 31, 2024

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants