diff --git a/src/io.cpp b/src/io.cpp index 815e1fe1a..617180a25 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -373,6 +373,8 @@ void fd_output_stream_t::append(const wchar_t *s, size_t amt) { void null_output_stream_t::append(const wchar_t *, size_t) {} +void string_output_stream_t::append(const wchar_t *s, size_t amt) { contents_.append(s, amt); } + void buffered_output_stream_t::append(const wchar_t *s, size_t amt) { buffer_.append(s, s + amt); } void buffered_output_stream_t::append_with_separation(const wchar_t *s, size_t len, diff --git a/src/io.h b/src/io.h index af9753421..24713d81b 100644 --- a/src/io.h +++ b/src/io.h @@ -475,6 +475,19 @@ class fd_output_stream_t final : public output_stream_t { bool errored_{false}; }; +/// A simple output stream which buffers into a wcstring. +class string_output_stream_t final : public output_stream_t { + public: + string_output_stream_t() = default; + void append(const wchar_t *s, size_t amt) override; + + /// \return the wcstring containing the output. + const wcstring &contents() const { return contents_; } + + private: + wcstring contents_; +}; + /// An output stream for builtins which buffers into a separated buffer. class buffered_output_stream_t final : public output_stream_t { public: diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index b30a4de4c..09638b7a5 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -390,9 +390,8 @@ end_execution_reason_t parse_execution_context_t::run_function_statement( return result; } trace_if_enabled(*parser, L"function", arguments); - // no limit on the amount of output from builtin_function() - buffered_output_stream_t outs(0); - buffered_output_stream_t errs(0); + null_output_stream_t outs; + string_output_stream_t errs; io_streams_t streams(outs, errs); int err_code = 0; maybe_t err = builtin_function(*parser, streams, arguments, pstree, statement); @@ -402,7 +401,7 @@ end_execution_reason_t parse_execution_context_t::run_function_statement( parser->set_last_statuses(statuses_t::just(err_code)); } - wcstring errtext = errs.contents(); + const wcstring &errtext = errs.contents(); if (!errtext.empty()) { return this->report_error(err_code, header, L"%ls", errtext.c_str()); }