Skip to content

Commit 338c474

Browse files
committed
dentry: use escape_ascii_string() to decode names
Linux only requires that filenames cannot contain the byte '/' or the NUL byte. They do not have any specific encoding and aren't even guaranteed to be text. Thus, using bytes.decode() is not reliable. Use escape_ascii_string() when handling dentry names and paths. Signed-off-by: Stephen Brennan <[email protected]>
1 parent 7722e84 commit 338c474

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

drgn_tools/dentry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import drgn
1313
from drgn import Object
1414
from drgn import Program
15+
from drgn.helpers.common.format import escape_ascii_string
1516
from drgn.helpers.linux import d_path
1617
from drgn.helpers.linux.fs import path_lookup
1718
from drgn.helpers.linux.list import hlist_for_each_entry
@@ -239,15 +240,15 @@ def print_dentry_table(
239240
d.d_inode.value_(),
240241
int(d_count(d)),
241242
file_type,
242-
d_path(d).decode(),
243+
escape_ascii_string(d_path(d)),
243244
)
244245
else:
245246
table.row(
246247
d.value_(),
247248
d.d_sb.value_(),
248249
d.d_inode.value_(),
249250
file_type,
250-
d_path(d).decode(),
251+
escape_ascii_string(d_path(d)),
251252
)
252253

253254

@@ -374,7 +375,7 @@ def ls(
374375
print(f"{directory}:")
375376
pos = neg = 0
376377
for i, dentry in enumerate(dentries):
377-
name = dentry.d_name.name.string_().decode()
378+
name = escape_ascii_string(dentry.d_name.name.string_())
378379
if dentry_is_negative(dentry):
379380
neg += 1
380381
dentry_type = "NEG"
@@ -474,5 +475,5 @@ def run(self, prog: Program, args: argparse.Namespace) -> None:
474475
else:
475476
# Emulate oled dentrycache
476477
for i, dentry in enumerate(dentries):
477-
path = d_path(dentry).decode()
478+
path = escape_ascii_string(d_path(dentry))
478479
print(f"{i:05d} {path}")

0 commit comments

Comments
 (0)