Author: Tim Felgentreff <timfelgentr...@gmail.com>
Branch: 
Changeset: r148:0974cda2a4eb
Date: 2013-03-08 15:23 +0100
http://bitbucket.org/pypy/lang-smalltalk/changeset/0974cda2a4eb/

Log:    raise appropriate errors if calling FILE_WRITE prim with negative
        bounds

diff --git a/spyvm/primitives.py b/spyvm/primitives.py
--- a/spyvm/primitives.py
+++ b/spyvm/primitives.py
@@ -758,9 +758,9 @@
 def func(interp, s_frame, w_rcvr, fd):
     try:
         os.close(fd)
-        return w_rcvr
     except OSError:
         raise PrimitiveFailedError()
+    return w_rcvr
 
 @expose_primitive(FILE_OPEN, unwrap_spec=[object, str, object])
 def func(interp, s_frame, w_rcvr, filename, w_writeable_flag):
@@ -769,17 +769,21 @@
             filename,
             (os.O_RDWR | os.O_CREAT | os.O_TRUNC) if w_writeable_flag is 
interp.space.w_true else os.O_RDONLY
         )
-        return interp.space.wrap_int(fd)
     except OSError:
         raise PrimitiveFailedError()
+    return interp.space.wrap_int(fd)
 
 @expose_primitive(FILE_WRITE, unwrap_spec=[object, int, str, int, int])
 def func(interp, s_frame, w_rcvr, fd, src, start, count):
+    start = start - 1
+    end = start + count
+    if end < 0 or start < 0:
+        raise PrimitiveFailedError()
     try:
-        os.write(fd, src[start - 1:start + count - 1])
-        return w_rcvr
+        os.write(fd, src[start:end])
     except OSError:
         raise PrimitiveFailedError()
+    return w_rcvr
 
 @expose_primitive(DIRECTORY_DELIMITOR, unwrap_spec=[object])
 def func(interp, s_frame, _):
diff --git a/spyvm/test/test_primitives.py b/spyvm/test/test_primitives.py
--- a/spyvm/test/test_primitives.py
+++ b/spyvm/test/test_primitives.py
@@ -487,6 +487,18 @@
     finally:
         monkeypatch.undo()
 
+def test_file_write_errors(monkeypatch):
+    with py.test.raises(PrimitiveFailedError):
+        w_c = prim(
+            primitives.FILE_WRITE,
+            [1, space.wrap_int(42), space.wrap_string("hello"), 
space.wrap_int(-1), space.wrap_int(3)]
+        )
+    with py.test.raises(PrimitiveFailedError):
+        w_c = prim(
+            primitives.FILE_WRITE,
+            [1, space.wrap_int(42), space.wrap_string("hello"), 
space.wrap_int(2), space.wrap_int(-1)]
+        )
+
 def test_directory_delimitor():
     import os.path
     w_c = prim(primitives.DIRECTORY_DELIMITOR, [1])
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to