• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

open-source-parsers / jsoncpp / 27653199524

16 Jun 2026 10:47PM UTC coverage: 89.949% (-0.01%) from 89.959%
27653199524

Pull #1695

github

baylesj
fix: accept subnormal doubles when parsing (#1427)

decodeDouble parsed numbers via `istringstream >> double`. For a
subnormal value such as `3.2114e-312`, operator>> sets failbit (the
result underflowed) even though it produced the correctly-rounded value.
The failure path only special-cased overflow, so subnormals were
rejected as "not a number" -- meaning a value jsoncpp had just serialized
could fail to parse back.

In the failure path, accept the value when it is a subnormal
(std::fpclassify(value) == FP_SUBNORMAL). This keys off the value
operator>> produces, which is the correctly-rounded subnormal on
libstdc++, libc++, and MSVC, so it needs no errno/eof heuristics. It
deliberately does not accept results that round to zero, so malformed
numbers like "0e" / "0e+" (jsonchecker fail29/fail30) and other junk are
still rejected. Applied to both Reader and OurReader.

Adds CharReaderTest/parseSubnormal covering subnormals, a writer
round-trip, and continued rejection of malformed numbers.
Pull Request #1695: fix: accept subnormal and underflowing doubles when parsing (#1427)

2201 of 2614 branches covered (84.2%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 1 file covered. (50.0%)

16 existing lines in 1 file now uncovered.

2605 of 2729 relevant lines covered (95.46%)

23444.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.03
/src/lib_json/json_reader.cpp
1
// Copyright 2007-2011 Baptiste Lepilleur and The JsonCpp Authors
2
// Copyright (C) 2016 InfoTeCS JSC. All rights reserved.
3
// Distributed under MIT license, or public domain if desired and
4
// recognized in your jurisdiction.
5
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
6

7
#if !defined(JSON_IS_AMALGAMATION)
8
#include "json_tool.h"
9
#include <json/assertions.h>
10
#include <json/reader.h>
11
#include <json/value.h>
12
#endif // if !defined(JSON_IS_AMALGAMATION)
13
#include <algorithm>
14
#include <cassert>
15
#include <cmath>
16
#include <cstring>
17
#include <iostream>
18
#include <istream>
19
#include <iterator>
20
#include <limits>
21
#include <memory>
22
#include <set>
23
#include <sstream>
24
#include <utility>
25

26
#include <cstdio>
27

28
#if defined(_MSC_VER)
29
#if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
30
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
31
#endif //_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
32
#endif //_MSC_VER
33

34
#if defined(_MSC_VER)
35
// Disable warning about strdup being deprecated.
36
#pragma warning(disable : 4996)
37
#endif
38

39
// Define JSONCPP_DEPRECATED_STACK_LIMIT as an appropriate integer at compile
40
// time to change the stack limit
41
#if !defined(JSONCPP_DEPRECATED_STACK_LIMIT)
42
#define JSONCPP_DEPRECATED_STACK_LIMIT 256
43
#endif
44

45
static size_t const stackLimit_g =
46
    JSONCPP_DEPRECATED_STACK_LIMIT; // see readValue()
47

48
namespace Json {
49

50
using CharReaderPtr = std::unique_ptr<CharReader>;
51

52
// Implementation of class Features
53
// ////////////////////////////////
54

55
Features::Features() = default;
614✔
56

57
Features Features::all() { return {}; }
16✔
58

59
Features Features::strictMode() {
100✔
60
  Features features;
100✔
61
  features.allowComments_ = false;
100✔
62
  features.strictRoot_ = true;
100✔
63
  features.allowDroppedNullPlaceholders_ = false;
100✔
64
  features.allowNumericKeys_ = false;
100✔
65
  return features;
100✔
66
}
67

68
// Implementation of class Reader
69
// ////////////////////////////////
70

71
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
184✔
72
  return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
347!
73
}
74

75
// Class Reader
76
// //////////////////////////////////////////////////////////////////
77

78
Reader::Reader() : features_(Features::all()) {}
32✔
79

80
Reader::Reader(const Features& features) : features_(features) {}
1,374✔
81

82
bool Reader::parse(const std::string& document, Value& root,
26✔
83
                   bool collectComments) {
84
  document_.assign(document.begin(), document.end());
26✔
85
  const char* begin = document_.c_str();
86
  const char* end = begin + document_.length();
26✔
87
  return parse(begin, end, root, collectComments);
26✔
88
}
89

90
bool Reader::parse(std::istream& is, Value& root, bool collectComments) {
1✔
91
  document_.assign(std::istreambuf_iterator<char>(is),
1✔
92
                   std::istreambuf_iterator<char>());
93
  return parse(document_.data(), document_.data() + document_.size(), root,
1✔
94
               collectComments);
1✔
95
}
96

97
bool Reader::parse(const char* beginDoc, const char* endDoc, Value& root,
711✔
98
                   bool collectComments) {
99
  if (!features_.allowComments_) {
711✔
100
    collectComments = false;
101
  }
102

103
  begin_ = beginDoc;
711✔
104
  end_ = endDoc;
711✔
105
  collectComments_ = collectComments;
711✔
106
  current_ = begin_;
711✔
107
  lastValueEnd_ = nullptr;
711✔
108
  lastValue_ = nullptr;
711✔
109
  commentsBefore_.clear();
110
  errors_.clear();
711✔
111
  while (!nodes_.empty())
722✔
112
    nodes_.pop();
11✔
113
  nodes_.push(&root);
711!
114

115
  bool successful = readValue();
711✔
116
  Token token;
117
  readTokenSkippingComments(token);
711✔
118
  if (collectComments_ && !commentsBefore_.empty())
711✔
119
    root.setComment(commentsBefore_, commentAfter);
26✔
120
  if (features_.strictRoot_) {
711✔
121
    if (!root.isArray() && !root.isObject()) {
1!
122
      // Set error location to start of doc, ideally should be first token found
123
      // in doc
124
      token.type_ = tokenError;
1✔
125
      token.start_ = beginDoc;
1✔
126
      token.end_ = endDoc;
1✔
127
      addError(
1✔
128
          "A valid JSON document must be either an array or an object value.",
129
          token);
130
      return false;
1✔
131
    }
132
  }
133
  return successful;
134
}
135

136
bool Reader::readValue() {
53,564!
137
  // readValue() may call itself only if it calls readObject() or ReadArray().
138
  // These methods execute nodes_.push() just before and nodes_.pop)() just
139
  // after calling readValue(). parse() executes one nodes_.push(), so > instead
140
  // of >=.
141
  if (nodes_.size() > stackLimit_g)
53,564!
142
#if JSON_USE_EXCEPTION
143
    throwRuntimeError("Exceeded stackLimit in readValue().");
×
144
#else
145
    // throwRuntimeError aborts. Don't abort here.
146
    return false;
147
#endif
148

149
  Token token;
150
  readTokenSkippingComments(token);
53,564✔
151
  bool successful = true;
152

153
  if (collectComments_ && !commentsBefore_.empty()) {
53,564✔
154
    currentValue().setComment(commentsBefore_, commentBefore);
1,191✔
155
    commentsBefore_.clear();
156
  }
157

158
  switch (token.type_) {
53,564✔
159
  case tokenObjectBegin:
244✔
160
    successful = readObject(token);
244✔
161
    currentValue().setOffsetLimit(current_ - begin_);
244✔
162
    break;
163
  case tokenArrayBegin:
190✔
164
    successful = readArray(token);
190✔
165
    currentValue().setOffsetLimit(current_ - begin_);
190✔
166
    break;
167
  case tokenNumber:
52,650✔
168
    successful = decodeNumber(token);
52,650✔
169
    break;
170
  case tokenString:
374✔
171
    successful = decodeString(token);
374✔
172
    break;
173
  case tokenTrue: {
26✔
174
    Value v(true);
26✔
175
    currentValue().swapPayload(v);
26✔
176
    currentValue().setOffsetStart(token.start_ - begin_);
26✔
177
    currentValue().setOffsetLimit(token.end_ - begin_);
26✔
178
  } break;
26✔
179
  case tokenFalse: {
25✔
180
    Value v(false);
25✔
181
    currentValue().swapPayload(v);
25✔
182
    currentValue().setOffsetStart(token.start_ - begin_);
25✔
183
    currentValue().setOffsetLimit(token.end_ - begin_);
25✔
184
  } break;
25✔
185
  case tokenNull: {
49✔
186
    Value v;
49✔
187
    currentValue().swapPayload(v);
49✔
188
    currentValue().setOffsetStart(token.start_ - begin_);
49✔
189
    currentValue().setOffsetLimit(token.end_ - begin_);
49✔
190
  } break;
49✔
191
  case tokenArraySeparator:
1✔
192
  case tokenObjectEnd:
193
  case tokenArrayEnd:
194
    if (features_.allowDroppedNullPlaceholders_) {
1!
195
      // "Un-read" the current token and mark the current value as a null
196
      // token.
197
      current_--;
1✔
198
      Value v;
1✔
199
      currentValue().swapPayload(v);
1✔
200
      currentValue().setOffsetStart(current_ - begin_ - 1);
1✔
201
      currentValue().setOffsetLimit(current_ - begin_);
1✔
202
      break;
203
    } // Else, fall through...
1✔
204
  default:
205
    currentValue().setOffsetStart(token.start_ - begin_);
5✔
206
    currentValue().setOffsetLimit(token.end_ - begin_);
5✔
207
    return addError("Syntax error: value, object or array expected.", token);
10✔
208
  }
209

210
  if (collectComments_) {
53,559✔
211
    lastValueEnd_ = current_;
53,558✔
212
    lastValue_ = &currentValue();
53,558✔
213
  }
214

215
  return successful;
216
}
217

218
bool Reader::readTokenSkippingComments(Token& token) {
107,548✔
219
  bool success = readToken(token);
107,548✔
220
  if (features_.allowComments_) {
107,548✔
221
    while (success && token.type_ == tokenComment) {
108,115✔
222
      success = readToken(token);
569✔
223
    }
224
  }
225
  return success;
107,548✔
226
}
227

228
bool Reader::readToken(Token& token) {
108,571✔
229
  skipSpaces();
108,571✔
230
  token.start_ = current_;
108,571✔
231
  Char c = getNextChar();
108,571✔
232
  bool ok = true;
233
  switch (c) {
108,571✔
234
  case '{':
244✔
235
    token.type_ = tokenObjectBegin;
244✔
236
    break;
237
  case '}':
242✔
238
    token.type_ = tokenObjectEnd;
242✔
239
    break;
240
  case '[':
190✔
241
    token.type_ = tokenArrayBegin;
190✔
242
    break;
243
  case ']':
189✔
244
    token.type_ = tokenArrayEnd;
189✔
245
    break;
246
  case '"':
792✔
247
    token.type_ = tokenString;
792✔
248
    ok = readString();
792✔
249
    break;
792✔
250
  case '/':
569✔
251
    token.type_ = tokenComment;
569✔
252
    ok = readComment();
569✔
253
    break;
569✔
254
  case '0':
52,651✔
255
  case '1':
256
  case '2':
257
  case '3':
258
  case '4':
259
  case '5':
260
  case '6':
261
  case '7':
262
  case '8':
263
  case '9':
264
  case '-':
265
    token.type_ = tokenNumber;
52,651✔
266
    readNumber();
52,651✔
267
    break;
268
  case 't':
26✔
269
    token.type_ = tokenTrue;
26✔
270
    ok = match("rue", 3);
26✔
271
    break;
26✔
272
  case 'f':
27✔
273
    token.type_ = tokenFalse;
27✔
274
    ok = match("alse", 4);
27✔
275
    break;
27✔
276
  case 'n':
52✔
277
    token.type_ = tokenNull;
52✔
278
    ok = match("ull", 3);
52✔
279
    break;
52✔
280
  case ',':
52,446✔
281
    token.type_ = tokenArraySeparator;
52,446✔
282
    break;
283
  case ':':
417✔
284
    token.type_ = tokenMemberSeparator;
417✔
285
    break;
286
  case 0:
718✔
287
    token.type_ = tokenEndOfStream;
718✔
288
    break;
289
  default:
290
    ok = false;
291
    break;
292
  }
293
  if (!ok)
1,466✔
294
    token.type_ = tokenError;
13✔
295
  token.end_ = current_;
108,571✔
296
  return ok;
108,571✔
297
}
298

299
void Reader::skipSpaces() {
108,761✔
300
  while (current_ != end_) {
226,621✔
301
    Char c = *current_;
225,903✔
302
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
225,903✔
303
      ++current_;
117,860✔
304
    else
305
      break;
306
  }
307
}
108,761✔
308

309
bool Reader::match(const Char* pattern, int patternLength) {
105✔
310
  if (end_ - current_ < patternLength)
105✔
311
    return false;
312
  int index = patternLength;
313
  while (index--)
429✔
314
    if (current_[index] != pattern[index])
329✔
315
      return false;
316
  current_ += patternLength;
100✔
317
  return true;
100✔
318
}
319

320
bool Reader::readComment() {
569✔
321
  Location commentBegin = current_ - 1;
569✔
322
  Char c = getNextChar();
569✔
323
  bool successful = false;
324
  if (c == '*')
569✔
325
    successful = readCStyleComment();
73✔
326
  else if (c == '/')
496!
327
    successful = readCppStyleComment();
496✔
328
  if (!successful)
569!
329
    return false;
×
330

331
  if (collectComments_) {
569!
332
    CommentPlacement placement = commentBefore;
333
    if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
569✔
334
      if (c != '*' || !containsNewLine(commentBegin, current_))
50!
335
        placement = commentAfterOnSameLine;
336
    }
337

338
    addComment(commentBegin, current_, placement);
569✔
339
  }
340
  return true;
341
}
342

343
String Reader::normalizeEOL(Reader::Location begin, Reader::Location end) {
569✔
344
  String normalized;
345
  normalized.reserve(static_cast<size_t>(end - begin));
569✔
346
  Reader::Location current = begin;
347
  while (current != end) {
21,596✔
348
    char c = *current++;
21,027✔
349
    if (c == '\r') {
21,027✔
350
      if (current != end && *current == '\n')
2!
351
        // convert dos EOL
352
        ++current;
1✔
353
      // convert Mac EOL
354
      normalized += '\n';
355
    } else {
356
      normalized += c;
357
    }
358
  }
359
  return normalized;
569✔
360
}
361

362
void Reader::addComment(Location begin, Location end,
569✔
363
                        CommentPlacement placement) {
364
  assert(collectComments_);
569!
365
  const String& normalized = normalizeEOL(begin, end);
569✔
366
  if (placement == commentAfterOnSameLine) {
569✔
367
    assert(lastValue_ != nullptr);
50!
368
    lastValue_->setComment(normalized, placement);
100✔
369
  } else {
370
    commentsBefore_ += normalized;
519✔
371
  }
372
}
569✔
373

374
bool Reader::readCStyleComment() {
73✔
375
  while ((current_ + 1) < end_) {
2,419!
376
    Char c = getNextChar();
2,419✔
377
    if (c == '*' && *current_ == '/')
2,419!
378
      break;
379
  }
380
  return getNextChar() == '/';
73✔
381
}
382

383
bool Reader::readCppStyleComment() {
496✔
384
  while (current_ != end_) {
17,397!
385
    Char c = getNextChar();
17,397✔
386
    if (c == '\n')
17,397✔
387
      break;
388
    if (c == '\r') {
16,903✔
389
      // Consume DOS EOL. It will be normalized in addComment.
390
      if (current_ != end_ && *current_ == '\n')
2!
391
        getNextChar();
1✔
392
      // Break on Moc OS 9 EOL.
393
      break;
394
    }
395
  }
396
  return true;
496✔
397
}
398

399
void Reader::readNumber() {
52,651✔
400
  Location p = current_;
52,651✔
401
  char c = '0'; // stopgap for already consumed character
402
  // integral part
403
  while (c >= '0' && c <= '9')
234,911✔
404
    c = (current_ = p) < end_ ? *p++ : '\0';
182,260✔
405
  // fractional part
406
  if (c == '.') {
52,651✔
407
    c = (current_ = p) < end_ ? *p++ : '\0';
91!
408
    while (c >= '0' && c <= '9')
674✔
409
      c = (current_ = p) < end_ ? *p++ : '\0';
583✔
410
  }
411
  // exponential part
412
  if (c == 'e' || c == 'E') {
52,651✔
413
    c = (current_ = p) < end_ ? *p++ : '\0';
67!
414
    if (c == '+' || c == '-')
67✔
415
      c = (current_ = p) < end_ ? *p++ : '\0';
55!
416
    while (c >= '0' && c <= '9')
225✔
417
      c = (current_ = p) < end_ ? *p++ : '\0';
158✔
418
  }
419
}
52,651✔
420

421
bool Reader::readString() {
792✔
422
  Char c = '\0';
423
  while (current_ != end_) {
30,369!
424
    c = getNextChar();
30,369✔
425
    if (c == '\\')
30,369✔
426
      getNextChar();
1,152✔
427
    else if (c == '"')
29,217✔
428
      break;
429
  }
430
  return c == '"';
792✔
431
}
432

433
bool Reader::readObject(Token& token) {
244✔
434
  Token tokenName;
435
  String name;
436
  Value init(objectValue);
244✔
437
  currentValue().swapPayload(init);
244✔
438
  currentValue().setOffsetStart(token.start_ - begin_);
244✔
439
  while (readTokenSkippingComments(tokenName)) {
429!
440
    if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
429!
441
      return true;
243✔
442
    name.clear();
443
    if (tokenName.type_ == tokenString) {
417✔
444
      if (!decodeString(tokenName, name))
415!
445
        return recoverFromError(tokenObjectEnd);
×
446
    } else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
2!
447
      Value numberName;
1✔
448
      if (!decodeNumber(tokenName, numberName))
1!
449
        return recoverFromError(tokenObjectEnd);
×
450
      name = numberName.asString();
1✔
451
    } else {
1✔
452
      break;
453
    }
454

455
    Token colon;
456
    if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
416!
457
      return addErrorAndRecover("Missing ':' after object member name", colon,
2✔
458
                                tokenObjectEnd);
459
    }
460
    Value& value = currentValue()[name];
415✔
461
    nodes_.push(&value);
415!
462
    bool ok = readValue();
415✔
463
    nodes_.pop();
415✔
464
    if (!ok) // error already set
415✔
465
      return recoverFromError(tokenObjectEnd);
5✔
466

467
    Token comma;
468
    if (!readTokenSkippingComments(comma) ||
410!
469
        (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator)) {
410✔
470
      return addErrorAndRecover("Missing ',' or '}' in object declaration",
2✔
471
                                comma, tokenObjectEnd);
472
    }
473
    if (comma.type_ == tokenObjectEnd)
409✔
474
      return true;
475
  }
476
  return addErrorAndRecover("Missing '}' or object member name", tokenName,
2✔
477
                            tokenObjectEnd);
478
}
244✔
479

480
bool Reader::readArray(Token& token) {
190✔
481
  Value init(arrayValue);
190✔
482
  currentValue().swapPayload(init);
190✔
483
  currentValue().setOffsetStart(token.start_ - begin_);
190✔
484
  skipSpaces();
190✔
485
  if (current_ != end_ && *current_ == ']') // empty array
190!
486
  {
487
    Token endArray;
488
    readToken(endArray);
12✔
489
    return true;
490
  }
491
  int index = 0;
492
  for (;;) {
493
    Value& value = currentValue()[index++];
52,438✔
494
    nodes_.push(&value);
52,438!
495
    bool ok = readValue();
52,438✔
496
    nodes_.pop();
52,438✔
497
    if (!ok) // error already set
52,438✔
498
      return recoverFromError(tokenArrayEnd);
6✔
499

500
    Token currentToken;
501
    // Accept Comment after last item in the array.
502
    ok = readTokenSkippingComments(currentToken);
52,434✔
503
    bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
52,434✔
504
                         currentToken.type_ != tokenArrayEnd);
52,434✔
505
    if (!ok || badTokenType) {
52,434✔
506
      return addErrorAndRecover("Missing ',' or ']' in array declaration",
4✔
507
                                currentToken, tokenArrayEnd);
508
    }
509
    if (currentToken.type_ == tokenArrayEnd)
52,432✔
510
      break;
511
  }
52,260✔
512
  return true;
172✔
513
}
190✔
514

515
bool Reader::decodeNumber(Token& token) {
52,650✔
516
  Value decoded;
52,650✔
517
  if (!decodeNumber(token, decoded))
52,650!
518
    return false;
519
  currentValue().swapPayload(decoded);
52,650✔
520
  currentValue().setOffsetStart(token.start_ - begin_);
52,650✔
521
  currentValue().setOffsetLimit(token.end_ - begin_);
52,650✔
522
  return true;
523
}
52,650✔
524

525
bool Reader::decodeNumber(Token& token, Value& decoded) {
52,651✔
526
  // Attempts to parse the number as an integer. If the number is
527
  // larger than the maximum supported value of an integer then
528
  // we decode the number as a double.
529
  Location current = token.start_;
52,651✔
530
  bool isNegative = *current == '-';
52,651✔
531
  if (isNegative)
52,651✔
532
    ++current;
133✔
533
  // TODO: Help the compiler do the div and mod at compile time or get rid of
534
  // them.
535
  Value::LargestUInt maxIntegerValue =
536
      isNegative ? Value::LargestUInt(Value::maxLargestInt) + 1
537
                 : Value::maxLargestUInt;
538
  Value::LargestUInt threshold = maxIntegerValue / 10;
52,651✔
539
  Value::LargestUInt value = 0;
540
  while (current < token.end_) {
234,760✔
541
    Char c = *current++;
182,254✔
542
    if (c < '0' || c > '9')
182,254✔
543
      return decodeDouble(token, decoded);
127✔
544
    auto digit(static_cast<Value::UInt>(c - '0'));
182,127✔
545
    if (value >= threshold) {
182,127✔
546
      // We've hit or exceeded the max value divided by 10 (rounded down). If
547
      // a) we've only just touched the limit, b) this is the last digit, and
548
      // c) it's small enough to fit in that rounding delta, we're okay.
549
      // Otherwise treat this number as a double to avoid overflow.
550
      if (value > threshold || current != token.end_ ||
42!
551
          digit > maxIntegerValue % 10) {
30✔
552
        return decodeDouble(token, decoded);
18✔
553
      }
554
    }
555
    value = value * 10 + digit;
182,109✔
556
  }
557
  if (isNegative && value == maxIntegerValue)
52,506✔
558
    decoded = Value::minLargestInt;
12✔
559
  else if (isNegative)
52,494✔
560
    decoded = -Value::LargestInt(value);
72✔
561
  else if (value <= Value::LargestUInt(Value::maxInt))
52,422✔
562
    decoded = Value::LargestInt(value);
52,362✔
563
  else
564
    decoded = value;
60✔
565
  return true;
566
}
567

568
bool Reader::decodeDouble(Token& token) {
×
569
  Value decoded;
×
570
  if (!decodeDouble(token, decoded))
×
571
    return false;
572
  currentValue().swapPayload(decoded);
×
573
  currentValue().setOffsetStart(token.start_ - begin_);
×
574
  currentValue().setOffsetLimit(token.end_ - begin_);
×
575
  return true;
576
}
×
577

578
bool Reader::decodeDouble(Token& token, Value& decoded) {
145✔
579
  double value = 0;
145✔
580
  IStringStream is(String(token.start_, token.end_));
145✔
581
  is.imbue(std::locale::classic());
145✔
582
  if (!(is >> value)) {
145✔
583
    if (value == std::numeric_limits<double>::max())
24✔
584
      value = std::numeric_limits<double>::infinity();
12✔
585
    else if (value == std::numeric_limits<double>::lowest())
12!
586
      value = -std::numeric_limits<double>::infinity();
12✔
587
    // operator>> sets failbit for a subnormal result (underflow) even though
588
    // it produced the correctly-rounded value, which made such numbers fail to
589
    // parse back after jsoncpp serialized them. Keep a subnormal value instead
590
    // of rejecting it. See issue #1427. Other failures -- malformed numbers
591
    // like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite
592
    // and are still rejected.
NEW
593
    else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL)
×
594
      return addError(
×
595
          "'" + String(token.start_, token.end_) + "' is not a number.", token);
×
596
  }
597
  decoded = value;
145✔
598
  return true;
145✔
599
}
145✔
600

601
bool Reader::decodeString(Token& token) {
374✔
602
  String decoded_string;
603
  if (!decodeString(token, decoded_string))
374✔
604
    return false;
605
  Value decoded(decoded_string);
369✔
606
  currentValue().swapPayload(decoded);
369✔
607
  currentValue().setOffsetStart(token.start_ - begin_);
369✔
608
  currentValue().setOffsetLimit(token.end_ - begin_);
369✔
609
  return true;
610
}
369✔
611

612
bool Reader::decodeString(Token& token, String& decoded) {
789✔
613
  decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
789✔
614
  Location current = token.start_ + 1; // skip '"'
789✔
615
  Location end = token.end_ - 1;       // do not include '"'
789✔
616
  while (current != end) {
29,861✔
617
    Char c = *current++;
29,077✔
618
    if (c == '"')
29,077!
619
      break;
620
    if (c == '\\') {
29,077✔
621
      if (current == end)
1,138!
622
        return addError("Empty escape sequence in string", token, current);
×
623
      Char escape = *current++;
1,138✔
624
      switch (escape) {
1,138✔
625
      case '"':
626
        decoded += '"';
627
        break;
628
      case '/':
629
        decoded += '/';
630
        break;
631
      case '\\':
632
        decoded += '\\';
633
        break;
634
      case 'b':
635
        decoded += '\b';
636
        break;
637
      case 'f':
638
        decoded += '\f';
639
        break;
640
      case 'n':
641
        decoded += '\n';
642
        break;
643
      case 'r':
644
        decoded += '\r';
645
        break;
646
      case 't':
647
        decoded += '\t';
648
        break;
649
      case 'u': {
103✔
650
        unsigned int unicode;
651
        if (!decodeUnicodeCodePoint(token, current, end, unicode))
103✔
652
          return false;
4✔
653
        decoded += codePointToUTF8(unicode);
99✔
654
      } break;
99✔
655
      default:
1✔
656
        return addError("Bad escape sequence in string", token, current);
2✔
657
      }
658
    } else {
659
      if (static_cast<unsigned char>(c) < 0x20)
27,939!
660
        return addError("Control character in string", token, current - 1);
×
661
      decoded += c;
662
    }
663
  }
664
  return true;
665
}
666

667
bool Reader::decodeUnicodeCodePoint(Token& token, Location& current,
103✔
668
                                    Location end, unsigned int& unicode) {
669

670
  if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
103✔
671
    return false;
672
  if (unicode >= 0xD800 && unicode <= 0xDBFF) {
101✔
673
    // surrogate pairs
674
    if (end - current < 6)
15✔
675
      return addError(
2✔
676
          "additional six characters expected to parse unicode surrogate pair.",
677
          token, current);
678
    if (*(current++) == '\\' && *(current++) == 'u') {
14!
679
      unsigned int surrogatePair;
680
      if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
13!
681
        unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
13✔
682
      } else
683
        return false;
×
684
    } else
685
      return addError("expecting another \\u token to begin the second half of "
2✔
686
                      "a unicode surrogate pair",
687
                      token, current);
688
  }
689
  return true;
690
}
691

692
bool Reader::decodeUnicodeEscapeSequence(Token& token, Location& current,
116✔
693
                                         Location end,
694
                                         unsigned int& ret_unicode) {
695
  if (end - current < 4)
116✔
696
    return addError(
2✔
697
        "Bad unicode escape sequence in string: four digits expected.", token,
698
        current);
699
  int unicode = 0;
700
  for (int index = 0; index < 4; ++index) {
573✔
701
    Char c = *current++;
459✔
702
    unicode *= 16;
459✔
703
    if (c >= '0' && c <= '9')
459✔
704
      unicode += c - '0';
342✔
705
    else if (c >= 'a' && c <= 'f')
117✔
706
      unicode += c - 'a' + 10;
74✔
707
    else if (c >= 'A' && c <= 'F')
43✔
708
      unicode += c - 'A' + 10;
42✔
709
    else
710
      return addError(
2✔
711
          "Bad unicode escape sequence in string: hexadecimal digit expected.",
712
          token, current);
713
  }
714
  ret_unicode = static_cast<unsigned int>(unicode);
114✔
715
  return true;
114✔
716
}
717

718
bool Reader::addError(const String& message, Token& token, Location extra) {
16✔
719
  ErrorInfo info;
720
  info.token_ = token;
16✔
721
  info.message_ = message;
722
  info.extra_ = extra;
16✔
723
  errors_.push_back(info);
16✔
724
  return false;
16✔
725
}
726

727
bool Reader::recoverFromError(TokenType skipUntilToken) {
14✔
728
  size_t const errorCount = errors_.size();
729
  Token skip;
730
  for (;;) {
731
    if (!readToken(skip))
26✔
732
      errors_.resize(errorCount); // discard errors caused by recovery
10✔
733
    if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
26✔
734
      break;
735
  }
736
  errors_.resize(errorCount);
14✔
737
  return false;
14✔
738
}
739

740
bool Reader::addErrorAndRecover(const String& message, Token& token,
5✔
741
                                TokenType skipUntilToken) {
742
  addError(message, token);
5✔
743
  return recoverFromError(skipUntilToken);
5✔
744
}
745

746
Value& Reader::currentValue() { return *(nodes_.top()); }
267,480✔
747

748
Reader::Char Reader::getNextChar() {
160,551✔
749
  if (current_ == end_)
160,551✔
750
    return 0;
751
  return *current_++;
159,833✔
752
}
753

754
void Reader::getLocationLineAndColumn(Location location, int& line,
24✔
755
                                      int& column) const {
756
  Location current = begin_;
24✔
757
  Location lastLineStart = current;
758
  line = 0;
24✔
759
  while (current < location && current != end_) {
263!
760
    Char c = *current++;
239✔
761
    if (c == '\r') {
239!
762
      if (current != end_ && *current == '\n')
×
763
        ++current;
×
764
      lastLineStart = current;
765
      ++line;
×
766
    } else if (c == '\n') {
239!
767
      lastLineStart = current;
768
      ++line;
×
769
    }
770
  }
771
  // column & line start at 1
772
  column = int(location - lastLineStart) + 1;
24✔
773
  ++line;
24✔
774
}
24✔
775

776
String Reader::getLocationLineAndColumn(Location location) const {
24✔
777
  int line, column;
778
  getLocationLineAndColumn(location, line, column);
24✔
779
  char buffer[18 + 16 + 16 + 1];
780
  jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
24✔
781
  return buffer;
24✔
782
}
783

784
// Deprecated. Preserved for backward compatibility
785
String Reader::getFormatedErrorMessages() const {
×
786
  return getFormattedErrorMessages();
×
787
}
788

789
String Reader::getFormattedErrorMessages() const {
19✔
790
  String formattedMessage;
791
  for (const auto& error : errors_) {
37✔
792
    formattedMessage +=
793
        "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
18✔
794
    formattedMessage += "  " + error.message_ + "\n";
18✔
795
    if (error.extra_)
18✔
796
      formattedMessage +=
797
          "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n";
12✔
798
  }
799
  return formattedMessage;
19✔
800
}
801

802
std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
17✔
803
  std::vector<Reader::StructuredError> allErrors;
804
  for (const auto& error : errors_) {
33✔
805
    Reader::StructuredError structured;
806
    structured.offset_start = error.token_.start_ - begin_;
16✔
807
    structured.offset_limit = error.token_.end_ - begin_;
16✔
808
    structured.message = error.message_;
16✔
809
    allErrors.push_back(structured);
16✔
810
  }
811
  return allErrors;
17✔
812
}
×
813

814
bool Reader::pushError(const Value& value, const String& message) {
1✔
815
  ptrdiff_t const length = end_ - begin_;
1✔
816
  if (value.getOffsetStart() > length || value.getOffsetLimit() > length)
1!
817
    return false;
×
818
  Token token;
819
  token.type_ = tokenError;
820
  token.start_ = begin_ + value.getOffsetStart();
1✔
821
  token.end_ = begin_ + value.getOffsetLimit();
1✔
822
  ErrorInfo info;
823
  info.token_ = token;
1✔
824
  info.message_ = message;
825
  info.extra_ = nullptr;
1✔
826
  errors_.push_back(info);
1✔
827
  return true;
828
}
829

830
bool Reader::pushError(const Value& value, const String& message,
1✔
831
                       const Value& extra) {
832
  ptrdiff_t const length = end_ - begin_;
1✔
833
  if (value.getOffsetStart() > length || value.getOffsetLimit() > length ||
2!
834
      extra.getOffsetLimit() > length)
1✔
835
    return false;
×
836
  Token token;
837
  token.type_ = tokenError;
838
  token.start_ = begin_ + value.getOffsetStart();
1✔
839
  token.end_ = begin_ + value.getOffsetLimit();
1✔
840
  ErrorInfo info;
841
  info.token_ = token;
1✔
842
  info.message_ = message;
843
  info.extra_ = begin_ + extra.getOffsetStart();
1✔
844
  errors_.push_back(info);
1✔
845
  return true;
846
}
847

848
bool Reader::good() const { return errors_.empty(); }
×
849

850
// Originally copied from the Features class (now deprecated), used internally
851
// for features implementation.
852
class OurFeatures {
853
public:
854
  static OurFeatures all();
855
  bool allowComments_;
856
  bool allowTrailingCommas_;
857
  bool strictRoot_;
858
  bool allowDroppedNullPlaceholders_;
859
  bool allowNumericKeys_;
860
  bool allowSingleQuotes_;
861
  bool failIfExtra_;
862
  bool rejectDupKeys_;
863
  bool allowSpecialFloats_;
864
  bool skipBom_;
865
  size_t stackLimit_;
866
}; // OurFeatures
867

868
OurFeatures OurFeatures::all() { return {}; }
903✔
869

870
// Implementation of class Reader
871
// ////////////////////////////////
872

873
// Originally copied from the Reader class (now deprecated), used internally
874
// for implementing JSON reading.
875
class OurReader {
876
public:
877
  using Char = char;
878
  using Location = const Char*;
879

880
  explicit OurReader(OurFeatures const& features);
881
  bool parse(const char* beginDoc, const char* endDoc, Value& root,
882
             bool collectComments = true);
883
  String getFormattedErrorMessages() const;
884
  std::vector<CharReader::StructuredError> getStructuredErrors() const;
885

886
private:
887
  OurReader(OurReader const&);      // no impl
888
  void operator=(OurReader const&); // no impl
889

890
  enum TokenType {
891
    tokenEndOfStream = 0,
892
    tokenObjectBegin,
893
    tokenObjectEnd,
894
    tokenArrayBegin,
895
    tokenArrayEnd,
896
    tokenString,
897
    tokenNumber,
898
    tokenTrue,
899
    tokenFalse,
900
    tokenNull,
901
    tokenNaN,
902
    tokenPosInf,
903
    tokenNegInf,
904
    tokenArraySeparator,
905
    tokenMemberSeparator,
906
    tokenComment,
907
    tokenError
908
  };
909

910
  class Token {
911
  public:
912
    TokenType type_;
913
    Location start_;
914
    Location end_;
915
  };
916

917
  class ErrorInfo {
165✔
918
  public:
919
    Token token_;
920
    String message_;
921
    Location extra_;
922
  };
923

924
  using Errors = std::deque<ErrorInfo>;
925

926
  bool readToken(Token& token);
927
  bool readTokenSkippingComments(Token& token);
928
  void skipSpaces();
929
  void skipBom(bool skipBom);
930
  bool match(const Char* pattern, int patternLength);
931
  bool readComment();
932
  bool readCStyleComment(bool* containsNewLineResult);
933
  bool readCppStyleComment();
934
  bool readString();
935
  bool readStringSingleQuote();
936
  bool readNumber(bool checkInf);
937
  bool readValue();
938
  bool readObject(Token& token);
939
  bool readArray(Token& token);
940
  bool decodeNumber(Token& token);
941
  bool decodeNumber(Token& token, Value& decoded);
942
  bool decodeString(Token& token);
943
  bool decodeString(Token& token, String& decoded);
944
  bool decodeDouble(Token& token);
945
  bool decodeDouble(Token& token, Value& decoded);
946
  bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
947
                              unsigned int& unicode);
948
  bool decodeUnicodeEscapeSequence(Token& token, Location& current,
949
                                   Location end, unsigned int& unicode);
950
  bool addError(const String& message, Token& token, Location extra = nullptr);
951
  bool recoverFromError(TokenType skipUntilToken);
952
  bool addErrorAndRecover(const String& message, Token& token,
953
                          TokenType skipUntilToken);
954
  void skipUntilSpace();
955
  Value& currentValue();
956
  Char getNextChar();
957
  void getLocationLineAndColumn(Location location, int& line,
958
                                int& column) const;
959
  String getLocationLineAndColumn(Location location) const;
960
  void addComment(Location begin, Location end, CommentPlacement placement);
961

962
  static String normalizeEOL(Location begin, Location end);
963
  static bool containsNewLine(Location begin, Location end);
964

965
  using Nodes = std::stack<Value*>;
966

967
  Nodes nodes_{};
968
  Errors errors_{};
969
  String document_{};
970
  Location begin_ = nullptr;
971
  Location end_ = nullptr;
972
  Location current_ = nullptr;
973
  Location lastValueEnd_ = nullptr;
974
  Value* lastValue_ = nullptr;
975
  bool lastValueHasAComment_ = false;
976
  String commentsBefore_{};
977

978
  OurFeatures const features_;
979
  bool collectComments_ = false;
980
}; // OurReader
981

982
// complete copy of Read impl, for OurReader
983

984
// Test-only instrumentation: total bytes examined by
985
// OurReader::containsNewLine, so unit tests can assert that comment handling
986
// stays linear in the input rather than quadratic in the comment count (see
987
// CharReaderTest/parseCommentsAfterValueScansLinearly). thread_local so it
988
// never races during concurrent parsing; the increment is negligible and only
989
// runs while parsing comments. Not part of the supported public API.
990
JSON_API size_t& newlineScanByteCountForTesting() {
164✔
991
  static thread_local size_t count = 0;
992
  return count;
164✔
993
}
994

995
bool OurReader::containsNewLine(OurReader::Location begin,
162✔
996
                                OurReader::Location end) {
997
  newlineScanByteCountForTesting() += static_cast<size_t>(end - begin);
162✔
998
  return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
202!
999
}
1000

1001
OurReader::OurReader(OurFeatures const& features) : features_(features) {}
1,806✔
1002

1003
bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
941✔
1004
                      bool collectComments) {
1005
  if (!features_.allowComments_) {
941✔
1006
    collectComments = false;
1007
  }
1008

1009
  begin_ = beginDoc;
941✔
1010
  end_ = endDoc;
941✔
1011
  collectComments_ = collectComments;
941✔
1012
  current_ = begin_;
941✔
1013
  lastValueEnd_ = nullptr;
941✔
1014
  lastValue_ = nullptr;
941✔
1015
  commentsBefore_.clear();
1016
  errors_.clear();
941✔
1017
  while (!nodes_.empty())
941!
1018
    nodes_.pop();
×
1019
  nodes_.push(&root);
941!
1020

1021
  // skip byte order mark if it exists at the beginning of the UTF-8 text.
1022
  skipBom(features_.skipBom_);
941✔
1023
  bool successful = readValue();
941✔
1024
  nodes_.pop();
933✔
1025
  Token token;
1026
  readTokenSkippingComments(token);
933✔
1027
  if (features_.failIfExtra_ && (token.type_ != tokenEndOfStream)) {
933✔
1028
    addError("Extra non-whitespace after JSON value.", token);
5✔
1029
    return false;
5✔
1030
  }
1031
  if (collectComments_ && !commentsBefore_.empty())
928✔
1032
    root.setComment(commentsBefore_, commentAfter);
48✔
1033
  if (features_.strictRoot_) {
928✔
1034
    if (!root.isArray() && !root.isObject()) {
101✔
1035
      // Set error location to start of doc, ideally should be first token found
1036
      // in doc
1037
      token.type_ = tokenError;
4✔
1038
      token.start_ = beginDoc;
4✔
1039
      token.end_ = endDoc;
4✔
1040
      addError(
4✔
1041
          "A valid JSON document must be either an array or an object value.",
1042
          token);
1043
      return false;
4✔
1044
    }
1045
  }
1046
  return successful;
1047
}
1048

1049
bool OurReader::readValue() {
56,109✔
1050
  //  To preserve the old behaviour we cast size_t to int.
1051
  if (nodes_.size() > features_.stackLimit_)
56,109✔
1052
    throwRuntimeError("Exceeded stackLimit in readValue().");
8✔
1053
  Token token;
1054
  readTokenSkippingComments(token);
56,101✔
1055
  bool successful = true;
1056

1057
  if (collectComments_ && !commentsBefore_.empty()) {
56,101✔
1058
    currentValue().setComment(commentsBefore_, commentBefore);
1,203✔
1059
    commentsBefore_.clear();
1060
  }
1061

1062
  switch (token.type_) {
56,101✔
1063
  case tokenObjectBegin:
367✔
1064
    successful = readObject(token);
367✔
1065
    currentValue().setOffsetLimit(current_ - begin_);
366✔
1066
    break;
1067
  case tokenArrayBegin:
2,158✔
1068
    successful = readArray(token);
2,158✔
1069
    currentValue().setOffsetLimit(current_ - begin_);
366✔
1070
    break;
1071
  case tokenNumber:
52,842✔
1072
    successful = decodeNumber(token);
52,842✔
1073
    break;
1074
  case tokenString:
519✔
1075
    successful = decodeString(token);
519✔
1076
    break;
1077
  case tokenTrue: {
48✔
1078
    Value v(true);
48✔
1079
    currentValue().swapPayload(v);
48✔
1080
    currentValue().setOffsetStart(token.start_ - begin_);
48✔
1081
    currentValue().setOffsetLimit(token.end_ - begin_);
48✔
1082
  } break;
48✔
1083
  case tokenFalse: {
31✔
1084
    Value v(false);
31✔
1085
    currentValue().swapPayload(v);
31✔
1086
    currentValue().setOffsetStart(token.start_ - begin_);
31✔
1087
    currentValue().setOffsetLimit(token.end_ - begin_);
31✔
1088
  } break;
31✔
1089
  case tokenNull: {
61✔
1090
    Value v;
61✔
1091
    currentValue().swapPayload(v);
61✔
1092
    currentValue().setOffsetStart(token.start_ - begin_);
61✔
1093
    currentValue().setOffsetLimit(token.end_ - begin_);
61✔
1094
  } break;
61✔
1095
  case tokenNaN: {
1✔
1096
    Value v(std::numeric_limits<double>::quiet_NaN());
1✔
1097
    currentValue().swapPayload(v);
1✔
1098
    currentValue().setOffsetStart(token.start_ - begin_);
1✔
1099
    currentValue().setOffsetLimit(token.end_ - begin_);
1✔
1100
  } break;
1✔
1101
  case tokenPosInf: {
5✔
1102
    Value v(std::numeric_limits<double>::infinity());
5✔
1103
    currentValue().swapPayload(v);
5✔
1104
    currentValue().setOffsetStart(token.start_ - begin_);
5✔
1105
    currentValue().setOffsetLimit(token.end_ - begin_);
5✔
1106
  } break;
5✔
1107
  case tokenNegInf: {
3✔
1108
    Value v(-std::numeric_limits<double>::infinity());
3✔
1109
    currentValue().swapPayload(v);
3✔
1110
    currentValue().setOffsetStart(token.start_ - begin_);
3✔
1111
    currentValue().setOffsetLimit(token.end_ - begin_);
3✔
1112
  } break;
3✔
1113
  case tokenArraySeparator:
41✔
1114
  case tokenObjectEnd:
1115
  case tokenArrayEnd:
1116
    if (features_.allowDroppedNullPlaceholders_) {
41✔
1117
      // "Un-read" the current token and mark the current value as a null
1118
      // token.
1119
      current_--;
29✔
1120
      Value v;
29✔
1121
      currentValue().swapPayload(v);
29✔
1122
      currentValue().setOffsetStart(current_ - begin_ - 1);
29✔
1123
      currentValue().setOffsetLimit(current_ - begin_);
29✔
1124
      break;
1125
    } // else, fall through ...
29✔
1126
  default:
1127
    currentValue().setOffsetStart(token.start_ - begin_);
37✔
1128
    currentValue().setOffsetLimit(token.end_ - begin_);
37✔
1129
    return addError("Syntax error: value, object or array expected.", token);
74✔
1130
  }
1131

1132
  if (collectComments_) {
54,271✔
1133
    lastValueEnd_ = current_;
53,810✔
1134
    lastValueHasAComment_ = false;
53,810✔
1135
    lastValue_ = &currentValue();
53,810✔
1136
  }
1137

1138
  return successful;
1139
}
1140

1141
bool OurReader::readTokenSkippingComments(Token& token) {
111,030✔
1142
  bool success = readToken(token);
111,030✔
1143
  if (features_.allowComments_) {
111,030✔
1144
    while (success && token.type_ == tokenComment) {
111,531✔
1145
      success = readToken(token);
1,591✔
1146
    }
1147
  }
1148
  return success;
111,030✔
1149
}
1150

1151
bool OurReader::readToken(Token& token) {
113,703✔
1152
  skipSpaces();
113,703✔
1153
  token.start_ = current_;
113,703✔
1154
  Char c = getNextChar();
113,703✔
1155
  bool ok = true;
1156
  switch (c) {
113,703✔
1157
  case '{':
373✔
1158
    token.type_ = tokenObjectBegin;
373✔
1159
    break;
1160
  case '}':
359✔
1161
    token.type_ = tokenObjectEnd;
359✔
1162
    break;
1163
  case '[':
2,158✔
1164
    token.type_ = tokenArrayBegin;
2,158✔
1165
    break;
1166
  case ']':
367✔
1167
    token.type_ = tokenArrayEnd;
367✔
1168
    break;
1169
  case '"':
1,174✔
1170
    token.type_ = tokenString;
1,174✔
1171
    ok = readString();
1,174✔
1172
    break;
1,174✔
1173
  case '\'':
23✔
1174
    if (features_.allowSingleQuotes_) {
23✔
1175
      token.type_ = tokenString;
11✔
1176
      ok = readStringSingleQuote();
11✔
1177
    } else {
1178
      // If we don't allow single quotes, this is a failure case.
1179
      ok = false;
1180
    }
1181
    break;
1182
  case '/':
1,616✔
1183
    token.type_ = tokenComment;
1,616✔
1184
    ok = readComment();
1,616✔
1185
    break;
1,616✔
1186
  case '0':
52,728✔
1187
  case '1':
1188
  case '2':
1189
  case '3':
1190
  case '4':
1191
  case '5':
1192
  case '6':
1193
  case '7':
1194
  case '8':
1195
  case '9':
1196
    token.type_ = tokenNumber;
52,728✔
1197
    readNumber(false);
52,728✔
1198
    break;
1199
  case '-':
148✔
1200
    if (readNumber(true)) {
148✔
1201
      token.type_ = tokenNumber;
145✔
1202
    } else {
1203
      token.type_ = tokenNegInf;
3✔
1204
      ok = features_.allowSpecialFloats_ && match("nfinity", 7);
3!
1205
    }
1206
    break;
1207
  case '+':
7✔
1208
    if (readNumber(true)) {
7✔
1209
      token.type_ = tokenNumber;
4✔
1210
    } else {
1211
      token.type_ = tokenPosInf;
3✔
1212
      ok = features_.allowSpecialFloats_ && match("nfinity", 7);
3!
1213
    }
1214
    break;
1215
  case 't':
72✔
1216
    token.type_ = tokenTrue;
72✔
1217
    ok = match("rue", 3);
72✔
1218
    break;
72✔
1219
  case 'f':
43✔
1220
    token.type_ = tokenFalse;
43✔
1221
    ok = match("alse", 4);
43✔
1222
    break;
43✔
1223
  case 'n':
97✔
1224
    token.type_ = tokenNull;
97✔
1225
    ok = match("ull", 3);
97✔
1226
    break;
97✔
1227
  case 'N':
3✔
1228
    if (features_.allowSpecialFloats_) {
3✔
1229
      token.type_ = tokenNaN;
1✔
1230
      ok = match("aN", 2);
1✔
1231
    } else {
1232
      ok = false;
1233
    }
1234
    break;
1235
  case 'I':
8✔
1236
    if (features_.allowSpecialFloats_) {
8✔
1237
      token.type_ = tokenPosInf;
7✔
1238
      ok = match("nfinity", 7);
7✔
1239
    } else {
1240
      ok = false;
1241
    }
1242
    break;
1243
  case ',':
52,771✔
1244
    token.type_ = tokenArraySeparator;
52,771✔
1245
    break;
1246
  case ':':
663✔
1247
    token.type_ = tokenMemberSeparator;
663✔
1248
    break;
1249
  case 0:
952✔
1250
    token.type_ = tokenEndOfStream;
952✔
1251
    break;
1252
  default:
1253
    ok = false;
1254
    break;
1255
  }
1256
  if (!ok)
3,021✔
1257
    token.type_ = tokenError;
216✔
1258
  token.end_ = current_;
113,703✔
1259
  return ok;
113,703✔
1260
}
1261

1262
void OurReader::skipSpaces() {
168,267✔
1263
  while (current_ != end_) {
287,994✔
1264
    Char c = *current_;
287,042✔
1265
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
287,042✔
1266
      ++current_;
119,727✔
1267
    else
1268
      break;
1269
  }
1270
}
168,267✔
1271

1272
void OurReader::skipBom(bool skipBom) {
941✔
1273
  // The default behavior is to skip BOM.
1274
  if (skipBom) {
941✔
1275
    if ((end_ - begin_) >= 3 && strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
940✔
1276
      begin_ += 3;
1✔
1277
      current_ = begin_;
1✔
1278
    }
1279
  }
1280
}
941✔
1281

1282
bool OurReader::match(const Char* pattern, int patternLength) {
226✔
1283
  if (end_ - current_ < patternLength)
226✔
1284
    return false;
1285
  int index = patternLength;
1286
  while (index--)
797✔
1287
    if (current_[index] != pattern[index])
631✔
1288
      return false;
1289
  current_ += patternLength;
166✔
1290
  return true;
166✔
1291
}
1292

1293
bool OurReader::readComment() {
1,616✔
1294
  const Location commentBegin = current_ - 1;
1,616✔
1295
  const Char c = getNextChar();
1,616✔
1296
  bool successful = false;
1297
  bool cStyleWithEmbeddedNewline = false;
1,616✔
1298

1299
  const bool isCStyleComment = (c == '*');
1300
  const bool isCppStyleComment = (c == '/');
1301
  if (isCStyleComment) {
1,616✔
1302
    successful = readCStyleComment(&cStyleWithEmbeddedNewline);
1,086✔
1303
  } else if (isCppStyleComment) {
530!
1304
    successful = readCppStyleComment();
530✔
1305
  }
1306

1307
  if (!successful)
1,616!
1308
    return false;
×
1309

1310
  if (collectComments_) {
1,616✔
1311
    CommentPlacement placement = commentBefore;
1312

1313
    if (!lastValueHasAComment_) {
1,598✔
1314
      if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
434✔
1315
        if (isCppStyleComment || !cStyleWithEmbeddedNewline) {
66✔
1316
          placement = commentAfterOnSameLine;
1317
        }
1318
      }
1319
      // The gap between the last value and this comment only grows as more
1320
      // comments are consumed, so a later comment can never be on the same
1321
      // line as that value. Mark it handled to avoid re-scanning the same
1322
      // growing prefix for every following comment (quadratic behavior).
1323
      lastValueHasAComment_ = true;
434✔
1324
    }
1325

1326
    addComment(commentBegin, current_, placement);
1,598✔
1327
  }
1328
  return true;
1329
}
1330

1331
String OurReader::normalizeEOL(OurReader::Location begin,
1,598✔
1332
                               OurReader::Location end) {
1333
  String normalized;
1334
  normalized.reserve(static_cast<size_t>(end - begin));
1,598✔
1335
  OurReader::Location current = begin;
1336
  while (current != end) {
28,206✔
1337
    char c = *current++;
26,608✔
1338
    if (c == '\r') {
26,608✔
1339
      if (current != end && *current == '\n')
3!
1340
        // convert dos EOL
1341
        ++current;
1✔
1342
      // convert Mac EOL
1343
      normalized += '\n';
1344
    } else {
1345
      normalized += c;
1346
    }
1347
  }
1348
  return normalized;
1,598✔
1349
}
1350

1351
void OurReader::addComment(Location begin, Location end,
1,598✔
1352
                           CommentPlacement placement) {
1353
  assert(collectComments_);
1,598!
1354
  const String& normalized = normalizeEOL(begin, end);
1,598✔
1355
  if (placement == commentAfterOnSameLine) {
1,598✔
1356
    assert(lastValue_ != nullptr);
64!
1357
    lastValue_->setComment(normalized, placement);
128✔
1358
  } else {
1359
    commentsBefore_ += normalized;
1,534✔
1360
  }
1361
}
1,598✔
1362

1363
bool OurReader::readCStyleComment(bool* containsNewLineResult) {
1,086✔
1364
  *containsNewLineResult = false;
1,086✔
1365

1366
  while ((current_ + 1) < end_) {
6,013!
1367
    Char c = getNextChar();
4,927✔
1368
    if (c == '*' && *current_ == '/')
4,927!
1369
      break;
1370
    if (c == '\n')
3,841✔
1371
      *containsNewLineResult = true;
98✔
1372
  }
1373

1374
  return getNextChar() == '/';
1,086✔
1375
}
1376

1377
bool OurReader::readCppStyleComment() {
530✔
1378
  while (current_ != end_) {
18,083✔
1379
    Char c = getNextChar();
18,077✔
1380
    if (c == '\n')
18,077✔
1381
      break;
1382
    if (c == '\r') {
17,556✔
1383
      // Consume DOS EOL. It will be normalized in addComment.
1384
      if (current_ != end_ && *current_ == '\n')
3!
1385
        getNextChar();
1✔
1386
      // Break on Moc OS 9 EOL.
1387
      break;
1388
    }
1389
  }
1390
  return true;
530✔
1391
}
1392

1393
bool OurReader::readNumber(bool checkInf) {
52,883✔
1394
  Location p = current_;
52,883✔
1395
  if (checkInf && p != end_ && *p == 'I') {
52,883!
1396
    current_ = ++p;
6✔
1397
    return false;
6✔
1398
  }
1399
  char c = '0'; // stopgap for already consumed character
1400
  // integral part
1401
  while (c >= '0' && c <= '9')
235,533✔
1402
    c = (current_ = p) < end_ ? *p++ : '\0';
182,656✔
1403
  // fractional part
1404
  if (c == '.') {
52,877✔
1405
    c = (current_ = p) < end_ ? *p++ : '\0';
116!
1406
    while (c >= '0' && c <= '9')
809✔
1407
      c = (current_ = p) < end_ ? *p++ : '\0';
693✔
1408
  }
1409
  // exponential part
1410
  if (c == 'e' || c == 'E') {
52,877✔
1411
    c = (current_ = p) < end_ ? *p++ : '\0';
109!
1412
    if (c == '+' || c == '-')
109✔
1413
      c = (current_ = p) < end_ ? *p++ : '\0';
81!
1414
    while (c >= '0' && c <= '9')
324✔
1415
      c = (current_ = p) < end_ ? *p++ : '\0';
215✔
1416
  }
1417
  return true;
1418
}
1419
bool OurReader::readString() {
1,174✔
1420
  Char c = 0;
1421
  while (current_ != end_) {
34,521!
1422
    c = getNextChar();
34,521✔
1423
    if (c == '\\')
34,521✔
1424
      getNextChar();
1,275✔
1425
    else if (c == '"')
33,246✔
1426
      break;
1427
  }
1428
  return c == '"';
1,174✔
1429
}
1430

1431
bool OurReader::readStringSingleQuote() {
11✔
1432
  Char c = 0;
1433
  while (current_ != end_) {
26!
1434
    c = getNextChar();
26✔
1435
    if (c == '\\')
26✔
1436
      getNextChar();
2✔
1437
    else if (c == '\'')
24✔
1438
      break;
1439
  }
1440
  return c == '\'';
11✔
1441
}
1442

1443
bool OurReader::readObject(Token& token) {
367✔
1444
  Token tokenName;
1445
  String name;
1446
  Value init(objectValue);
367✔
1447
  currentValue().swapPayload(init);
367✔
1448
  currentValue().setOffsetStart(token.start_ - begin_);
367✔
1449
  while (readTokenSkippingComments(tokenName)) {
691✔
1450
    if (tokenName.type_ == tokenObjectEnd &&
682✔
1451
        (name.empty() ||
6✔
1452
         features_.allowTrailingCommas_)) // empty object or trailing comma
6!
1453
      return true;
340✔
1454
    name.clear();
1455
    if (tokenName.type_ == tokenString) {
657✔
1456
      if (!decodeString(tokenName, name))
637!
1457
        return recoverFromError(tokenObjectEnd);
×
1458
    } else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
20✔
1459
      Value numberName;
3✔
1460
      if (!decodeNumber(tokenName, numberName))
3!
1461
        return recoverFromError(tokenObjectEnd);
×
1462
      name = numberName.asString();
3✔
1463
    } else {
3✔
1464
      break;
1465
    }
1466
    if (name.length() >= (1U << 30))
640!
1467
      throwRuntimeError("keylength >= 2^30");
×
1468
    if (features_.rejectDupKeys_ && currentValue().isMember(name)) {
640✔
1469
      String msg = "Duplicate key: '" + name + "'";
1✔
1470
      return addErrorAndRecover(msg, tokenName, tokenObjectEnd);
1✔
1471
    }
1472

1473
    Token colon;
1474
    if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
639!
1475
      return addErrorAndRecover("Missing ':' after object member name", colon,
14✔
1476
                                tokenObjectEnd);
1477
    }
1478
    Value& value = currentValue()[name];
632✔
1479
    nodes_.push(&value);
632!
1480
    bool ok = readValue();
632✔
1481
    nodes_.pop();
631✔
1482
    if (!ok) // error already set
631✔
1483
      return recoverFromError(tokenObjectEnd);
22✔
1484

1485
    Token comma;
1486
    if (!readTokenSkippingComments(comma) ||
609✔
1487
        (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator)) {
601✔
1488
      return addErrorAndRecover("Missing ',' or '}' in object declaration",
54✔
1489
                                comma, tokenObjectEnd);
1490
    }
1491
    if (comma.type_ == tokenObjectEnd)
582✔
1492
      return true;
1493
  }
1494
  return addErrorAndRecover("Missing '}' or object member name", tokenName,
52✔
1495
                            tokenObjectEnd);
1496
}
367✔
1497

1498
bool OurReader::readArray(Token& token) {
2,158✔
1499
  Value init(arrayValue);
2,158✔
1500
  currentValue().swapPayload(init);
2,158✔
1501
  currentValue().setOffsetStart(token.start_ - begin_);
2,158✔
1502
  int index = 0;
1503
  for (;;) {
1504
    skipSpaces();
54,564✔
1505
    if (current_ != end_ && *current_ == ']' &&
54,564!
1506
        (index == 0 ||
14✔
1507
         (features_.allowTrailingCommas_ &&
14!
1508
          !features_.allowDroppedNullPlaceholders_))) // empty array or trailing
14✔
1509
                                                      // comma
1510
    {
1511
      Token endArray;
1512
      readToken(endArray);
28✔
1513
      return true;
1514
    }
1515
    Value& value = currentValue()[index++];
54,536✔
1516
    nodes_.push(&value);
54,536✔
1517
    bool ok = readValue();
54,536✔
1518
    nodes_.pop();
52,744✔
1519
    if (!ok) // error already set
52,744✔
1520
      return recoverFromError(tokenArrayEnd);
48✔
1521

1522
    Token currentToken;
1523
    // Accept Comment after last item in the array.
1524
    ok = readTokenSkippingComments(currentToken);
52,696✔
1525
    bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
52,696✔
1526
                         currentToken.type_ != tokenArrayEnd);
52,696✔
1527
    if (!ok || badTokenType) {
52,696✔
1528
      return addErrorAndRecover("Missing ',' or ']' in array declaration",
48✔
1529
                                currentToken, tokenArrayEnd);
1530
    }
1531
    if (currentToken.type_ == tokenArrayEnd)
52,672✔
1532
      break;
1533
  }
52,406✔
1534
  return true;
266✔
1535
}
2,158✔
1536

1537
bool OurReader::decodeNumber(Token& token) {
52,842✔
1538
  Value decoded;
52,842✔
1539
  if (!decodeNumber(token, decoded))
52,842✔
1540
    return false;
1541
  currentValue().swapPayload(decoded);
52,831✔
1542
  currentValue().setOffsetStart(token.start_ - begin_);
52,831✔
1543
  currentValue().setOffsetLimit(token.end_ - begin_);
52,831✔
1544
  return true;
1545
}
52,842✔
1546

1547
bool OurReader::decodeNumber(Token& token, Value& decoded) {
52,845✔
1548
  // Attempts to parse the number as an integer. If the number is
1549
  // larger than the maximum supported value of an integer then
1550
  // we decode the number as a double.
1551
  Location current = token.start_;
52,845✔
1552
  const bool isNegative = *current == '-';
52,845✔
1553
  if (isNegative) {
52,845✔
1554
    ++current;
142✔
1555
  }
1556

1557
  // We assume we can represent the largest and smallest integer types as
1558
  // unsigned integers with separate sign. This is only true if they can fit
1559
  // into an unsigned integer.
1560
  static_assert(Value::maxLargestInt <= Value::maxLargestUInt,
1561
                "Int must be smaller than UInt");
1562

1563
  // We need to convert minLargestInt into a positive number. The easiest way
1564
  // to do this conversion is to assume our "threshold" value of minLargestInt
1565
  // divided by 10 can fit in maxLargestInt when absolute valued. This should
1566
  // be a safe assumption.
1567
  static_assert(Value::minLargestInt <= -Value::maxLargestInt,
1568
                "The absolute value of minLargestInt must be greater than or "
1569
                "equal to maxLargestInt");
1570
  static_assert(Value::minLargestInt / 10 >= -Value::maxLargestInt,
1571
                "The absolute value of minLargestInt must be only 1 magnitude "
1572
                "larger than maxLargest Int");
1573

1574
  static constexpr Value::LargestUInt positive_threshold =
1575
      Value::maxLargestUInt / 10;
1576
  static constexpr Value::UInt positive_last_digit = Value::maxLargestUInt % 10;
1577

1578
  // For the negative values, we have to be more careful. Since typically
1579
  // -Value::minLargestInt will cause an overflow, we first divide by 10 and
1580
  // then take the inverse. This assumes that minLargestInt is only a single
1581
  // power of 10 different in magnitude, which we check above. For the last
1582
  // digit, we take the modulus before negating for the same reason.
1583
  static constexpr auto negative_threshold =
1584
      Value::LargestUInt(-(Value::minLargestInt / 10));
1585
  static constexpr auto negative_last_digit =
1586
      Value::UInt(-(Value::minLargestInt % 10));
1587

1588
  const Value::LargestUInt threshold =
1589
      isNegative ? negative_threshold : positive_threshold;
1590
  const Value::UInt max_last_digit =
1591
      isNegative ? negative_last_digit : positive_last_digit;
1592

1593
  Value::LargestUInt value = 0;
1594
  while (current < token.end_) {
235,298✔
1595
    Char c = *current++;
182,655✔
1596
    if (c < '0' || c > '9')
182,655✔
1597
      return decodeDouble(token, decoded);
183✔
1598

1599
    const auto digit(static_cast<Value::UInt>(c - '0'));
182,472✔
1600
    if (value >= threshold) {
182,472✔
1601
      // We've hit or exceeded the max value divided by 10 (rounded down). If
1602
      // a) we've only just touched the limit, meaning value == threshold,
1603
      // b) this is the last digit, or
1604
      // c) it's small enough to fit in that rounding delta, we're okay.
1605
      // Otherwise treat this number as a double to avoid overflow.
1606
      if (value > threshold || current != token.end_ ||
43!
1607
          digit > max_last_digit) {
1608
        return decodeDouble(token, decoded);
19✔
1609
      }
1610
    }
1611
    value = value * 10 + digit;
182,453✔
1612
  }
1613

1614
  if (isNegative) {
52,643✔
1615
    // We use the same magnitude assumption here, just in case.
1616
    const auto last_digit = static_cast<Value::UInt>(value % 10);
89✔
1617
    decoded = -Value::LargestInt(value / 10) * 10 - last_digit;
89✔
1618
  } else if (value <= Value::LargestUInt(Value::maxLargestInt)) {
52,554✔
1619
    decoded = Value::LargestInt(value);
52,530✔
1620
  } else {
1621
    decoded = value;
24✔
1622
  }
1623

1624
  return true;
1625
}
1626

1627
bool OurReader::decodeDouble(Token& token) {
×
1628
  Value decoded;
×
1629
  if (!decodeDouble(token, decoded))
×
1630
    return false;
1631
  currentValue().swapPayload(decoded);
×
1632
  currentValue().setOffsetStart(token.start_ - begin_);
×
1633
  currentValue().setOffsetLimit(token.end_ - begin_);
×
1634
  return true;
1635
}
×
1636

1637
bool OurReader::decodeDouble(Token& token, Value& decoded) {
202✔
1638
  double value = 0;
202✔
1639
  IStringStream is(String(token.start_, token.end_));
202✔
1640
  is.imbue(std::locale::classic());
202✔
1641
  if (!(is >> value)) {
202✔
1642
    if (value == std::numeric_limits<double>::max())
35✔
1643
      value = std::numeric_limits<double>::infinity();
12✔
1644
    else if (value == std::numeric_limits<double>::lowest())
23✔
1645
      value = -std::numeric_limits<double>::infinity();
12✔
1646
    // operator>> sets failbit for a subnormal result (underflow) even though
1647
    // it produced the correctly-rounded value, which made such numbers fail to
1648
    // parse back after jsoncpp serialized them. Keep a subnormal value instead
1649
    // of rejecting it. See issue #1427. Other failures -- malformed numbers
1650
    // like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite
1651
    // and are still rejected.
1652
    else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL)
11!
1653
      return addError(
11✔
1654
          "'" + String(token.start_, token.end_) + "' is not a number.", token);
22✔
1655
  }
1656
  decoded = value;
191✔
1657
  return true;
191✔
1658
}
202✔
1659

1660
bool OurReader::decodeString(Token& token) {
519✔
1661
  String decoded_string;
1662
  if (!decodeString(token, decoded_string))
519✔
1663
    return false;
1664
  Value decoded(decoded_string);
496✔
1665
  currentValue().swapPayload(decoded);
496✔
1666
  currentValue().setOffsetStart(token.start_ - begin_);
496✔
1667
  currentValue().setOffsetLimit(token.end_ - begin_);
496✔
1668
  return true;
1669
}
496✔
1670

1671
bool OurReader::decodeString(Token& token, String& decoded) {
1,156✔
1672
  decoded.reserve(static_cast<size_t>(token.end_ - token.start_ - 2));
1,156✔
1673
  Location current = token.start_ + 1; // skip '"'
1,156✔
1674
  Location end = token.end_ - 1;       // do not include '"'
1,156✔
1675
  while (current != end) {
33,563✔
1676
    Char c = *current++;
32,430✔
1677
    if (c == '"')
32,430!
1678
      break;
1679
    if (c == '\\') {
32,430✔
1680
      if (current == end)
1,254!
1681
        return addError("Empty escape sequence in string", token, current);
×
1682
      Char escape = *current++;
1,254✔
1683
      switch (escape) {
1,254✔
1684
      case '"':
1685
        decoded += '"';
1686
        break;
1687
      case '/':
1688
        decoded += '/';
1689
        break;
1690
      case '\\':
1691
        decoded += '\\';
1692
        break;
1693
      case 'b':
1694
        decoded += '\b';
1695
        break;
1696
      case 'f':
1697
        decoded += '\f';
1698
        break;
1699
      case 'n':
1700
        decoded += '\n';
1701
        break;
1702
      case 'r':
1703
        decoded += '\r';
1704
        break;
1705
      case 't':
1706
        decoded += '\t';
1707
        break;
1708
      case 'u': {
145✔
1709
        unsigned int unicode;
1710
        if (!decodeUnicodeCodePoint(token, current, end, unicode))
145✔
1711
          return false;
4✔
1712
        decoded += codePointToUTF8(unicode);
141✔
1713
      } break;
141✔
1714
      default:
13✔
1715
        return addError("Bad escape sequence in string", token, current);
26✔
1716
      }
1717
    } else {
1718
      if (static_cast<unsigned char>(c) < 0x20)
31,176✔
1719
        return addError("Control character in string", token, current - 1);
12✔
1720
      decoded += c;
1721
    }
1722
  }
1723
  return true;
1724
}
1725

1726
bool OurReader::decodeUnicodeCodePoint(Token& token, Location& current,
145✔
1727
                                       Location end, unsigned int& unicode) {
1728

1729
  if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
145✔
1730
    return false;
1731
  if (unicode >= 0xD800 && unicode <= 0xDBFF) {
143✔
1732
    // surrogate pairs
1733
    if (end - current < 6)
15✔
1734
      return addError(
2✔
1735
          "additional six characters expected to parse unicode surrogate pair.",
1736
          token, current);
1737
    if (*(current++) == '\\' && *(current++) == 'u') {
14!
1738
      unsigned int surrogatePair;
1739
      if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
13!
1740
        unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
13✔
1741
      } else
1742
        return false;
×
1743
    } else
1744
      return addError("expecting another \\u token to begin the second half of "
2✔
1745
                      "a unicode surrogate pair",
1746
                      token, current);
1747
  }
1748
  return true;
1749
}
1750

1751
bool OurReader::decodeUnicodeEscapeSequence(Token& token, Location& current,
158✔
1752
                                            Location end,
1753
                                            unsigned int& ret_unicode) {
1754
  if (end - current < 4)
158✔
1755
    return addError(
2✔
1756
        "Bad unicode escape sequence in string: four digits expected.", token,
1757
        current);
1758
  int unicode = 0;
1759
  for (int index = 0; index < 4; ++index) {
783✔
1760
    Char c = *current++;
627✔
1761
    unicode *= 16;
627✔
1762
    if (c >= '0' && c <= '9')
627✔
1763
      unicode += c - '0';
400✔
1764
    else if (c >= 'a' && c <= 'f')
227✔
1765
      unicode += c - 'a' + 10;
115✔
1766
    else if (c >= 'A' && c <= 'F')
112✔
1767
      unicode += c - 'A' + 10;
111✔
1768
    else
1769
      return addError(
2✔
1770
          "Bad unicode escape sequence in string: hexadecimal digit expected.",
1771
          token, current);
1772
  }
1773
  ret_unicode = static_cast<unsigned int>(unicode);
156✔
1774
  return true;
156✔
1775
}
1776

1777
bool OurReader::addError(const String& message, Token& token, Location extra) {
165✔
1778
  ErrorInfo info;
1779
  info.token_ = token;
165✔
1780
  info.message_ = message;
1781
  info.extra_ = extra;
165✔
1782
  errors_.push_back(info);
165✔
1783
  return false;
165✔
1784
}
1785

1786
bool OurReader::recoverFromError(TokenType skipUntilToken) {
155✔
1787
  size_t errorCount = errors_.size();
1788
  Token skip;
1789
  for (;;) {
1790
    if (!readToken(skip))
415✔
1791
      errors_.resize(errorCount); // discard errors caused by recovery
175✔
1792
    if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
415✔
1793
      break;
1794
  }
1795
  errors_.resize(errorCount);
155✔
1796
  return false;
155✔
1797
}
1798

1799
bool OurReader::addErrorAndRecover(const String& message, Token& token,
85✔
1800
                                   TokenType skipUntilToken) {
1801
  addError(message, token);
85✔
1802
  return recoverFromError(skipUntilToken);
85✔
1803
}
1804

1805
Value& OurReader::currentValue() { return *(nodes_.top()); }
275,753✔
1806

