WerWolv / WerWolv/PatternLanguage
`std::math::accumulate` bug with size>1
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 275
- Forks
- 75
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 10
Description
Using imhex version is 1.33.2 I tried to use std::math::accumulate to verify the entire file u8 sum equals the value required b a format I'm investigating. However I found a bug that seems already fixed in nightlystd::math::accumulate returns zero if the end points "just after the file end".
I can sum "all file bytes except last", but can't sum "all file bytes".
I looked at source code near https://github.com/WerWolv/PatternLanguage/blob/ImHex-v1.33.2/lib/source/pl/lib/std/math.cpp#L178 but failed to find the reason.
However, looking at source code I find that it has another bug: the end adress itself is divided by size, instead of dividing the adress difference. So summing from non-zero adress of non-single-byte elements fails.
Here are sample input and output as text on a simple file consisting of 16 identical bytes 0x01
#include <std/math.pat>
#include <std/io.pat>
std::print("0x{:X}", std::math::accumulate(0, 0x1, 1)); //sum single byte
std::print("0x{:X}", std::math::accumulate(0, 0x2, 1)); //sum a pair of bytes
std::print("0x{:X}", std::math::accumulate(0, 0xE, 1)); //sum all bytes except last two
std::print("0x{:X}", std::math::accumulate(0, 0xF, 1)); //sum all bytes except last
std::print("0x{:X} WRONG", std::math::accumulate(0, 0x10, 1)); //sum all bytes - FAILS
std::print("0x{:X}", std::math::accumulate(0, 0x11, 1)); //out-of-file acess, should fail
std::print("0x{:X}", std::math::accumulate(0, 0xE, 2)); //sum 7 u16s from start
std::print("0x{:X} WRONG", std::math::accumulate(2, 0xE, 2)); //sum 6 u16s, skipping one from start - WRONG result
I: 0x1
I: 0x2
I: 0xE
I: 0xF
I: 0x0 WRONG
I: 0x0
I: 0x707
I: 0x505 WRONG
I: Pattern exited with code: 0
I: Evaluation took 0.0106068s
I dont know what causes the bug with handling the last file byte, but the "non-zero start and >1 size" seems to be fixable by something like replacing
u128 endAddr = end / size; by u128 endAddr = start + ((end - start) / size);
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/source/pl/lib/std/math.cpp near line 178 and reproduce the issue with the provided Pattern Language sample on a 16-byte file of 0x01 values. Check the reported end-of-file and non-zero-start cases for element sizes greater than one. Done means the sample produces the expected sums for valid ranges and handles the out-of-file range consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100