diff options
| author | Louis Dionne <ldionne@apple.com> | 2019-02-12 16:06:02 +0000 |
|---|---|---|
| committer | Louis Dionne <ldionne@apple.com> | 2019-02-12 16:06:02 +0000 |
| commit | 7232a84e686a0d1bf834a845e4e59c5594ae8957 (patch) | |
| tree | 195b6c40e1303e25ea40e4e2bc4829a081d7231a /libcxx/src | |
| parent | d694160e665eb3cefc93a07af8232aec0b7d2410 (diff) | |
| download | bcm5719-llvm-7232a84e686a0d1bf834a845e4e59c5594ae8957.tar.gz bcm5719-llvm-7232a84e686a0d1bf834a845e4e59c5594ae8957.zip | |
[libc++] Avoid UB in the no-exceptions mode in a few places
Summary:
A few places in the library seem to behave unexpectedly when the library
is compiled or used with exceptions disabled. For example, not throwing
an exception when a pointer is NULL can lead us to dereference the pointer
later on, which is UB. This patch fixes such occurences.
It's hard to tell whether there are other places where the no-exceptions
mode misbehaves like this, because the replacement for throwing an
exception does not always seem to be abort()ing, but at least this
patch will improve the situation somewhat.
See http://lists.llvm.org/pipermail/libcxx-dev/2019-January/000172.html
Reviewers: mclow.lists, EricWF
Subscribers: christof, jkorous, dexonsmith, libcxx-commits
Differential Revision: https://reviews.llvm.org/D57761
llvm-svn: 353850
Diffstat (limited to 'libcxx/src')
| -rw-r--r-- | libcxx/src/hash.cpp | 12 | ||||
| -rw-r--r-- | libcxx/src/ios.cpp | 21 | ||||
| -rw-r--r-- | libcxx/src/locale.cpp | 16 |
3 files changed, 11 insertions, 38 deletions
diff --git a/libcxx/src/hash.cpp b/libcxx/src/hash.cpp index 1631b91acb1..89bb736c86c 100644 --- a/libcxx/src/hash.cpp +++ b/libcxx/src/hash.cpp @@ -153,12 +153,8 @@ inline _LIBCPP_INLINE_VISIBILITY typename enable_if<_Sz == 4, void>::type __check_for_overflow(size_t N) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (N > 0xFFFFFFFB) - throw overflow_error("__next_prime overflow"); -#else - (void)N; -#endif + __throw_overflow_error("__next_prime overflow"); } template <size_t _Sz = sizeof(size_t)> @@ -166,12 +162,8 @@ inline _LIBCPP_INLINE_VISIBILITY typename enable_if<_Sz == 8, void>::type __check_for_overflow(size_t N) { -#ifndef _LIBCPP_NO_EXCEPTIONS if (N > 0xFFFFFFFFFFFFFFC5ull) - throw overflow_error("__next_prime overflow"); -#else - (void)N; -#endif + __throw_overflow_error("__next_prime overflow"); } size_t diff --git a/libcxx/src/ios.cpp b/libcxx/src/ios.cpp index fdff2e8fe3f..2dc84be8287 100644 --- a/libcxx/src/ios.cpp +++ b/libcxx/src/ios.cpp @@ -266,10 +266,9 @@ ios_base::clear(iostate state) __rdstate_ = state; else __rdstate_ = state | badbit; -#ifndef _LIBCPP_NO_EXCEPTIONS + if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0) - throw failure("ios_base::clear"); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_failure("ios_base::clear"); } // init @@ -309,35 +308,27 @@ ios_base::copyfmt(const ios_base& rhs) { size_t newesize = sizeof(event_callback) * rhs.__event_size_; new_callbacks.reset(static_cast<event_callback*>(malloc(newesize))); -#ifndef _LIBCPP_NO_EXCEPTIONS if (!new_callbacks) - throw bad_alloc(); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_bad_alloc(); size_t newisize = sizeof(int) * rhs.__event_size_; new_ints.reset(static_cast<int *>(malloc(newisize))); -#ifndef _LIBCPP_NO_EXCEPTIONS if (!new_ints) - throw bad_alloc(); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_bad_alloc(); } if (__iarray_cap_ < rhs.__iarray_size_) { size_t newsize = sizeof(long) * rhs.__iarray_size_; new_longs.reset(static_cast<long*>(malloc(newsize))); -#ifndef _LIBCPP_NO_EXCEPTIONS if (!new_longs) - throw bad_alloc(); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_bad_alloc(); } if (__parray_cap_ < rhs.__parray_size_) { size_t newsize = sizeof(void*) * rhs.__parray_size_; new_pointers.reset(static_cast<void**>(malloc(newsize))); -#ifndef _LIBCPP_NO_EXCEPTIONS if (!new_pointers) - throw bad_alloc(); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_bad_alloc(); } // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_ __fmtflags_ = rhs.__fmtflags_; diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp index 18edad73f5b..00eb574ec45 100644 --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -468,10 +468,8 @@ locale::__imp::install(facet* f, long id) const locale::facet* locale::__imp::use_facet(long id) const { -#ifndef _LIBCPP_NO_EXCEPTIONS if (!has_facet(id)) - throw bad_cast(); -#endif // _LIBCPP_NO_EXCEPTIONS + __throw_bad_cast(); return facets_[static_cast<size_t>(id)]; } @@ -537,12 +535,8 @@ locale::operator=(const locale& other) _NOEXCEPT } locale::locale(const char* name) -#ifndef _LIBCPP_NO_EXCEPTIONS : __locale_(name ? new __imp(name) - : throw runtime_error("locale constructed with null")) -#else // _LIBCPP_NO_EXCEPTIONS - : __locale_(new __imp(name)) -#endif + : (__throw_runtime_error("locale constructed with null"), (__imp*)0)) { __locale_->__add_shared(); } @@ -554,12 +548,8 @@ locale::locale(const string& name) } locale::locale(const locale& other, const char* name, category c) -#ifndef _LIBCPP_NO_EXCEPTIONS : __locale_(name ? new __imp(*other.__locale_, name, c) - : throw runtime_error("locale constructed with null")) -#else // _LIBCPP_NO_EXCEPTIONS - : __locale_(new __imp(*other.__locale_, name, c)) -#endif + : (__throw_runtime_error("locale constructed with null"), (__imp*)0)) { __locale_->__add_shared(); } |

