Fix etl::pearson::reset() not resetting the first-byte flag (#1487)

reset() cleared the hash array but left the `first` flag false, so a
calculator reused after reset() took the XOR update path on its next
add() instead of the initialisation path. This produced an incorrect
hash for any pearson object that was reset and reused.

Reset `first` to true in reset() so a reset calculator behaves like a
freshly constructed one.

Add test_pearson_reset regression test.

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Roland Reichwein 2026-07-07 16:41:50 +02:00 committed by GitHub
parent 032ec4cc80
commit 165674cb23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View File

@ -93,6 +93,7 @@ namespace etl
void reset() void reset()
{ {
hash.fill(0); hash.fill(0);
first = true;
} }
//************************************************************************* //*************************************************************************

View File

@ -144,6 +144,25 @@ namespace
CHECK(compare == hash); CHECK(compare == hash);
} }
//*************************************************************************
TEST(test_pearson_reset)
{
std::string data("123456789");
etl::pearson<HASH_SIZE> pearson_calculator;
// Use the calculator, then reset it and reuse it for the same data.
pearson_calculator.add(data.begin(), data.end());
pearson_calculator.reset();
pearson_calculator.add(data.begin(), data.end());
hash_t compare = Pearson_Compare(data);
hash_t hash = pearson_calculator.value();
// A reset calculator must produce the same hash as a fresh one.
CHECK(compare == hash);
}
//************************************************************************* //*************************************************************************
TEST(test_pearson_add_range_endian) TEST(test_pearson_add_range_endian)
{ {