Skip to content

Commit 8f51593

Browse files
nfrapradorobherring
authored andcommitted
dt: dt-extract-compatibles: Don't follow symlinks when walking tree
The iglob function, which we use to find C source files in the kernel tree, always follows symbolic links. This can cause unintentional recursions whenever a symbolic link points to a parent directory. A common scenario is building the kernel with the output set to a directory inside the kernel tree, which will contain such a symlink. Instead of using the iglob function, use os.walk to traverse the directory tree, which by default doesn't follow symbolic links. fnmatch is then used to match the glob on the filename, as well as ignore hidden files (which were ignored by default with iglob). This approach runs just as fast as using iglob. Fixes: b6acf80 ("dt: Add a check for undocumented compatible strings in kernel") Reported-by: Aishwarya TCV <[email protected]> Closes: https://lore.kernel.org/all/[email protected] Signed-off-by: "Nícolas F. R. A. Prado" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent fe61262 commit 8f51593

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

scripts/dtc/dt-extract-compatibles

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
# SPDX-License-Identifier: GPL-2.0-only
33

4+
import fnmatch
45
import os
5-
import glob
66
import re
77
import argparse
88

@@ -81,10 +81,20 @@ def print_compat(filename, compatibles):
8181
else:
8282
print(*compatibles, sep='\n')
8383

84+
def glob_without_symlinks(root, glob):
85+
for path, dirs, files in os.walk(root):
86+
# Ignore hidden directories
87+
for d in dirs:
88+
if fnmatch.fnmatch(d, ".*"):
89+
dirs.remove(d)
90+
for f in files:
91+
if fnmatch.fnmatch(f, glob):
92+
yield os.path.join(path, f)
93+
8494
def files_to_parse(path_args):
8595
for f in path_args:
8696
if os.path.isdir(f):
87-
for filename in glob.iglob(f + "/**/*.c", recursive=True):
97+
for filename in glob_without_symlinks(f, "*.c"):
8898
yield filename
8999
else:
90100
yield f

0 commit comments

Comments
 (0)