-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Overrun of data causes unassigned variable to be provided to functions #60
Comments
you are not placing any variables in data. How is anything being printed in console other than this?
please provide input data too. |
It seems to work fine but you are defining the template argument differently in function. using auto inside template creates non type template parameter, but original the template was defined as type parameter. Instead make the Option variable auto as in the code below. I ran it on file with 01 on first and second value and it prints true for both the console and the pattern data #include <type/guid.pat>
struct Option<T> {
bool is_some;
T value;
} [[format("format_option")]];
fn format_option(auto option) {
std::print("{}", option.is_some);
if (option.is_some) {
return std::format("Some({})", option.value);
} else {
return "None";
}
};
Option<u8> option @ 0; PS: Don't call templates Generics. The documentation clearly uses the word template as the name for this feature. Generics is the name that C# uses instead of templates when they introduce the feature even though they use the keyword template in the feature itself. |
So i found what is the issue, my data was 64 bytes long but I was trying to parse 65 bytes total 😑. Looks like it silently breaks down, ending up with an empty argument in that function while the data inspector is fine and shows 0 as the last byte struct Pubkey {
u8 bytes[32];
};
struct A {
u8 a[32];
Option<Pubkey> b;
}; Regardless i'll use To reproduce: with open('test2.bin', 'wb+') as f:
f.write(bytearray([i for i in range(32)] * 2)) Then the struct in format_option doesn't seem to contain anything, changing the first field to So i tried with something more basic struct A {
u8 a[32];
u8 b[33] [[format("format_array")]];
};
fn format_array(auto array) {
std::print("{}", array[0]);
return "";
}; Array in format_array is empty when b overruns the data. Thanks for the reply, PS: Understood, i'll say template. |
It doesn't silently break. The pattern will read data until it reaches the end of the file and then stop. 0,1,2, ... ,30,31,0,1,2, ... ,30,31 in the first case you assign 32 bytes to a[32] so Option<Pubkey> option; sees this data 0,1,2, ... ,30,31 then bool is_some becomes false (0) and there are 31 bytes left. so T value; becomes a u8 bytes[32]; the first 31 bytes get values from file and the last one remains zero because thats the default value all of them have before assignment. The reason it appears not to read the 31 bytes is that bool is_some; is false so format function returns "None", but if you open the variable for value you'll see all the values from 1 to 31 are assigned correctly. Option<Pubkey> option; sees this data 31,0,1,2, ... ,30,31 now there are 33 bytes left and the first one is not zero so is_some now is true and the 32 bytes of the array get values from the file (the first one is still zero). That explains the whole situation. When you were using Option<auto> option the error of using the wrong type means that option is not loaded with the value it had before calling format function so its values are all zero including the value of is_some is false. |
in this case struct A {
u8 a[32];
u8 b[33] [[format("format_array")]];
};
fn format_array(auto array) {
std::print("{}", array[0]);
return "";
}; the problem is that your format function returns the empty string. what gets printed as value when you use a format function it the value of the string returned by the format function. using std::print inside the format function prints the data to the console which in this case is the first value of the array only which happens to be zero. it is not legal to pass arrays as arguments to functions. always create a struct around them and pass the struct instead. ps: you can change the title of the issue at any time. please change Generics to Templates |
If there is an error in imhex that your code is showing then the error has nothing to do with templates and formats. to see that then the error should disappear when templates are not used but if you use #include <std/io.pat>
struct Option {
bool is_some;
u8 value;
} [[format("format_option")]];
fn format_option(Option option) {
std::print("{}", option.is_some);
if (option.is_some) {
return std::format("Some({})", option.value);
} else {
return "None";
}
};
struct A {
u8 first;
Option second;
};
A data @ 0x00; nothing changes in the output so templates are not to blame. As for whether or not the extra byte that the pattern has should cause an overrun I am not sure if that is a bug. if you add a second byte to value u8 value[2]; you will see the error about going past the end. The pattern may allow a non existing byte of value zero at the end of the file for some reason. |
"If there is an error in imhex that your code is showing then the error has nothing to do with templates and formats. to see that then the error should disappear when templates are not used but if you use" "you will see the error about going past the end. The pattern may allow a non existing byte of value zero at the end of the file for some reason." yes it does that but in addition silently allows format to run on the "empty" argument. |
you need to change the title of the issue to reflect the true nature of the error you are seeing or close it and open a new one |
It is definitely does not silently allow it to run.
|
|
if that was true then making the file size 3 and leaving last value of zero should have no effect on the output but thats not true. the value for the bool in the console changes and the value printed for Option variable also goes from "None" to "Some(0)". it still is a vast improvement over the previous title. thank you for doing that. |
Could also be broken with all similar functions but logging what I observed:
I can see in pattern data that
is_some
is true but insideformat_option
it is false (in the console). Value is also not populated.The text was updated successfully, but these errors were encountered: