From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 838F43858D1E; Wed, 17 May 2023 14:20:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 838F43858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684333225; bh=qACEFxZEIsIfU8UcCb1qER2zyx04mhUSCLNE2to7c44=; h=From:To:Subject:Date:From; b=W/LA5/eK7cvROQIulA6imFJur3qKdYY8HA/3GcVBWkZA7AY+uDJ84g4FDZeIFFYGa Dk7CHn6eTGzhlLln2EaxjtWfBzs+01SmVyi4Izc2ItjeVMNaHGvTRfxcwC4tawkHCd P25mzJ/9kOH8dFx2l/qQUwKpFhnTSroRowrkvTTg= From: "mimomorin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/109891] New: Null pointer special handling in ostream's operator << for C-strings Date: Wed, 17 May 2023 14:20:25 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mimomorin at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109891 Bug ID: 109891 Summary: Null pointer special handling in ostream's operator << for C-strings Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: mimomorin at gmail dot com Target Milestone: --- This code #include int main() { std::cout << (char*)nullptr; } does not cause any bad things (like SEGV), because libstdc++'s operator<<(ostream, char const*) has special handling of null pointers:=20 template inline basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) { if (!__s) __out.setstate(ios_base::badbit); else __ostream_insert(...); return __out; } Passing a null pointer to this operator is a precondition violation, so the current implementation perfectly conforms to the C++ standard. But, why don= 't we remove this special handling? By doing so, we get - better interoperability with toolings (i.e. sanitizers can find the bug easily) - unnoticeable performace improvement and we lose - deterministic behaviors (of poor codes) on a particular stdlib I believe the first point makes more sense than the last point. It seems that old special handling `if (s =3D=3D NULL) s =3D "(null)";` (https://github.com/gcc-mirror/gcc/blob/6599da0/libio/iostream.cc#L638) was removed in GCC 3.0, but reintroduced (in the current form) in GCC 3.2 in response to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D6518 .=