Bug 58395 - Undefined behavior vs. exception
Summary: Undefined behavior vs. exception
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-11 17:49 UTC by frankhb1989
Modified: 2015-04-09 15:01 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description frankhb1989 2013-09-11 17:49:04 UTC
Several operations are undefined because they failed to meet required behavior(ISO C++11 [defns.required.behavior]) at runtime. Currently libstdc++ throw exceptions for them. For example, when object of type std::string being initialized using a null pointer value, an exception of type std::logic_error is thrown by basic_string<char>::_S_construct. That's nothing wrong for libstdc++ on conformance, because an implementation is free to do anything when the behavior is undefined. However, this behavior is somewhat confusing for users not so familiar with the standard. I suggest 2 options for future releases of libstdc++:
1. Make the type of exceptions specific(i.e. as a documented feature), distinguishable with other exceptions required to be thrown by the standard. It can be a type derived from std::logic_error, as an extension.
2. Make the implementation-defined result of what() specific and distinguishable, e.g. some prefix to indicate the exception is not mandated.
This is not only good for manually distinguishing UB, but also beneficial for tests on conformance/portability of user programs.
Comment 1 Jonathan Wakely 2015-04-09 15:01:47 UTC
(In reply to frankhb1989 from comment #0)
> is somewhat confusing for users not so familiar with the standard.

I'm not convinced by this.

Doing something like passing a null pointer to std::string is a mistake. Whether we throw an exception of one type or another type, or abort, or segfault, the user needs to fix their code. I don't see why adding a new type (along with its typeinfo and vtable) to the library will make it any easier.

I don't see why the user should care that our method of reporting their mistake is specific to our implementation, they still need to fix the mistake.

Good messages in what() are helpful, but we don't need a new type with a special prefix for that.