commit e341e28225052955749cdd6876f13e6cb361de56
parent 97e708f0a853d3fffcf0608079f21fa635333409
Author: Byron Torres <b@torresjrjr.com>
Date: Mon, 16 May 2022 17:23:41 +0100
time::chrono: handle invalid utf8
Signed-off-by: Byron Torres <b@torresjrjr.com>
Diffstat:
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha
@@ -1,6 +1,7 @@
// License: MPL-2.0
// (c) 2021-2022 Byron Torres <b@torresjrjr.com>
use bufio;
+use encoding::utf8;
use fs;
use io;
use os;
@@ -53,7 +54,7 @@ let utc_leapsecs: [](i64, i64) = [];
fn read_utc_leapsecs_file(
h: io::handle,
leapsecs: *[](i64, i64),
-) (void | io::error) = {
+) (void | io::error | encoding::utf8::invalid) = {
for (true) {
const line = match (bufio::scanline(h)) {
case let err: io::error =>
@@ -61,7 +62,7 @@ fn read_utc_leapsecs_file(
case io::EOF =>
return;
case let line: []u8 =>
- yield strings::fromutf8(line);
+ yield strings::try_fromutf8(line)?;
};
defer free(line);
if (strings::hasprefix(line, '#')) {
diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha
@@ -2,6 +2,7 @@
// (c) 2021-2022 Byron Torres <b@torresjrjr.com>
use bufio;
use bytes;
+use encoding::utf8;
use endian;
use errors;
use fs;
@@ -198,7 +199,12 @@ fn parse_tzif(h: io::handle, tz: *timezone) (void | invalidtzif | io::error) = {
};
append(footerdata, buf1...);
};
- const posix_extend = strings::fromutf8(footerdata);
+ const posix_extend = match (strings::try_fromutf8(footerdata)) {
+ case let s: str =>
+ yield s;
+ case encoding::utf8::invalid =>
+ return invalidtzif;
+ };
// assemble structured data
@@ -240,7 +246,12 @@ fn parse_tzif(h: io::handle, tz: *timezone) (void | invalidtzif | io::error) = {
if (len(bytes) == 0) { // no NUL encountered
return invalidtzif;
};
- const abbr = strings::fromutf8(bytes);
+ const abbr = match (strings::try_fromutf8(bytes)) {
+ case let s: str =>
+ yield s;
+ case encoding::utf8::invalid =>
+ return invalidtzif;
+ };
zone.abbr = abbr;
append(zones, zone);