Subject: [Boost-bugs] [Boost C++ Libraries] #11150: string_ref::to_string is necessarily UB for a default-constructed string_ref, despite stating no preconditions
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-03-28 18:22:43
#11150: string_ref::to_string is necessarily UB for a default-constructed
string_ref, despite stating no preconditions
-------------------------------+---------------------------
Reporter: Tomalak Geret'kal | Owner: no-maintainer
Type: Bugs | Status: new
Milestone: To Be Determined | Component: utility
Version: Boost 1.57.0 | Severity: Problem
Keywords: |
-------------------------------+---------------------------
Consider:
{{{
boost::string_ref foo;
auto bar = foo.to_string();
}}}
As currently specified (`to_string` is not documented but in the code it
seems to share the semantics of explicit string conversion, which is
defined by the reference material, N3442), this invokes
`std::string::string(nullptr, 0)`, which has UB because one of the
preconditions of that `std::string` constructor is that the pointer not be
`nullptr` or `NULL`... and default-constructing `boost::string_ref` indeed
sets the relevant pointer to `NULL`.
This has been fixed in newer versions of the proposal, e.g. by N3762
(seemingly accidentally):
> Returns: `basic_string<charT, traits, Allocator>(str.begin(), str.end(),
a)`
''Boost.StringRef'' should be updated to make the same change, otherwise
invoking `.to_string()` on a default-constructed `boost::to_string` has
undefined behaviour, despite there being no preconditions specified on
`.to_string()`.
Alternatively it could be documented that `.to_string()`'s precondition is
that the `string_ref` actually refers to a string.
(Thanks to Mgetz and Luc Danton on Stack Overflow for help identifying
this bug.)
(I tried to link to references but Trac thought I was a spammer and the
Captcha did not show up.)
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/11150> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:18 UTC