https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111729
Bug ID: 111729
Summary: Design considerations for operator<<(basic_ostream&,
const charT*)
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: llvm at rifkin dot dev
Target Milestone: ---
The following program violates a precondition specified in the standard and is
UB however it leads to some surprising behavior
#include <iostream>
int main() {
std::cout<<(const char*)nullptr;
std::cout<<"test";
}
There is no segfault, no abort, no non-zero exit status, and no exception.
Instead the program silently continues with the output stream's badbit set.
This can silently ruin the behavior of the rest of the program leaving the
programmer to pull their hair out over what on earth is happening.
Yes, it's UB and the programmer can't expect anything, however it's a very easy
mistake to make and the current behavior makes tracking down that mistake
exceedingly difficult.
I would like to humbly suggest that any of the following behaviors would be
better: Segfaulting, throwing an exception, aborting, an assertion failure in
debug, or printing "(null)" (which is the %s behavior). Libc++ and msvc's stl
both segfault here.