Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/emc/rs274ngc/interp_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <charconv>
#include <string>
#include "rs274ngc.hh"
#include "rs274ngc_return.hh"
#include "interp_internal.hh"
Expand Down Expand Up @@ -740,7 +741,9 @@ int Interp::read_integer_unsigned(char *line, //!< string: line of RS274 code
break;
}
CHKS((n == *counter), NCE_BAD_FORMAT_UNSIGNED_INTEGER);
if (sscanf(line + *counter, "%d", integer_ptr) == 0)
// the digits are already delimited above, so from_chars needs no sscanf
std::from_chars_result r = std::from_chars(line + *counter, line + n, *integer_ptr);
if (r.ec != std::errc())
ERS(NCE_SSCANF_FAILED);
*counter = n;
return INTERP_OK;
Expand Down Expand Up @@ -2749,14 +2752,25 @@ int Interp::read_real_number(char *line, //!< string: line of RS274/NGC code bei

start = line + *counter;

after = strspn(start, "+-");
after = strspn(start+after, "0123456789.") + after;
size_t signs = strspn(start, "+-");
after = strspn(start+signs, "0123456789.") + signs;

std::string st(start, start+after);
std::stringstream s(st);
double val;
if(!(s >> val)) ERS(_("bad number format (conversion failed) parsing '%s'"), st.c_str());
if(s.get() != std::char_traits<char>::eof()) ERS(_("bad number format (trailing characters) parsing '%s'"), st.c_str());
const char *first = start + ((signs == 1 && *start == '+') ? 1 : 0);
const char *last = start + after;
double val = 0;
std::from_chars_result r{first, std::errc::invalid_argument};
if (signs <= 1) r = std::from_chars(first, last, val);

if (r.ec != std::errc()) {
// No number there, or a magnitude that does not fit a double; the stream
// conversion set failbit for the former and for an overflow.
std::string st(start, after);
ERS(_("bad number format (conversion failed) parsing '%s'"), st.c_str());
}
if (r.ptr != last) {
std::string st(start, after);
ERS(_("bad number format (trailing characters) parsing '%s'"), st.c_str());
}

*double_ptr = val;
*counter = start + after - line;
Expand Down