From 1d957625bf03457132dacbf82931b4be1931df68 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Wed, 19 Feb 2025 03:09:38 +0900
Subject: [PATCH 1147/1160] pe-binary: fix array overrun

This is a kind of paranoia, as memeqzero() does not read anyting if
length is zero. But, strictly speaking C language does not allow such,
and Coverity warn about that.

Fixes CID#1561177.

(cherry picked from commit 6529ab0b066c93a6b8a8bf24b999d67e67a261f5)
(cherry picked from commit 73986494b65acd5eb68b889d0b8966f72f55bbb3)
(cherry picked from commit 9b7aaf3e02469676efcbcbdeab53dda40f090fe9)
---
 src/shared/pe-binary.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/shared/pe-binary.c b/src/shared/pe-binary.c
index 997e0e49a6..0554940da6 100644
--- a/src/shared/pe-binary.c
+++ b/src/shared/pe-binary.c
@@ -54,7 +54,7 @@ const IMAGE_SECTION_HEADER *pe_header_find_section(
 
         FOREACH_ARRAY(section, sections, le16toh(pe_header->pe.NumberOfSections))
                 if (memcmp(section->Name, name, n) == 0 &&
-                    memeqzero(section->Name + n, sizeof(section->Name) - n))
+                    (n == sizeof(sections[0].Name) || memeqzero(section->Name + n, sizeof(section->Name) - n)))
                         return section;
 
         return NULL;
-- 
2.33.0