[clang] [clang][analyzer] scan-build: Ensure path prefixes exist (PR #71053)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 05:18:07 PDT 2023


https://github.com/amcn created https://github.com/llvm/llvm-project/pull/71053

scan-build's `UpdatePrefix` calculates the shared prefix of a set of files. Its use of strings and regular expressions to do so means that it can sometimes end up calculating a prefix which does not correspond to a real directory that exists on the filesystem. If such a prefix is calculated, when it is subsequently used in the calculation of the paths to files containing bugs in the toplevel html report, it can cause incorrect paths to be generated.

This patch fixes this by requiring that a calculated prefix exist in the filesystem for it to be considered valid.

I noticed this when using `scan-build` to analyse a project which contains several folders each starting with a common prefix. `UpdatePrefix` was including this prefix in its calculated prefix, which subsequently caused the paths in the report to lack it. I have created a project which can demonstrate the issue [here](https://github.com/amcn/scan-build-path-bug-example-repo).


>From d9f912f3e71e069417b9cefeca8e89b29f3c21fc Mon Sep 17 00:00:00 2001
From: Andrew McNulty <amcn102 at gmail.com>
Date: Thu, 2 Nov 2023 11:47:33 +0100
Subject: [PATCH] [clang][analyzer] scan-build: Ensure path prefixes exist

scan-build's UpdatePrefix calculates the lowest common ancestor
directory of a set of files. Its use of strings and regexes to
do so means that it can sometimes end up calculating a prefix
which does not correspond to a real directory that exists on
the filesystem.

This patch fixes this by requiring that a calculated prefix exist
in the filesystem for it to be considered valid.
---
 clang/tools/scan-build/bin/scan-build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/scan-build/bin/scan-build b/clang/tools/scan-build/bin/scan-build
index 04734d9cfa9af69..074989ab543af2c 100755
--- a/clang/tools/scan-build/bin/scan-build
+++ b/clang/tools/scan-build/bin/scan-build
@@ -282,7 +282,7 @@ sub UpdatePrefix {
     return;
   }
 
-  chop $Prefix while (!($x =~ /^\Q$Prefix/));
+  chop $Prefix while (!($x =~ /^\Q$Prefix/) || !(-e $Prefix));
 }
 
 sub GetPrefix {



More information about the cfe-commits mailing list