Skip to content

Commit e442ab7

Browse files
authored
Merge pull request #3 from doomlaur/Fix_Issue_735
Fix XLNT issue tfussell#735
2 parents 2c59134 + 7e2e3d3 commit e442ab7

File tree

1 file changed

+82
-21
lines changed

1 file changed

+82
-21
lines changed

source/detail/serialization/xlsx_consumer.cpp

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ xlnt::detail::Cell parse_cell(xlnt::row_t row_arg, xml::parser *parser, std::uno
267267
case xml::parser::end_attribute:
268268
case xml::parser::eof:
269269
default: {
270-
throw xlnt::exception("unexcpected XML parsing event");
270+
throw xlnt::exception("unexpected XML parsing event");
271271
}
272272
}
273273
// Prevents unhandled exceptions from being triggered.
@@ -344,7 +344,7 @@ std::pair<xlnt::row_properties, int> parse_row(xml::parser *parser, xlnt::detail
344344
case xml::parser::end_attribute:
345345
case xml::parser::eof:
346346
default: {
347-
throw xlnt::exception("unexcpected XML parsing event");
347+
throw xlnt::exception("unexpected XML parsing event");
348348
}
349349
}
350350
}
@@ -382,7 +382,7 @@ Sheet_Data parse_sheet_data(xml::parser *parser, xlnt::detail::number_serialiser
382382
case xml::parser::end_attribute:
383383
case xml::parser::eof:
384384
default: {
385-
throw xlnt::exception("unexcpected XML parsing event");
385+
throw xlnt::exception("unexpected XML parsing event");
386386
}
387387
}
388388
}
@@ -2386,7 +2386,12 @@ void xlsx_consumer::read_stylesheet()
23862386
if (current_style_element == qn("spreadsheetml", "borders"))
23872387
{
23882388
auto &borders = stylesheet.borders;
2389-
auto count = parser().attribute<std::size_t>("count");
2389+
optional<std::size_t> count;
2390+
if (parser().attribute_present("count"))
2391+
{
2392+
count = parser().attribute<std::size_t>("count");
2393+
borders.reserve(count.get());
2394+
}
23902395

23912396
while (in_element(qn("spreadsheetml", "borders")))
23922397
{
@@ -2439,15 +2444,22 @@ void xlsx_consumer::read_stylesheet()
24392444
expect_end_element(qn("spreadsheetml", "border"));
24402445
}
24412446

2442-
if (count != borders.size())
2447+
#ifdef THROW_ON_INVALID_XML
2448+
if (count.is_set() && count != borders.size())
24432449
{
24442450
throw xlnt::exception("border counts don't match");
24452451
}
2452+
#endif
24462453
}
24472454
else if (current_style_element == qn("spreadsheetml", "fills"))
24482455
{
24492456
auto &fills = stylesheet.fills;
2450-
auto count = parser().attribute<std::size_t>("count");
2457+
optional<std::size_t> count;
2458+
if (parser().attribute_present("count"))
2459+
{
2460+
count = parser().attribute<std::size_t>("count");
2461+
fills.reserve(count.get());
2462+
}
24512463

24522464
while (in_element(qn("spreadsheetml", "fills")))
24532465
{
@@ -2524,15 +2536,22 @@ void xlsx_consumer::read_stylesheet()
25242536
expect_end_element(qn("spreadsheetml", "fill"));
25252537
}
25262538

2527-
if (count != fills.size())
2539+
#ifdef THROW_ON_INVALID_XML
2540+
if (count.is_set() && count != fills.size())
25282541
{
25292542
throw xlnt::exception("counts don't match");
25302543
}
2544+
#endif
25312545
}
25322546
else if (current_style_element == qn("spreadsheetml", "fonts"))
25332547
{
25342548
auto &fonts = stylesheet.fonts;
2535-
auto count = parser().attribute<std::size_t>("count", 0);
2549+
optional<std::size_t> count;
2550+
if (parser().attribute_present("count"))
2551+
{
2552+
count = parser().attribute<std::size_t>("count");
2553+
fonts.reserve(count.get());
2554+
}
25362555

25372556
if (parser().attribute_present(qn("x14ac", "knownFonts")))
25382557
{
@@ -2667,15 +2686,22 @@ void xlsx_consumer::read_stylesheet()
26672686
expect_end_element(qn("spreadsheetml", "font"));
26682687
}
26692688

2670-
if (count != stylesheet.fonts.size())
2689+
#ifdef THROW_ON_INVALID_XML
2690+
if (count.is_set() && count != stylesheet.fonts.size())
26712691
{
2672-
// throw xlnt::exception("counts don't match");
2692+
throw xlnt::exception("counts don't match");
26732693
}
2694+
#endif
26742695
}
26752696
else if (current_style_element == qn("spreadsheetml", "numFmts"))
26762697
{
26772698
auto &number_formats = stylesheet.number_formats;
2678-
auto count = parser().attribute<std::size_t>("count");
2699+
optional<std::size_t> count;
2700+
if (parser().attribute_present("count"))
2701+
{
2702+
count = parser().attribute<std::size_t>("count");
2703+
number_formats.reserve(count.get());
2704+
}
26792705

26802706
while (in_element(qn("spreadsheetml", "numFmts")))
26812707
{
@@ -2698,14 +2724,21 @@ void xlsx_consumer::read_stylesheet()
26982724
number_formats.push_back(nf);
26992725
}
27002726

2701-
if (count != number_formats.size())
2727+
#ifdef THROW_ON_INVALID_XML
2728+
if (count.is_set() && count != number_formats.size())
27022729
{
27032730
throw xlnt::exception("counts don't match");
27042731
}
2732+
#endif
27052733
}
27062734
else if (current_style_element == qn("spreadsheetml", "cellStyles"))
27072735
{
2708-
auto count = parser().attribute<std::size_t>("count");
2736+
optional<std::size_t> count;
2737+
if (parser().attribute_present("count"))
2738+
{
2739+
count = parser().attribute<std::size_t>("count");
2740+
styles.reserve(count.get());
2741+
}
27092742

27102743
while (in_element(qn("spreadsheetml", "cellStyles")))
27112744
{
@@ -2734,16 +2767,30 @@ void xlsx_consumer::read_stylesheet()
27342767
expect_end_element(qn("spreadsheetml", "cellStyle"));
27352768
}
27362769

2737-
if (count != styles.size())
2770+
#ifdef THROW_ON_INVALID_XML
2771+
if (count.is_set() && count != styles.size())
27382772
{
27392773
throw xlnt::exception("counts don't match");
27402774
}
2775+
#endif
27412776
}
27422777
else if (current_style_element == qn("spreadsheetml", "cellStyleXfs")
27432778
|| current_style_element == qn("spreadsheetml", "cellXfs"))
27442779
{
27452780
auto in_style_records = current_style_element.name() == "cellStyleXfs";
2746-
auto count = parser().attribute<std::size_t>("count");
2781+
optional<std::size_t> count;
2782+
if (parser().attribute_present("count"))
2783+
{
2784+
count = parser().attribute<std::size_t>("count");
2785+
if (in_style_records)
2786+
{
2787+
style_records.reserve(count.get());
2788+
}
2789+
else
2790+
{
2791+
format_records.reserve(count.get());
2792+
}
2793+
}
27472794

27482795
while (in_element(current_style_element))
27492796
{
@@ -2872,15 +2919,21 @@ void xlsx_consumer::read_stylesheet()
28722919
expect_end_element(qn("spreadsheetml", "xf"));
28732920
}
28742921

2875-
if ((in_style_records && count != style_records.size())
2876-
|| (!in_style_records && count != format_records.size()))
2922+
#ifdef THROW_ON_INVALID_XML
2923+
if (count.is_set() && ((in_style_records && count != style_records.size())
2924+
|| (!in_style_records && count != format_records.size())))
28772925
{
28782926
throw xlnt::exception("counts don't match");
28792927
}
2928+
#endif
28802929
}
28812930
else if (current_style_element == qn("spreadsheetml", "dxfs"))
28822931
{
2883-
auto count = parser().attribute<std::size_t>("count");
2932+
optional<std::size_t> count;
2933+
if (parser().attribute_present("count"))
2934+
{
2935+
count = parser().attribute<std::size_t>("count");
2936+
}
28842937
std::size_t processed = 0;
28852938

28862939
while (in_element(current_style_element))
@@ -2891,17 +2944,23 @@ void xlsx_consumer::read_stylesheet()
28912944
++processed;
28922945
}
28932946

