@@ -1741,6 +1741,126 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithStackLimit) {
1741
1741
}
1742
1742
}
1743
1743
1744
+ struct CharReaderFailIfExtraTest : JsonTest::TestCase {};
1745
+
1746
+ JSONTEST_FIXTURE (CharReaderFailIfExtraTest, issue164) {
1747
+ // This is interpretted as a string value followed by a colon.
1748
+ Json::CharReaderBuilder b;
1749
+ Json::Value root;
1750
+ char const doc[] =
1751
+ " \" property\" : \" value\" }" ;
1752
+ {
1753
+ b.settings_ [" failIfExtra" ] = false ;
1754
+ Json::CharReader* reader (b.newCharReader ());
1755
+ std::string errs;
1756
+ bool ok = reader->parse (
1757
+ doc, doc + std::strlen (doc),
1758
+ &root, &errs);
1759
+ JSONTEST_ASSERT (ok);
1760
+ JSONTEST_ASSERT (errs == " " );
1761
+ JSONTEST_ASSERT_EQUAL (" property" , root);
1762
+ delete reader;
1763
+ }
1764
+ {
1765
+ b.settings_ [" failIfExtra" ] = true ;
1766
+ Json::CharReader* reader (b.newCharReader ());
1767
+ std::string errs;
1768
+ bool ok = reader->parse (
1769
+ doc, doc + std::strlen (doc),
1770
+ &root, &errs);
1771
+ JSONTEST_ASSERT (!ok);
1772
+ JSONTEST_ASSERT_STRING_EQUAL (errs,
1773
+ " * Line 1, Column 13\n "
1774
+ " Extra non-whitespace after JSON value.\n " );
1775
+ JSONTEST_ASSERT_EQUAL (" property" , root);
1776
+ delete reader;
1777
+ }
1778
+ {
1779
+ b.settings_ [" failIfExtra" ] = false ;
1780
+ b.strictMode (&b.settings_ );
1781
+ Json::CharReader* reader (b.newCharReader ());
1782
+ std::string errs;
1783
+ bool ok = reader->parse (
1784
+ doc, doc + std::strlen (doc),
1785
+ &root, &errs);
1786
+ JSONTEST_ASSERT (!ok);
1787
+ JSONTEST_ASSERT_STRING_EQUAL (errs,
1788
+ " * Line 1, Column 13\n "
1789
+ " Extra non-whitespace after JSON value.\n " );
1790
+ JSONTEST_ASSERT_EQUAL (" property" , root);
1791
+ delete reader;
1792
+ }
1793
+ }
1794
+ JSONTEST_FIXTURE (CharReaderFailIfExtraTest, issue107) {
1795
+ // This is interpretted as an int value followed by a colon.
1796
+ Json::CharReaderBuilder b;
1797
+ Json::Value root;
1798
+ char const doc[] =
1799
+ " 1:2:3" ;
1800
+ b.settings_ [" failIfExtra" ] = true ;
1801
+ Json::CharReader* reader (b.newCharReader ());
1802
+ std::string errs;
1803
+ bool ok = reader->parse (
1804
+ doc, doc + std::strlen (doc),
1805
+ &root, &errs);
1806
+ JSONTEST_ASSERT (!ok);
1807
+ JSONTEST_ASSERT_STRING_EQUAL (
1808
+ " * Line 1, Column 2\n "
1809
+ " Extra non-whitespace after JSON value.\n " ,
1810
+ errs);
1811
+ JSONTEST_ASSERT_EQUAL (1 , root.asInt ());
1812
+ delete reader;
1813
+ }
1814
+ JSONTEST_FIXTURE (CharReaderFailIfExtraTest, commentAfterObject) {
1815
+ Json::CharReaderBuilder b;
1816
+ Json::Value root;
1817
+ {
1818
+ char const doc[] =
1819
+ " { \" property\" : \" value\" } //trailing\n //comment\n " ;
1820
+ b.settings_ [" failIfExtra" ] = true ;
1821
+ Json::CharReader* reader (b.newCharReader ());
1822
+ std::string errs;
1823
+ bool ok = reader->parse (
1824
+ doc, doc + std::strlen (doc),
1825
+ &root, &errs);
1826
+ JSONTEST_ASSERT (ok);
1827
+ JSONTEST_ASSERT_STRING_EQUAL (" " , errs);
1828
+ JSONTEST_ASSERT_EQUAL (" value" , root[" property" ]);
1829
+ delete reader;
1830
+ }
1831
+ }
1832
+ JSONTEST_FIXTURE (CharReaderFailIfExtraTest, commentAfterArray) {
1833
+ Json::CharReaderBuilder b;
1834
+ Json::Value root;
1835
+ char const doc[] =
1836
+ " [ \" property\" , \" value\" ] //trailing\n //comment\n " ;
1837
+ b.settings_ [" failIfExtra" ] = true ;
1838
+ Json::CharReader* reader (b.newCharReader ());
1839
+ std::string errs;
1840
+ bool ok = reader->parse (
1841
+ doc, doc + std::strlen (doc),
1842
+ &root, &errs);
1843
+ JSONTEST_ASSERT (ok);
1844
+ JSONTEST_ASSERT_STRING_EQUAL (" " , errs);
1845
+ JSONTEST_ASSERT_EQUAL (" value" , root[1u ]);
1846
+ delete reader;
1847
+ }
1848
+ JSONTEST_FIXTURE (CharReaderFailIfExtraTest, commentAfterBool) {
1849
+ Json::CharReaderBuilder b;
1850
+ Json::Value root;
1851
+ char const doc[] =
1852
+ " true /*trailing\n comment*/" ;
1853
+ b.settings_ [" failIfExtra" ] = true ;
1854
+ Json::CharReader* reader (b.newCharReader ());
1855
+ std::string errs;
1856
+ bool ok = reader->parse (
1857
+ doc, doc + std::strlen (doc),
1858
+ &root, &errs);
1859
+ JSONTEST_ASSERT (ok);
1860
+ JSONTEST_ASSERT_STRING_EQUAL (" " , errs);
1861
+ JSONTEST_ASSERT_EQUAL (true , root.asBool ());
1862
+ delete reader;
1863
+ }
1744
1864
int main (int argc, const char * argv[]) {
1745
1865
JsonTest::Runner runner;
1746
1866
JSONTEST_REGISTER_FIXTURE (runner, ValueTest, checkNormalizeFloatingPointStr);
@@ -1779,6 +1899,12 @@ int main(int argc, const char* argv[]) {
1779
1899
JSONTEST_REGISTER_FIXTURE (runner, CharReaderTest, parseWithDetailError);
1780
1900
JSONTEST_REGISTER_FIXTURE (runner, CharReaderTest, parseWithStackLimit);
1781
1901
1902
+ JSONTEST_REGISTER_FIXTURE (runner, CharReaderFailIfExtraTest, issue164);
1903
+ JSONTEST_REGISTER_FIXTURE (runner, CharReaderFailIfExtraTest, issue107);
1904
+ JSONTEST_REGISTER_FIXTURE (runner, CharReaderFailIfExtraTest, commentAfterObject);
1905
+ JSONTEST_REGISTER_FIXTURE (runner, CharReaderFailIfExtraTest, commentAfterArray);
1906
+ JSONTEST_REGISTER_FIXTURE (runner, CharReaderFailIfExtraTest, commentAfterBool);
1907
+
1782
1908
JSONTEST_REGISTER_FIXTURE (runner, WriterTest, dropNullPlaceholders);
1783
1909
JSONTEST_REGISTER_FIXTURE (runner, StreamWriterTest, dropNullPlaceholders);
1784
1910
0 commit comments