Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 1a7c77d

Browse files
committed
fix: mask exceptions when trying to parse a number
fix #58
1 parent 3d0e0d5 commit 1a7c77d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

jsonxx.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,26 @@ bool parse_identifier(std::istream& input, String& value) {
182182
}
183183
}
184184

185+
class IOStateMasker {
186+
public:
187+
explicit IOStateMasker(std::istream& input): stream(input) {
188+
mask = input.exceptions();
189+
input.exceptions(0);
190+
}
191+
192+
~IOStateMasker() {
193+
stream.exceptions(mask);
194+
}
195+
196+
private:
197+
std::istream& stream;
198+
std::istream::iostate mask;
199+
};
200+
185201
bool parse_number(std::istream& input, Number& value) {
186202
input >> std::ws;
187203
std::streampos rollback = input.tellg();
204+
IOStateMasker masker(input);
188205
input >> value;
189206
if (input.fail()) {
190207
input.clear();

jsonxx_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ int main(int argc, const char **argv) {
120120
Object o;
121121
TEST(o.parse(input));
122122
}
123+
{
124+
string teststr("{ \"field1\" : true }");
125+
istringstream input(teststr);
126+
istringstream::iostate old_mask = input.exceptions();
127+
input.exceptions(istringstream::eofbit | istringstream::failbit |
128+
istringstream::badbit);
129+
Object o;
130+
TEST(o.parse(input));
131+
input.exceptions(old_mask);
132+
}
123133
{
124134
string teststr("{ \"field1 : 6 }");
125135
istringstream input(teststr);

0 commit comments

Comments
 (0)