hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 0cf6503998baa3f68b9d680f2f4d448bf291f7f9
parent 4ef0cf5bba13a1437a7f9b959a09822ead7adf8c
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Wed, 18 Jan 2023 15:24:23 +0100

io::writeall: abort on error after partial write

Errors after partial writes may lead to subtle bugs. Which may cause
invalid data e.g. on non-blocking writes. Therefore abort on partial
writes and dissuade from using writeall, if the underlying write can
fail.

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Mio/util.ha | 13++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/io/util.ha b/io/util.ha @@ -23,10 +23,21 @@ export fn readall(in: handle, buf: []u8) (size | EOF | error) = { }; // Writes an entire buffer, perhaps issuing several [[write]] calls to do so. +// Aborts on errors after partial writes. Hence it should only be used if it is +// certain that the underlying writes will not fail after an initial write. export fn writeall(out: handle, buf: []u8) (size | error) = { let z: size = 0; for (z < len(buf)) { - z += write(out, buf[z..])?; + z += match (write(out, buf[z..])) { + case let s: size => + yield s; + case let e: error => + if (z == 0) { + return e; + }; + + abort("error after partial write"); + }; }; return z; };