-
-
Notifications
You must be signed in to change notification settings - Fork 36.8k
Discrepancy between stat.filemode's C and Python implementation #144050
Copy link
Copy link
Closed
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
Bug description:
We found a behavioral discrepancy between CPython and PyPy in
stat.filemodevia fuzz testing. Upon further inspection, we observed this discrepancy attributes to the differences between CPython's C and Python implementation.Environment
Observed Discrepancy
Incorrect file type detection in
stat.filemode(mode)Reproduction
Output
CPython (default):
?rwsrwsrwtCpython (Python impl.):
brwsrwsrwtRoot Cause
CPython's _stat.c determines the file type using
(mode & S_IFMT) == S_IFBLK.For 32767,
(mode & S_IFMT) == 0o70000does not match any known file type, so CPython correctly uses?as the file-type character.Its pure-Python implementation instead checks:
(mode & S_IFBLK) == S_IFBLK. This check incorrectly evaluatestruefor32767, causing Python implementation (which in turn affects PyPy) to misclassify the mode as a block device, yielding the wrong prefixb.CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs