Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iostreams eat exceptions by default, this can lead to incorrect/truncated result #581

Closed
crusader-mike opened this issue Oct 12, 2017 · 4 comments

Comments

@crusader-mike
Copy link

crusader-mike commented Oct 12, 2017

In code snippet below if buffer's overflow() throws an exception (e.g. std::bad_alloc) -- it is going to be silently eaten by output and execution will continue.

// Formats a value.
template <typename Char, typename ArgFormatter_, typename T>
void format_arg(BasicFormatter<Char, ArgFormatter_> &f,
                const Char *&format_str, const T &value) {
  internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;

  internal::FormatBuf<Char> format_buf(buffer);
  std::basic_ostream<Char> output(&format_buf);
  output << value;

  BasicStringRef<Char> str(&buffer[0], buffer.size());
  typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
  format_str = f.format(format_str, MakeArg(str));
}
vitaut added a commit that referenced this issue Oct 14, 2017
@vitaut
Copy link
Contributor

vitaut commented Oct 14, 2017

Good catch, thanks! Fixed in 2c077dd.

@vitaut vitaut closed this as completed Oct 14, 2017
@crusader-mike
Copy link
Author

an interesting thought -- what if T's op<< explicitly sets bad/fail bit instead of throwing an exception?

@vitaut
Copy link
Contributor

vitaut commented Oct 14, 2017

what if T's op<< explicitly sets bad/fail bit instead of throwing an exception?

¯_(ツ)_/¯. I guess it could be translated into an exception.

@crusader-mike
Copy link
Author

Turns out in this case ios_base::failure will be thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants