From 9dfbb9a3cc7f6bc557bc1ccf25cc727a02c4274c Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Wed, 22 Jan 2025 05:43:02 -0500 Subject: [PATCH 1/8] [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 da271bbe6e3a0..04c49e8910d0a 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 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 Date: Fri, 24 Jan 2025 06:32:47 -0500 Subject: [PATCH 2/8] 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 04c49e8910d0a..9a26ef7a96a38 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) From 6b9460ab14abce7a3b2bda6851b920b64cfa9a67 Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 04:51:50 -0500 Subject: [PATCH 3/8] improve help string Co-authored-by: Owen Pan --- clang/tools/clang-format/git-clang-format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index 9a26ef7a96a38..90917533cf351 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -209,7 +209,7 @@ def main(): "-0", "--null", action="store_true", - help="print the affected paths with null-terminated characters", + help="end each printed filename with a null character", ) # We gather all the remaining positional arguments into 'args' since we need # to use some heuristics to determine whether or not was present. From 343fa15be5246d429c22f611371ff8b678ff3355 Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 04:59:41 -0500 Subject: [PATCH 4/8] improve print function to accept list of files Co-authored-by: Owen Pan --- clang/tools/clang-format/git-clang-format | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index 90917533cf351..c9de92a405a68 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -876,11 +876,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) +def print_filenames(filenames, print0=False): + for filename in filenames: + if print0: + print(filename, end="\0") + else: + print(" " * 4 + filename) if __name__ == "__main__": From e2ecb4bfc9dc8d43d7020d0d7476e86a6a29cadf Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 05:03:41 -0500 Subject: [PATCH 5/8] rename null option to print0 --- clang/tools/clang-format/git-clang-format | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index c9de92a405a68..308c7ea742fe5 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -206,8 +206,7 @@ def main(): ), ) p.add_argument( - "-0", - "--null", + "--print0", action="store_true", help="end each printed filename with a null character", ) @@ -267,14 +266,14 @@ def main(): "ignored by clang-format):" ) for filename in ignored_files: - print_filename(filename, opts.null) + print_filename(filename, opts.print0) if changed_lines: print("Running clang-format on the following files:") for filename in changed_lines: - print_filename(filename, opts.null) + print_filename(filename, opts.print0) if not changed_lines: - if opts.verbose >= 0 and not opts.null: + if opts.verbose >= 0 and not opts.print0: print("no modified files to format") return 0 @@ -295,7 +294,7 @@ def main(): print("new tree: %s" % new_tree) if old_tree == new_tree: - if opts.verbose >= 0 and not opts.null: + if opts.verbose >= 0 and not opts.print0: print("clang-format did not modify any files") return 0 @@ -308,10 +307,10 @@ 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: - if not opts.null: + if not opts.print0: print("changed files:") for filename in changed_files: - print_filename(filename, opts.null) + print_filename(filename, opts.print0) return 1 From 9bd8510d2e4739dbb0347694c84360820a5343b1 Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 05:06:15 -0500 Subject: [PATCH 6/8] order command-line arguments alphabetically --- clang/tools/clang-format/git-clang-format | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index 308c7ea742fe5..d515c64aeb20c 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -149,6 +149,15 @@ def main(): action="store_true", help="print a diff instead of applying the changes", ) + p.add_argument( + "--diff_from_common_commit", + action="store_true", + help=( + "diff from the last common commit for commits in " + "separate branches rather than the exact point of the " + "commits" + ), + ) p.add_argument( "--diffstat", action="store_true", @@ -171,6 +180,11 @@ def main(): p.add_argument( "-p", "--patch", action="store_true", help="select hunks interactively" ) + p.add_argument( + "--print0", + action="store_true", + help="end each printed filename with a null character", + ) p.add_argument( "-q", "--quiet", @@ -196,20 +210,6 @@ def main(): default=0, help="print extra information", ) - p.add_argument( - "--diff_from_common_commit", - action="store_true", - help=( - "diff from the last common commit for commits in " - "separate branches rather than the exact point of the " - "commits" - ), - ) - p.add_argument( - "--print0", - action="store_true", - help="end each printed filename with a null character", - ) # We gather all the remaining positional arguments into 'args' since we need # to use some heuristics to determine whether or not was present. # However, to print pretty messages, we make use of metavar and help. From ab12a7ef5c3942c0992d8ee3c1df42a29a114627 Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 05:08:15 -0500 Subject: [PATCH 7/8] use new print function to print file lists --- clang/tools/clang-format/git-clang-format | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index d515c64aeb20c..4af79103920e6 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -265,12 +265,10 @@ def main(): "Ignoring the following files (wrong extension, symlink, or " "ignored by clang-format):" ) - for filename in ignored_files: - print_filename(filename, opts.print0) + print_filenames(ignored_files, opts.print0) if changed_lines: print("Running clang-format on the following files:") - for filename in changed_lines: - print_filename(filename, opts.print0) + print_filenames(changed_lines, opts.print0) if not changed_lines: if opts.verbose >= 0 and not opts.print0: @@ -309,8 +307,7 @@ def main(): if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: if not opts.print0: print("changed files:") - for filename in changed_files: - print_filename(filename, opts.print0) + print_filenames(changed_files, opts.print0) return 1 From c47ce3ca8946f81f39757c82bfa5eb4cddc863a5 Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Sun, 26 Jan 2025 08:19:13 -0500 Subject: [PATCH 8/8] remove default value from print_filenames argument --- clang/tools/clang-format/git-clang-format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index 4af79103920e6..e14c6311b599f 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -872,7 +872,7 @@ def convert_string(bytes_input): return str(bytes_input) -def print_filenames(filenames, print0=False): +def print_filenames(filenames, print0): for filename in filenames: if print0: print(filename, end="\0")