[clang] [hmaptool] Fix for empty prefixes and suffixes (PR #102571)

Shoaib Meenai via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 8 22:31:33 PDT 2024


https://github.com/smeenai created https://github.com/llvm/llvm-project/pull/102571

The previous logic could fail in some edge cases.


>From 28297fc4e0b3b21990f1d614115b8eb946989479 Mon Sep 17 00:00:00 2001
From: Shoaib Meenai <smeenai at fb.com>
Date: Wed, 13 Dec 2023 18:59:25 -0800
Subject: [PATCH] [hmaptool] Fix for empty prefixes and suffixes

The previous logic could fail in some edge cases.
---
 clang/utils/hmaptool/hmaptool | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/utils/hmaptool/hmaptool b/clang/utils/hmaptool/hmaptool
index d7754632b162bf..aa400e3dd64e93 100755
--- a/clang/utils/hmaptool/hmaptool
+++ b/clang/utils/hmaptool/hmaptool
@@ -192,8 +192,11 @@ def action_write(name, args):
 
         key_idx = len(strtable)
         strtable += key + '\0'
-        prefix = os.path.dirname(value) + '/'
-        suffix = os.path.basename(value)
+        prefix, suffix = os.path.split(value)
+        # This guarantees that prefix + suffix == value in all cases, including when
+        # prefix is empty or contains a trailing slash or suffix is empty (hence the use
+        # of `len(value) - len(suffix)` instead of just `-len(suffix)`.
+        prefix += value[len(prefix) : len(value) - len(suffix)]
         prefix_idx = len(strtable)
         strtable += prefix + '\0'
         suffix_idx = len(strtable)



More information about the cfe-commits mailing list