Skip to content

Commit 5145c7f

Browse files
committed
Add parsing of junk payload in proper format
1 parent 57d6061 commit 5145c7f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

include/boost/decimal/detail/parser.hpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,14 @@ constexpr auto parser(const char* first, const char* last, bool& sign, Unsigned_
141141
++next;
142142
if (next != last)
143143
{
144+
const auto current_pos {next};
145+
144146
bool any_valid_char {false};
147+
bool has_opening_brace {false};
145148
if (*next == '(')
146149
{
147150
++next;
148-
any_valid_char = true;
151+
has_opening_brace = true;
149152
}
150153

151154
// Handle nan(SNAN)
@@ -166,6 +169,7 @@ constexpr auto parser(const char* first, const char* last, bool& sign, Unsigned_
166169
}
167170

168171
// Arbitrary numerical payload
172+
bool has_numerical_payload {false};
169173
auto significand_buffer_first {significand_buffer};
170174
std::size_t significand_characters {};
171175
while (next != last && (*next != ')'))
@@ -175,6 +179,7 @@ constexpr auto parser(const char* first, const char* last, bool& sign, Unsigned_
175179
++significand_characters;
176180
*significand_buffer_first++ = *next++;
177181
any_valid_char = true;
182+
has_numerical_payload = true;
178183
}
179184
else
180185
{
@@ -184,6 +189,24 @@ constexpr auto parser(const char* first, const char* last, bool& sign, Unsigned_
184189
}
185190
}
186191

192+
// Non-numerical payload still needs to be parsed
193+
// e.g. nan(PAYLOAD)
194+
if (!has_numerical_payload && has_opening_brace)
195+
{
196+
while (next != last && (*next != ')'))
197+
{
198+
if (is_payload_char(*next))
199+
{
200+
any_valid_char = true;
201+
++next;
202+
}
203+
else
204+
{
205+
break;
206+
}
207+
}
208+
}
209+
187210
if (next != last && any_valid_char)
188211
{
189212
// One past the end if we need to
@@ -195,6 +218,12 @@ constexpr auto parser(const char* first, const char* last, bool& sign, Unsigned_
195218
from_chars_dispatch(significand_buffer, significand_buffer + significand_characters, significand, 10);
196219
}
197220

221+
if (!any_valid_char)
222+
{
223+
// If we have nan(..BAD..) we should point to (
224+
next = current_pos;
225+
}
226+
198227
exponent = static_cast<Integer>(signaling);
199228
return {next, std::errc::not_supported};
200229
}

0 commit comments

Comments
 (0)