Skip to content

Commit 1be2524

Browse files
committed
[libcxx] Check return value for asprintf()
local __libcpp_asprintf_l() -> libc asprintf() was inspecting the pointer (with indeterminate value) for failure, rather than the return value of -1. Reviewed By: ldionne Differential Revision: https://reviews.llvm.org/D94564
1 parent 0cfadb3 commit 1be2524

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

libcxx/include/locale

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
15771577
__nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
15781578
else
15791579
__nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1580-
if (__nb == nullptr)
1580+
if (__nc == -1)
15811581
__throw_bad_alloc();
15821582
__nbh.reset(__nb);
15831583
}
@@ -1628,7 +1628,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
16281628
__nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
16291629
else
16301630
__nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1631-
if (__nb == nullptr)
1631+
if (__nc == -1)
16321632
__throw_bad_alloc();
16331633
__nbh.reset(__nb);
16341634
}
@@ -3402,17 +3402,17 @@ money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
34023402
char* __bb = __buf;
34033403
char_type __digits[__bs];
34043404
char_type* __db = __digits;
3405-
size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3405+
int __n = snprintf(__bb, __bs, "%.0Lf", __units);
34063406
unique_ptr<char, void(*)(void*)> __hn(nullptr, free);
34073407
unique_ptr<char_type, void(*)(void*)> __hd(0, free);
34083408
// secure memory for digit storage
3409-
if (__n > __bs-1)
3409+
if (static_cast<size_t>(__n) > __bs-1)
34103410
{
3411-
__n = static_cast<size_t>(__libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3412-
if (__bb == nullptr)
3411+
__n = __libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
3412+
if (__n == -1)
34133413
__throw_bad_alloc();
34143414
__hn.reset(__bb);
3415-
__hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3415+
__hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
34163416
if (__hd == nullptr)
34173417
__throw_bad_alloc();
34183418
__db = __hd.get();
@@ -3434,9 +3434,9 @@ money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
34343434
char_type __mbuf[__bs];
34353435
char_type* __mb = __mbuf;
34363436
unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3437-
size_t __exn = static_cast<int>(__n) > __fd ?
3438-
(__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3439-
__sym.size() + static_cast<size_t>(__fd) + 1
3437+
size_t __exn = __n > __fd ?
3438+
(static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 +
3439+
__sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
34403440
: __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
34413441
if (__exn > __bs)
34423442
{

0 commit comments

Comments
 (0)