D7252: dirs: reject consecutive slashes in paths

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Wed Nov 6 00:18:54 EST 2019


durin42 edited the summary of this revision.
durin42 updated this revision to Diff 17619.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7252?vs=17618&id=17619

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7252/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7252

AFFECTED FILES
  mercurial/cext/dirs.c
  mercurial/util.py
  tests/test-dirs.py

CHANGE DETAILS

diff --git a/tests/test-dirs.py b/tests/test-dirs.py
new file mode 100644
--- /dev/null
+++ b/tests/test-dirs.py
@@ -0,0 +1,27 @@
+from __future__ import absolute_import
+
+import unittest
+
+import silenttestrunner
+
+from mercurial import util
+
+
+class dirstests(unittest.TestCase):
+    def testdirs(self):
+        for case, want in [
+            (b'a/a/a', [b'a', b'a/a', b'']),
+            (b'alpha/beta/gamma', [b'', b'alpha', b'alpha/beta']),
+        ]:
+            d = util.dirs({})
+            d.addpath(case)
+            self.assertEqual(sorted(d), sorted(want))
+
+    def testinvalid(self):
+        with self.assertRaises(ValueError):
+            d = util.dirs({})
+            d.addpath(b'a//b')
+
+
+if __name__ == '__main__':
+    silenttestrunner.main(__name__)
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -3545,6 +3545,8 @@
 def finddirs(path):
     pos = path.rfind(b'/')
     while pos != -1:
+        if path[pos - 2 : pos - 1] == '/':
+            raise ValueError("found invalid consecutive slashes in path")
         yield path[:pos]
         pos = path.rfind(b'/', 0, pos)
     yield b''
diff --git a/mercurial/cext/dirs.c b/mercurial/cext/dirs.c
--- a/mercurial/cext/dirs.c
+++ b/mercurial/cext/dirs.c
@@ -66,6 +66,14 @@
 	while ((pos = _finddir(cpath, pos - 1)) != -1) {
 		PyObject *val;
 
+		/* Sniff for trailing slashes, a marker of an invalid input. */
+		if (pos > 0 && cpath[pos - 1] == '/') {
+			PyErr_SetString(
+			    PyExc_ValueError,
+			    "found invalid consecutive slashes in path");
+			goto bail;
+		}
+
 		key = PyBytes_FromStringAndSize(cpath, pos);
 		if (key == NULL)
 			goto bail;



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list