From 165674cb23dda1fd4f7e5a1182f49554a796bfe8 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 7 Jul 2026 16:41:50 +0200 Subject: [PATCH] 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 --- include/etl/pearson.h | 1 + test/test_pearson.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/etl/pearson.h b/include/etl/pearson.h index fffae1d8..2b1d0ab2 100644 --- a/include/etl/pearson.h +++ b/include/etl/pearson.h @@ -93,6 +93,7 @@ namespace etl void reset() { hash.fill(0); + first = true; } //************************************************************************* diff --git a/test/test_pearson.cpp b/test/test_pearson.cpp index 4b5e5609..aa3eea30 100644 --- a/test/test_pearson.cpp +++ b/test/test_pearson.cpp @@ -144,6 +144,25 @@ namespace CHECK(compare == hash); } + //************************************************************************* + TEST(test_pearson_reset) + { + std::string data("123456789"); + + etl::pearson 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) {