[clang] [clang-format] Add null-terminated path option (#123921) (PR #123926)

Nikolaos Chatzikonstantinou via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 24 03:34:11 PST 2025


https://github.com/createyourpersonalaccount updated https://github.com/llvm/llvm-project/pull/123926

>From 9dfbb9a3cc7f6bc557bc1ccf25cc727a02c4274c Mon Sep 17 00:00:00 2001
From: Nikolaos Chatzikonstantinou <nchatz314 at gmail.com>
Date: Wed, 22 Jan 2025 05:43:02 -0500
Subject: [PATCH 1/2] [clang-format] Add null-terminated path option (#123921)

This makes the `git clang-format` tool compose nicely with other shell
utilities in case of maliciously (or innocently) crafted filenames.
---
 clang/tools/clang-format/git-clang-format | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index da271bbe6e3a07..04c49e8910d0ac 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -205,6 +205,12 @@ def main():
             "commits"
         ),
     )
+    p.add_argument(
+        "-0",
+        "--null",
+        action="store_true",
+        help="print the affected paths with null-terminated characters",
+    )
     # We gather all the remaining positional arguments into 'args' since we need
     # to use some heuristics to determine whether or not <commit> was present.
     # However, to print pretty messages, we make use of metavar and help.
@@ -261,11 +267,11 @@ def main():
                 "ignored by clang-format):"
             )
             for filename in ignored_files:
-                print("    %s" % filename)
+                print_filename(filename, opts.null)
         if changed_lines:
             print("Running clang-format on the following files:")
             for filename in changed_lines:
-                print("    %s" % filename)
+                print_filename(filename, opts.null)
 
     if not changed_lines:
         if opts.verbose >= 0:
@@ -304,7 +310,7 @@ def main():
     if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:
         print("changed files:")
         for filename in changed_files:
-            print("    %s" % filename)
+            print_filename(filename, opts.null)
 
     return 1
 
@@ -869,5 +875,12 @@ def convert_string(bytes_input):
         return str(bytes_input)
 
 
+def print_filename(filename, null=False):
+    if null:
+        print(filename + "\0", end="")
+    else:
+        print("    %s" % filename)
+
+
 if __name__ == "__main__":
     sys.exit(main())

>From 63424768ccd5cd2067448b7a86aeab16f01a0e78 Mon Sep 17 00:00:00 2001
From: Nikolaos Chatzikonstantinou <nchatz314 at gmail.com>
Date: Fri, 24 Jan 2025 06:32:47 -0500
Subject: [PATCH 2/2] do not print anything but list of files when null is
 enabled

---
 clang/tools/clang-format/git-clang-format | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 04c49e8910d0ac..9a26ef7a96a386 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -274,7 +274,7 @@ def main():
                 print_filename(filename, opts.null)
 
     if not changed_lines:
-        if opts.verbose >= 0:
+        if opts.verbose >= 0 and not opts.null:
             print("no modified files to format")
         return 0
 
@@ -295,7 +295,7 @@ def main():
         print("new tree: %s" % new_tree)
 
     if old_tree == new_tree:
-        if opts.verbose >= 0:
+        if opts.verbose >= 0 and not opts.null:
             print("clang-format did not modify any files")
         return 0
 
@@ -308,7 +308,8 @@ def main():
         old_tree, new_tree, force=opts.force, patch_mode=opts.patch
     )
     if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:
-        print("changed files:")
+        if not opts.null:
+            print("changed files:")
         for filename in changed_files:
             print_filename(filename, opts.null)
 



More information about the cfe-commits mailing list