2894-
if (count != processed)
2947+
#ifdef THROW_ON_INVALID_XML
2948+
if (count.is_set() && count != processed)
28952949
{
28962950
throw xlnt::exception("counts don't match");
28972951
}
2952+
#endif
28982953
}
28992954
else if (current_style_element == qn("spreadsheetml", "tableStyles"))
29002955
{
29012956
skip_attribute("defaultTableStyle");
29022957
skip_attribute("defaultPivotStyle");
29032958

2904-
auto count = parser().attribute<std::size_t>("count");
2959+
optional<std::size_t> count;
2960+
if (parser().attribute_present("count"))
2961+
{
2962+
count = parser().attribute<std::size_t>("count");
2963+
}
29052964
std::size_t processed = 0;
29062965

29072966
while (in_element(qn("spreadsheetml", "tableStyles")))
@@ -2912,10 +2971,12 @@ void xlsx_consumer::read_stylesheet()
29122971
++processed;
29132972
}
29142973

2915-
if (count != processed)
2974+
#ifdef THROW_ON_INVALID_XML
2975+
if (count.is_set() && count != processed)
29162976
{
29172977
throw xlnt::exception("counts don't match");
29182978
}
2979+
#endif
29192980
}
29202981
else if (current_style_element == qn("spreadsheetml", "extLst"))
29212982
{

0 commit comments

Comments
 (0)