From d5dc7fdda0a216d6e6ef4b23b659ea9ea2f9a322 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sat, 7 Oct 2023 05:39:30 +0200 Subject: [PATCH] Resolve #976 --- src/Abstract/Section.cpp | 2 +- tests/abstract/test_abstract.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Abstract/Section.cpp b/src/Abstract/Section.cpp index e1c77d3e1f..d455568996 100644 --- a/src/Abstract/Section.cpp +++ b/src/Abstract/Section.cpp @@ -179,7 +179,7 @@ std::vector Section::search_all(const std::string& v) const { double Section::entropy() const { std::array frequencies = { {0} }; span content = this->content(); - if (content.empty()) { + if (content.empty() || content.size() == 1) { return 0.; } for (uint8_t x : content) { diff --git a/tests/abstract/test_abstract.py b/tests/abstract/test_abstract.py index 897ba62533..bb86cbc44e 100644 --- a/tests/abstract/test_abstract.py +++ b/tests/abstract/test_abstract.py @@ -93,3 +93,15 @@ def test_function(): assert binary.get_function_address("foo") == lief.lief_errors.not_found assert binary.get_function_address("add") == 0x6a0 + +def test_entropy(): + """ + from issue #976 by @PaulDance + """ + weird_section_0 = lief.MachO.Section("weird_section_0", []).entropy + weird_section_1 = lief.MachO.Section("weird_section_1", [1]).entropy + assert str(weird_section_0) == "0.0" + assert str(weird_section_1) == "0.0" + + assert weird_section_0 >= 0 + assert weird_section_1 >= 0