summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_os.py
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2018-03-05 22:26:28 (GMT)
committerGitHub <noreply@github.com>2018-03-05 22:26:28 (GMT)
commitbaa45079466eda1f5636a6d13f3a60c2c00fdcd3 (patch)
tree53900bd3a4e1936f47f8b8d056eeab817f2773e7 /Lib/test/test_os.py
parent6935a511670797a3aaebdf96aad3dcff66baa76e (diff)
downloadcpython-baa45079466eda1f5636a6d13f3a60c2c00fdcd3.zip
cpython-baa45079466eda1f5636a6d13f3a60c2c00fdcd3.tar.gz
cpython-baa45079466eda1f5636a6d13f3a60c2c00fdcd3.tar.bz2
[3.6] bpo-33001: Prevent buffer overrun in os.symlink (GH-5989) (GH-5990)
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r--Lib/test/test_os.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 26d544c..240b7c4 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -2170,6 +2170,41 @@ class Win32SymlinkTests(unittest.TestCase):
target = os.readlink(r'C:\Users\All Users')
self.assertTrue(os.path.samefile(target, r'C:\ProgramData'))
+ def test_buffer_overflow(self):
+ # Older versions would have a buffer overflow when detecting
+ # whether a link source was a directory. This test ensures we
+ # no longer crash, but does not otherwise validate the behavior
+ segment = 'X' * 27
+ path = os.path.join(*[segment] * 10)
+ test_cases = [
+ # overflow with absolute src
+ ('\\' + path, segment),
+ # overflow dest with relative src
+ (segment, path),
+ # overflow when joining src
+ (path[:180], path[:180]),
+ ]
+ for src, dest in test_cases:
+ try:
+ os.symlink(src, dest)
+ except FileNotFoundError:
+ pass
+ else:
+ try:
+ os.remove(dest)
+ except OSError:
+ pass
+ # Also test with bytes, since that is a separate code path.
+ try:
+ os.symlink(os.fsencode(src), os.fsencode(dest))
+ except FileNotFoundError:
+ pass
+ else:
+ try:
+ os.remove(dest)
+ except OSError:
+ pass
+
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32JunctionTests(unittest.TestCase):