commit 7a5099eec86b80c037aadd2dc5638049863d75ab
parent b218a528a703c95c7c2337160a72b4b17b6346ff
Author: Autumn! <autumnull@posteo.net>
Date: Sun, 7 May 2023 17:17:15 +0000
strio: return error instead of abort when fixed buffer exceeded
Signed-off-by: Autumn! <autumnull@posteo.net>
Diffstat:
3 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -1320,7 +1320,7 @@ strio() {
gen_srcs strio \
stream.ha \
ops.ha
- gen_ssa strio io strings slices encoding::utf8
+ gen_ssa strio errors io strings slices encoding::utf8
}
temp() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -1948,7 +1948,7 @@ stdlib_strio_any_srcs = \
$(STDLIB)/strio/stream.ha \
$(STDLIB)/strio/ops.ha
-$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_slices_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM))
+$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_errors_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_slices_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/strio
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nstrio \
@@ -4223,7 +4223,7 @@ testlib_strio_any_srcs = \
$(STDLIB)/strio/stream.ha \
$(STDLIB)/strio/ops.ha
-$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_slices_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM))
+$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_errors_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_slices_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/strio
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nstrio \
diff --git a/strio/stream.ha b/strio/stream.ha
@@ -1,6 +1,7 @@
// License: MPL-2.0
// (c) 2022 Alexey Yerin <yyp@disroot.org>
// (c) 2021 Drew DeVault <sir@cmpwn.com>
+use errors;
use io;
use slices;
use strings;
@@ -30,8 +31,8 @@ export fn reset(in: *stream) void = {
};
// Creates a write-only string stream using the provided buffer for storage.
-// The program aborts if writes would exceed the buffer's capacity. The stream
-// doesn't need to be closed.
+// The writes will return an error if they would exceed the buffer's capacity.
+// The stream doesn't need to be closed.
export fn fixed(in: []u8) stream = {
return stream {
stream = &fixed_vtable,
@@ -42,9 +43,7 @@ export fn fixed(in: []u8) stream = {
fn fixed_write(s: *io::stream, buf: const []u8) (size | io::error) = {
let s = s: *stream;
let cap = slices::cap(s.buf);
- if (cap == len(s.buf)) {
- abort("strio::fixed buffer exceeded");
- };
+ if (cap == len(s.buf)) return errors::overflow;
let n = if (cap - len(s.buf) < len(buf)) cap - len(s.buf) else len(buf);
static append(s.buf, buf[..n]...);
return n;