Skip to content

Commit 9dfbb9a

Browse files
[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.
1 parent 6b1db79 commit 9dfbb9a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

clang/tools/clang-format/git-clang-format

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ def main():
205205
"commits"
206206
),
207207
)
208+
p.add_argument(
209+
"-0",
210+
"--null",
211+
action="store_true",
212+
help="print the affected paths with null-terminated characters",
213+
)
208214
# We gather all the remaining positional arguments into 'args' since we need
209215
# to use some heuristics to determine whether or not <commit> was present.
210216
# However, to print pretty messages, we make use of metavar and help.
@@ -261,11 +267,11 @@ def main():
261267
"ignored by clang-format):"
262268
)
263269
for filename in ignored_files:
264-
print(" %s" % filename)
270+
print_filename(filename, opts.null)
265271
if changed_lines:
266272
print("Running clang-format on the following files:")
267273
for filename in changed_lines:
268-
print(" %s" % filename)
274+
print_filename(filename, opts.null)
269275

270276
if not changed_lines:
271277
if opts.verbose >= 0:
@@ -304,7 +310,7 @@ def main():
304310
if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1:
305311
print("changed files:")
306312
for filename in changed_files:
307-
print(" %s" % filename)
313+
print_filename(filename, opts.null)
308314

309315
return 1
310316

@@ -869,5 +875,12 @@ def convert_string(bytes_input):
869875
return str(bytes_input)
870876

871877

878+
def print_filename(filename, null=False):
879+
if null:
880+
print(filename + "\0", end="")
881+
else:
882+
print(" %s" % filename)
883+
884+
872885
if __name__ == "__main__":
873886
sys.exit(main())

0 commit comments

Comments
 (0)