Why exception from os.path.exists()?

Chris Angelico rosuav at gmail.com
Thu May 31 08:46:35 EDT 2018


On Thu, May 31, 2018 at 10:03 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>
> This surprising exception can even be a security issue:
>
>    >>> os.path.exists("\0")
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>      File "/usr/lib64/python3.6/genericpath.py", line 19, in exists
>        os.stat(path)
>    ValueError: embedded null byte
>
> Most other analogous reasons *don't* generate an exception, nor is that
> possibility mentioned in the specification:
>
>    https://docs.python.org/3/library/os.path.html?#os.path.exists
>
> Is the behavior a bug? Shouldn't it be:
>
>    >>> os.path.exists("\0")
>    False

A Unix path name cannot contain a null byte, so what you have is a
fundamentally invalid name. ValueError is perfectly acceptable.

ChrisA



More information about the Python-list mailing list