From 10b6ffd9a5409dc578dfaa58e0d137d3b6c74657 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 Apr 2016 06:43:11 -0700 Subject: Socket mode strings defaulted and checked. * stream.c (do_parse_mode): New static function. (parse_mode): Logic moved into do_parse_mode, leaving this function as a wrapper for do_parse_mode. Check added for malformed mode, so functions which call parse_mode now have the check. Added defaulting for mode_str argument, inside do_parse_mode. (normalize_mode): Call do_parse_mode instead of parse_mode. Don't default mode_str argument, since do_parse_mode does that. --- stream.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/stream.c b/stream.c index e07defb4..31cff28d 100644 --- a/stream.c +++ b/stream.c @@ -1155,10 +1155,10 @@ static struct strm_ops pipe_ops = stdio_clear_error, stdio_get_fd); -struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl) +static struct stdio_mode do_parse_mode(val mode_str, struct stdio_mode m_dfl) { struct stdio_mode m = stdio_mode_init_blank; - const wchar_t *ms = c_str(mode_str); + const wchar_t *ms = c_str(default_arg(mode_str, lit(""))); switch (*ms) { case 'r': @@ -1228,6 +1228,14 @@ struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl) return m; } +struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl) +{ + struct stdio_mode m = do_parse_mode(mode_str, m_dfl); + if (m.malformed) + uw_throwf(file_error_s, lit("invalid mode string ~a"), mode_str, nao); + return m; +} + static val format_mode(const struct stdio_mode m) { wchar_t buf[8], *ptr = buf; @@ -1256,11 +1264,9 @@ static val format_mode(const struct stdio_mode m) return string(buf); } -val normalize_mode(struct stdio_mode *m, val mode_str_in, struct stdio_mode m_dfl) +val normalize_mode(struct stdio_mode *m, val mode_str, struct stdio_mode m_dfl) { - val mode_str = default_arg(mode_str_in, lit("")); - - *m = parse_mode(mode_str, m_dfl); + *m = do_parse_mode(mode_str, m_dfl); if (m->malformed) uw_throwf(file_error_s, lit("invalid file open mode ~a"), mode_str, nao); -- cgit v1.2.3