This is an archive of the discontinued Mercurial Phabricator instance.

hg: raise Abort on invalid path
ClosedPublic

Authored by indygreg on Jan 30 2019, 8:23 PM.

Details

Summary

Currently, some os.path functions when opening repositories may
raise an uncaught TypeError or ValueError if the path is invalid.

Let's catch these exceptions and turn them into an Abort for
convenience.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

indygreg created this revision.Jan 30 2019, 8:23 PM
This revision was automatically updated to reflect the committed changes.
yuja added a subscriber: yuja.Jan 31 2019, 7:55 AM

+ try:
+ isfile = os.path.isfile(path)
+ # Python 2 raises TypeError, Python 3 ValueError.
+ except (TypeError, ValueError) as e:
+ raise error.Abort(_('invalid path %s: %s') % (
+ path, pycompat.bytestr(e)))

I don't know if the path is included in the exception message, but it's
probably safer to use stringutil.forcebytestr(e) to get around possible
UnicodeError.

Currently, some os.path functions when opening repositories may
raise an uncaught TypeError or ValueError if the path is invalid.

What kind of "invalid"? If the path doesn't exist? Or only if the path variable is of the wrong type (not bytes? not unicode? neither?)?

yuja added a comment.Feb 2 2019, 1:37 AM
> Currently, some os.path functions when opening repositories may
>  raise an uncaught TypeError or ValueError if the path is invalid.
What kind of "invalid"? If the path doesn't exist? Or only if the path variable is of the wrong type (not bytes? not unicode? neither?)?

It's NUL byte in path string.

spectral updated this revision to Diff 15304.May 30 2019, 5:16 PM

Oops. A typo caused me to update this diff, please ignore that.