about summary refs log tree commit diff stats
path: root/subx/062write-stream.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-05-11 22:44:49 -0700
committerKartik Agaram <vc@akkartik.com>2019-05-11 22:44:49 -0700
commite59a74abddaf0b4f9a39432da21c6a98eae46df6 (patch)
treeb21109b5889dcdb9011e5a8559e7fb8e0ba00c4c /subx/062write-stream.subx
parent6362c51d11ef27335875469e0ff06347357f46b1 (diff)
downloadmu-e59a74abddaf0b4f9a39432da21c6a98eae46df6.tar.gz
5156 - error-checking on writes to file
Pretty blunt for now; just abort the entire program on any failure to write.

I'm encountering it because I'm somehow treating a stream address as a
file descriptor. Maybe mmap is returning addresses below 0x08000000?
Diffstat (limited to 'subx/062write-stream.subx')
-rw-r--r--subx/062write-stream.subx18
1 files changed, 18 insertions, 0 deletions
diff --git a/subx/062write-stream.subx b/subx/062write-stream.subx
index eb2df0c5..83268422 100644
--- a/subx/062write-stream.subx
+++ b/subx/062write-stream.subx
@@ -102,6 +102,9 @@ _write-stream:  # fd : int, s : (address stream) -> <void>
     # . . syscall
     b8/copy-to-EAX  4/imm32/write
     cd/syscall  0x80/imm8
+    # if (EAX < 0) abort
+    3d/compare-EAX-with  0/imm32
+    0f 8c/jump-if-lesser  $_write-stream:abort/disp32
     # s->read += EAX
     01/add                          1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # add EAX to *(ESI+4)
     # . restore registers
@@ -116,6 +119,21 @@ _write-stream:  # fd : int, s : (address stream) -> <void>
     5d/pop-to-EBP
     c3/return
 
+$_write-stream:abort:
+    # . _write(2/stderr, error)
+    # . . push args
+    68/push  "_write-stream: failed to write to file"/imm32
+    68/push  2/imm32/stderr
+    # . . call
+    e8/call  _write/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . syscall(exit, 1)
+    bb/copy-to-EBX  1/imm32
+    b8/copy-to-EAX  1/imm32/exit
+    cd/syscall  0x80/imm8
+    # never gets here
+
 test-write-stream-single:
     # setup
     # . clear-stream(_test-stream)