Skip to content

When money_get::do_get analyzes a large value, inner code mallocs enough buffer, but sscanf doesn't get number from malloced buf #121878

@ZLFooler

Description

@ZLFooler

template <class _CharT, class _InputIterator>
_InputIterator money_get<_CharT, _InputIterator>::do_get(
iter_type __b, iter_type __e, bool __intl, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
const int __bz = 100;
char_type __wbuf[__bz];
unique_ptr<char_type, void (*)(void*)> __wb(__wbuf, __do_nothing);
char_type* __wn;
char_type* __we = __wbuf + __bz;
locale __loc = __iob.getloc();
const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
bool __neg = false;
if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct, __wb, __wn, __we)) {
const char __src[] = "0123456789";
char_type __atoms[sizeof(__src) - 1];
__ct.widen(__src, __src + (sizeof(__src) - 1), __atoms);
char __nbuf[__bz];
char* __nc = __nbuf;
unique_ptr<char, void (*)(void*)> __h(nullptr, free);
if (__wn - __wb.get() > __bz - 2) {
__h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
if (__h.get() == nullptr)
__throw_bad_alloc();
__nc = __h.get();
}
if (__neg)
*__nc++ = '-';
for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
*__nc = __src[std::find(__atoms, std::end(__atoms), *__w) - __atoms];
*__nc = char();
if (sscanf(__nbuf, "%Lf", &__v) != 1)
__throw_runtime_error("money_get error");
}
if (__b == __e)
__err |= ios_base::eofbit;
return __b;
}

When if (__wn - __wb.get() > __bz - 2) is true, __nc will change point from _nbuf to __h. The result will fill in __nc(__h), but sscanf(__nbuf, "%Lf", &__v) only get value from _nbuf. In this case, code can not get right value from __h.

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.localeissues related to localization

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions