Saleem Abdulrasool: avoid hardcoding buffer lengths.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@171169 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2012-12-27 21:17:53 +00:00
parent 0aa900e94f
commit bf68bdc4ed
2 changed files with 61 additions and 44 deletions

View File

@ -12,7 +12,6 @@
#include "stdexcept" #include "stdexcept"
#include "type_traits" #include "type_traits"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
_LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_NAMESPACE_STD

View File

@ -81,6 +81,24 @@ make(A0 a0, A1 a1, A2 a2)
return *(T*)&buf; return *(T*)&buf;
} }
template <typename T, size_t N>
_LIBCPP_ALWAYS_INLINE
_LIBCPP_CONSTEXPR
size_t
countof(const T (&)[N])
{
return N;
}
template <typename T>
_LIBCPP_ALWAYS_INLINE
_LIBCPP_CONSTEXPR
size_t
countof(const T * const begin, const T * const end)
{
return static_cast<size_t>(end - begin);
}
} }
const locale::category locale::none; const locale::category locale::none;
@ -4597,7 +4615,7 @@ __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct)
char f[3] = {0}; char f[3] = {0};
f[0] = '%'; f[0] = '%';
f[1] = fmt; f[1] = fmt;
size_t n = strftime_l(buf, 100, f, &t, __loc_); size_t n = strftime_l(buf, countof(buf), f, &t, __loc_);
char* bb = buf; char* bb = buf;
char* be = buf + n; char* be = buf + n;
string result; string result;
@ -4743,15 +4761,15 @@ __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
char f[3] = {0}; char f[3] = {0};
f[0] = '%'; f[0] = '%';
f[1] = fmt; f[1] = fmt;
strftime_l(buf, 100, f, &t, __loc_); strftime_l(buf, countof(buf), f, &t, __loc_);
wchar_t wbuf[100]; wchar_t wbuf[100];
wchar_t* wbb = wbuf; wchar_t* wbb = wbuf;
mbstate_t mb = {0}; mbstate_t mb = {0};
const char* bb = buf; const char* bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
#else #else
size_t j = __mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -4889,26 +4907,26 @@ __time_get_storage<char>::init(const ctype<char>& ct)
for (int i = 0; i < 7; ++i) for (int i = 0; i < 7; ++i)
{ {
t.tm_wday = i; t.tm_wday = i;
strftime_l(buf, 100, "%A", &t, __loc_); strftime_l(buf, countof(buf), "%A", &t, __loc_);
__weeks_[i] = buf; __weeks_[i] = buf;
strftime_l(buf, 100, "%a", &t, __loc_); strftime_l(buf, countof(buf), "%a", &t, __loc_);
__weeks_[i+7] = buf; __weeks_[i+7] = buf;
} }
// __months_ // __months_
for (int i = 0; i < 12; ++i) for (int i = 0; i < 12; ++i)
{ {
t.tm_mon = i; t.tm_mon = i;
strftime_l(buf, 100, "%B", &t, __loc_); strftime_l(buf, countof(buf), "%B", &t, __loc_);
__months_[i] = buf; __months_[i] = buf;
strftime_l(buf, 100, "%b", &t, __loc_); strftime_l(buf, countof(buf), "%b", &t, __loc_);
__months_[i+12] = buf; __months_[i+12] = buf;
} }
// __am_pm_ // __am_pm_
t.tm_hour = 1; t.tm_hour = 1;
strftime_l(buf, 100, "%p", &t, __loc_); strftime_l(buf, countof(buf), "%p", &t, __loc_);
__am_pm_[0] = buf; __am_pm_[0] = buf;
t.tm_hour = 13; t.tm_hour = 13;
strftime_l(buf, 100, "%p", &t, __loc_); strftime_l(buf, countof(buf), "%p", &t, __loc_);
__am_pm_[1] = buf; __am_pm_[1] = buf;
__c_ = __analyze('c', ct); __c_ = __analyze('c', ct);
__r_ = __analyze('r', ct); __r_ = __analyze('r', ct);
@ -4929,25 +4947,25 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
for (int i = 0; i < 7; ++i) for (int i = 0; i < 7; ++i)
{ {
t.tm_wday = i; t.tm_wday = i;
strftime_l(buf, 100, "%A", &t, __loc_); strftime_l(buf, countof(buf), "%A", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
const char* bb = buf; const char* bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
wbe = wbuf + j; wbe = wbuf + j;
__weeks_[i].assign(wbuf, wbe); __weeks_[i].assign(wbuf, wbe);
strftime_l(buf, 100, "%a", &t, __loc_); strftime_l(buf, countof(buf), "%a", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
bb = buf; bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -4958,25 +4976,25 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
for (int i = 0; i < 12; ++i) for (int i = 0; i < 12; ++i)
{ {
t.tm_mon = i; t.tm_mon = i;
strftime_l(buf, 100, "%B", &t, __loc_); strftime_l(buf, countof(buf), "%B", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
const char* bb = buf; const char* bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
wbe = wbuf + j; wbe = wbuf + j;
__months_[i].assign(wbuf, wbe); __months_[i].assign(wbuf, wbe);
strftime_l(buf, 100, "%b", &t, __loc_); strftime_l(buf, countof(buf), "%b", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
bb = buf; bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -4985,26 +5003,26 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
} }
// __am_pm_ // __am_pm_
t.tm_hour = 1; t.tm_hour = 1;
strftime_l(buf, 100, "%p", &t, __loc_); strftime_l(buf, countof(buf), "%p", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
const char* bb = buf; const char* bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
wbe = wbuf + j; wbe = wbuf + j;
__am_pm_[0].assign(wbuf, wbe); __am_pm_[0].assign(wbuf, wbe);
t.tm_hour = 13; t.tm_hour = 13;
strftime_l(buf, 100, "%p", &t, __loc_); strftime_l(buf, countof(buf), "%p", &t, __loc_);
mb = mbstate_t(); mb = mbstate_t();
bb = buf; bb = buf;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5267,7 +5285,7 @@ __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm,
char fmt[] = {'%', __fmt, __mod, 0}; char fmt[] = {'%', __fmt, __mod, 0};
if (__mod != 0) if (__mod != 0)
swap(fmt[1], fmt[2]); swap(fmt[1], fmt[2]);
size_t n = strftime_l(__nb, static_cast<size_t>(__ne-__nb), fmt, __tm, __loc_); size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
__ne = __nb + n; __ne = __nb + n;
} }
@ -5281,9 +5299,9 @@ __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
mbstate_t mb = {0}; mbstate_t mb = {0};
const char* __nb = __nar; const char* __nb = __nar;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_); size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
#else #else
size_t j = __mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_); size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5806,9 +5824,9 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
mbstate_t mb = {0}; mbstate_t mb = {0};
const char* bb = lc->currency_symbol; const char* bb = lc->currency_symbol;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5825,9 +5843,9 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
mb = mbstate_t(); mb = mbstate_t();
bb = lc->positive_sign; bb = lc->positive_sign;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5841,9 +5859,9 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
mb = mbstate_t(); mb = mbstate_t();
bb = lc->negative_sign; bb = lc->negative_sign;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5889,9 +5907,9 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
mbstate_t mb = {0}; mbstate_t mb = {0};
const char* bb = lc->int_curr_symbol; const char* bb = lc->int_curr_symbol;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5912,9 +5930,9 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
mb = mbstate_t(); mb = mbstate_t();
bb = lc->positive_sign; bb = lc->positive_sign;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");
@ -5932,9 +5950,9 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
mb = mbstate_t(); mb = mbstate_t();
bb = lc->negative_sign; bb = lc->negative_sign;
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#else #else
j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get()); j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
#endif #endif
if (j == size_t(-1)) if (j == size_t(-1))
__throw_runtime_error("locale not supported"); __throw_runtime_error("locale not supported");