1807
OurReader::Char OurReader::getNextChar() {
175,234✔
1808
  if (current_ == end_)
175,234✔
1809
    return 0;
1810
  return *current_++;
174,282✔
1811
}
1812

1813
void OurReader::getLocationLineAndColumn(Location location, int& line,
187✔
1814
                                         int& column) const {
1815
  Location current = begin_;
187✔
1816
  Location lastLineStart = current;
1817
  line = 0;
187✔
1818
  while (current < location && current != end_) {
2,278!
1819
    Char c = *current++;
2,091✔
1820
    if (c == '\r') {
2,091✔
1821
      if (current != end_ && *current == '\n')
1!
1822
        ++current;
×
1823
      lastLineStart = current;
1824
      ++line;
1✔
1825
    } else if (c == '\n') {
2,090✔
1826
      lastLineStart = current;
1827
      ++line;
28✔
1828
    }
1829
  }
1830
  // column & line start at 1
1831
  column = int(location - lastLineStart) + 1;
187✔
1832
  ++line;
187✔
1833
}
187✔
1834

1835
String OurReader::getLocationLineAndColumn(Location location) const {
187✔
1836
  int line, column;
1837
  getLocationLineAndColumn(location, line, column);
187✔
1838
  char buffer[18 + 16 + 16 + 1];
1839
  jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
187✔
1840
  return buffer;
187✔
1841
}
1842

1843
String OurReader::getFormattedErrorMessages() const {
931✔
1844
  String formattedMessage;
1845
  for (const auto& error : errors_) {
1,095✔
1846
    formattedMessage +=
1847
        "* " + getLocationLineAndColumn(error.token_.start_) + "\n";
164✔
1848
    formattedMessage += "  " + error.message_ + "\n";
164✔
1849
    if (error.extra_)
164✔
1850
      formattedMessage +=
1851
          "See " + getLocationLineAndColumn(error.extra_) + " for detail.\n";
46✔
1852
  }
1853
  return formattedMessage;
931✔
1854
}
1855

1856
std::vector<CharReader::StructuredError>
1857
OurReader::getStructuredErrors() const {
2✔
1858
  std::vector<CharReader::StructuredError> allErrors;
1859
  for (const auto& error : errors_) {
3✔
1860
    CharReader::StructuredError structured;
1861
    structured.offset_start = error.token_.start_ - begin_;
1✔
1862
    structured.offset_limit = error.token_.end_ - begin_;
1✔
1863
    structured.message = error.message_;
1✔
1864
    allErrors.push_back(structured);
1✔
1865
  }
1866
  return allErrors;
2✔
1867
}
×
1868

1869
class OurCharReader : public CharReader {
1870

1871
public:
1872
  OurCharReader(bool collectComments, OurFeatures const& features)
903✔
1873
      : CharReader(
903✔
1874
            std::unique_ptr<OurImpl>(new OurImpl(collectComments, features))) {}
1,806✔
1875

1876
protected:
1877
  class OurImpl : public Impl {
1878
  public:
1879
    OurImpl(bool collectComments, OurFeatures const& features)
1880
        : collectComments_(collectComments), reader_(features) {}
903✔
1881

1882
    bool parse(char const* beginDoc, char const* endDoc, Value* root,
941✔
1883
               String* errs) override {
1884
      bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_);
941✔
1885
      if (errs) {
933✔
1886
        *errs = reader_.getFormattedErrorMessages();
1,862✔
1887
      }
1888
      return ok;
933✔
1889
    }
1890

1891
    std::vector<CharReader::StructuredError>
1892
    getStructuredErrors() const override {
2✔
1893
      return reader_.getStructuredErrors();
2✔
1894
    }
1895

1896
  private:
1897
    bool const collectComments_;
1898
    OurReader reader_;
1899
  };
1900
};
1901

1902
CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
900✔
1903
CharReaderBuilder::~CharReaderBuilder() = default;
900✔
1904
CharReader* CharReaderBuilder::newCharReader() const {
903✔
1905
  bool collectComments = settings_["collectComments"].asBool();
903✔
1906
  OurFeatures features = OurFeatures::all();
903✔
1907
  features.allowComments_ = settings_["allowComments"].asBool();
903✔
1908
  features.allowTrailingCommas_ = settings_["allowTrailingCommas"].asBool();
903✔
1909
  features.strictRoot_ = settings_["strictRoot"].asBool();
903✔
1910
  features.allowDroppedNullPlaceholders_ =
903✔
1911
      settings_["allowDroppedNullPlaceholders"].asBool();
903✔
1912
  features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool();
903✔
1913
  features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool();
903✔
1914

1915
  // Stack limit is always a size_t, so we get this as an unsigned int
1916
  // regardless of it we have 64-bit integer support enabled.
1917
  features.stackLimit_ = static_cast<size_t>(settings_["stackLimit"].asUInt());
903✔
1918
  features.failIfExtra_ = settings_["failIfExtra"].asBool();
903✔
1919
  features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
903✔
1920
  features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();
903✔
1921
  features.skipBom_ = settings_["skipBom"].asBool();
903✔
1922
  return new OurCharReader(collectComments, features);
903✔
1923
}
1924

1925
bool CharReaderBuilder::validate(Json::Value* invalid) const {
2✔
1926
  static const auto& valid_keys = *new std::set<String>{
1927
      "collectComments",
1928
      "allowComments",
1929
      "allowTrailingCommas",
1930
      "strictRoot",
1931
      "allowDroppedNullPlaceholders",
1932
      "allowNumericKeys",
1933
      "allowSingleQuotes",
1934
      "stackLimit",
1935
      "failIfExtra",
1936
      "rejectDupKeys",
1937
      "allowSpecialFloats",
1938
      "skipBom",
1939
  };
15!
1940
  for (auto si = settings_.begin(); si != settings_.end(); ++si) {
54✔
1941
    auto key = si.name();
25✔
1942
    if (valid_keys.count(key))
25✔
1943
      continue;
1944
    if (invalid)
1!
1945
      (*invalid)[key] = *si;
1✔
1946
    else
1947
      return false;
1948
  }
1949
  return invalid ? invalid->empty() : true;
2!
1950
}
2!
1951

1952
Value& CharReaderBuilder::operator[](const String& key) {
1✔
1953
  return settings_[key];
1✔
1954
}
1955
// static
1956
void CharReaderBuilder::strictMode(Json::Value* settings) {
3✔
1957
  //! [CharReaderBuilderStrictMode]
1958
  (*settings)["allowComments"] = false;
3✔
1959
  (*settings)["allowTrailingCommas"] = false;
3✔
1960
  (*settings)["strictRoot"] = true;
3✔
1961
  (*settings)["allowDroppedNullPlaceholders"] = false;
3✔
1962
  (*settings)["allowNumericKeys"] = false;
3✔
1963
  (*settings)["allowSingleQuotes"] = false;
3✔
1964
  (*settings)["stackLimit"] = 256;
3✔
1965
  (*settings)["failIfExtra"] = true;
3✔
1966
  (*settings)["rejectDupKeys"] = true;
3✔
1967
  (*settings)["allowSpecialFloats"] = false;
3✔
1968
  (*settings)["skipBom"] = true;
3✔
1969
  //! [CharReaderBuilderStrictMode]
1970
}
3✔
1971
// static
1972
void CharReaderBuilder::setDefaults(Json::Value* settings) {
900✔
1973
  //! [CharReaderBuilderDefaults]
1974
  (*settings)["collectComments"] = true;
900✔
1975
  (*settings)["allowComments"] = true;
900✔
1976
  (*settings)["allowTrailingCommas"] = true;
900✔
1977
  (*settings)["strictRoot"] = false;
900✔
1978
  (*settings)["allowDroppedNullPlaceholders"] = false;
900✔
1979
  (*settings)["allowNumericKeys"] = false;
900✔
1980
  (*settings)["allowSingleQuotes"] = false;
900✔
1981
  (*settings)["stackLimit"] = 256;
900✔
1982
  (*settings)["failIfExtra"] = false;
900✔
1983
  (*settings)["rejectDupKeys"] = false;
900✔
1984
  (*settings)["allowSpecialFloats"] = false;
900✔
1985
  (*settings)["skipBom"] = true;
900✔
1986
  //! [CharReaderBuilderDefaults]
1987
}
900✔
1988
// static
1989
void CharReaderBuilder::ecma404Mode(Json::Value* settings) {
×
1990
  //! [CharReaderBuilderECMA404Mode]
1991
  (*settings)["allowComments"] = false;
×
1992
  (*settings)["allowTrailingCommas"] = false;
×
1993
  (*settings)["strictRoot"] = false;
×
1994
  (*settings)["allowDroppedNullPlaceholders"] = false;
×
1995
  (*settings)["allowNumericKeys"] = false;
×
1996
  (*settings)["allowSingleQuotes"] = false;
×
1997
  (*settings)["stackLimit"] = 256;
×
1998
  (*settings)["failIfExtra"] = true;
×
1999
  (*settings)["rejectDupKeys"] = false;
×
2000
  (*settings)["allowSpecialFloats"] = false;
×
2001
  (*settings)["skipBom"] = false;
×
2002
  //! [CharReaderBuilderECMA404Mode]
2003
}
×
2004

2005
std::vector<CharReader::StructuredError>
2006
CharReader::getStructuredErrors() const {
2✔
2007
  return _impl->getStructuredErrors();
2✔
2008
}
2009

2010
bool CharReader::parse(char const* beginDoc, char const* endDoc, Value* root,
941✔
2011
                       String* errs) {
2012
  return _impl->parse(beginDoc, endDoc, root, errs);
941✔
2013
}
2014

2015
//////////////////////////////////
2016
// global functions
2017

2018
bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
4✔
2019
                     String* errs) {
2020
  OStringStream ssin;
4✔
2021
  ssin << sin.rdbuf();
4✔
2022
  String doc = std::move(ssin).str();
2023
  char const* begin = doc.data();
2024
  char const* end = begin + doc.size();
4✔
2025
  // Note that we do not actually need a null-terminator.
2026
  CharReaderPtr const reader(fact.newCharReader());
4✔
2027
  return reader->parse(begin, end, root, errs);
8✔
2028
}
4✔
2029

2030
IStream& operator>>(IStream& sin, Value& root) {
1✔
2031
  CharReaderBuilder b;
1✔
2032
  String errs;
2033
  bool ok = parseFromStream(b, sin, &root, &errs);
1✔
2034
  if (!ok) {
1!
2035
    throwRuntimeError(errs);
×
2036
  }
2037
  return sin;
1✔
2038
}
1✔
2039

2040
} // namespace Json
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc