Skip to content

Commit

Permalink
Fix use-after-free error in read_float_impl and std::setlocale
Browse files Browse the repository at this point in the history
Resolves #35
  • Loading branch information
eliaskosunen committed Jun 30, 2021
1 parent ed27703 commit 756a65d
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ namespace scn {
static expected<float> get(const char* str, size_t& chars)
{
char* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
float f = std::strtof(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand All @@ -90,12 +90,12 @@ namespace scn {
static expected<double> get(const char* str, size_t& chars)
{
char* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
double d = std::strtod(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand All @@ -117,12 +117,12 @@ namespace scn {
static expected<long double> get(const char* str, size_t& chars)
{
char* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
long double ld = std::strtold(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand All @@ -145,12 +145,12 @@ namespace scn {
static expected<float> get(const wchar_t* str, size_t& chars)
{
wchar_t* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
float f = std::wcstof(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand All @@ -171,12 +171,12 @@ namespace scn {
static expected<double> get(const wchar_t* str, size_t& chars)
{
wchar_t* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
double d = std::wcstod(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand All @@ -197,12 +197,12 @@ namespace scn {
static expected<long double> get(const wchar_t* str, size_t& chars)
{
wchar_t* end{};
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
const auto loc = std::string{std::setlocale(LC_NUMERIC, nullptr)};
std::setlocale(LC_NUMERIC, "C");
errno = 0;
long double ld = std::wcstold(str, &end);
chars = static_cast<size_t>(end - str);
std::setlocale(LC_NUMERIC, loc);
std::setlocale(LC_NUMERIC, loc.c_str());
if (errno == ERANGE) {
errno = 0;
return error(error::value_out_of_range,
Expand Down

0 comments on commit 756a65d

Please sign in to comment.