@@ -267,7 +267,7 @@ xlnt::detail::Cell parse_cell(xlnt::row_t row_arg, xml::parser *parser, std::uno
267
267
case xml::parser::end_attribute:
268
268
case xml::parser::eof:
269
269
default : {
270
- throw xlnt::exception (" unexcpected XML parsing event" );
270
+ throw xlnt::exception (" unexpected XML parsing event" );
271
271
}
272
272
}
273
273
// Prevents unhandled exceptions from being triggered.
@@ -344,7 +344,7 @@ std::pair<xlnt::row_properties, int> parse_row(xml::parser *parser, xlnt::detail
344
344
case xml::parser::end_attribute:
345
345
case xml::parser::eof:
346
346
default : {
347
- throw xlnt::exception (" unexcpected XML parsing event" );
347
+ throw xlnt::exception (" unexpected XML parsing event" );
348
348
}
349
349
}
350
350
}
@@ -382,7 +382,7 @@ Sheet_Data parse_sheet_data(xml::parser *parser, xlnt::detail::number_serialiser
382
382
case xml::parser::end_attribute:
383
383
case xml::parser::eof:
384
384
default : {
385
- throw xlnt::exception (" unexcpected XML parsing event" );
385
+ throw xlnt::exception (" unexpected XML parsing event" );
386
386
}
387
387
}
388
388
}
@@ -2386,7 +2386,12 @@ void xlsx_consumer::read_stylesheet()
2386
2386
if (current_style_element == qn (" spreadsheetml" , " borders" ))
2387
2387
{
2388
2388
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
+ }
2390
2395
2391
2396
while (in_element (qn (" spreadsheetml" , " borders" )))
2392
2397
{
@@ -2439,15 +2444,22 @@ void xlsx_consumer::read_stylesheet()
2439
2444
expect_end_element (qn (" spreadsheetml" , " border" ));
2440
2445
}
2441
2446
2442
- if (count != borders.size ())
2447
+ #ifdef THROW_ON_INVALID_XML
2448
+ if (count.is_set () && count != borders.size ())
2443
2449
{
2444
2450
throw xlnt::exception (" border counts don't match" );
2445
2451
}
2452
+ #endif
2446
2453
}
2447
2454
else if (current_style_element == qn (" spreadsheetml" , " fills" ))
2448
2455
{
2449
2456
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
+ }
2451
2463
2452
2464
while (in_element (qn (" spreadsheetml" , " fills" )))
2453
2465
{
@@ -2524,15 +2536,22 @@ void xlsx_consumer::read_stylesheet()
2524
2536
expect_end_element (qn (" spreadsheetml" , " fill" ));
2525
2537
}
2526
2538
2527
- if (count != fills.size ())
2539
+ #ifdef THROW_ON_INVALID_XML
2540
+ if (count.is_set () && count != fills.size ())
2528
2541
{
2529
2542
throw xlnt::exception (" counts don't match" );
2530
2543
}
2544
+ #endif
2531
2545
}
2532
2546
else if (current_style_element == qn (" spreadsheetml" , " fonts" ))
2533
2547
{
2534
2548
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
+ }
2536
2555
2537
2556
if (parser ().attribute_present (qn (" x14ac" , " knownFonts" )))
2538
2557
{
@@ -2667,15 +2686,22 @@ void xlsx_consumer::read_stylesheet()
2667
2686
expect_end_element (qn (" spreadsheetml" , " font" ));
2668
2687
}
2669
2688
2670
- if (count != stylesheet.fonts .size ())
2689
+ #ifdef THROW_ON_INVALID_XML
2690
+ if (count.is_set () && count != stylesheet.fonts .size ())
2671
2691
{
2672
- // throw xlnt::exception("counts don't match");
2692
+ throw xlnt::exception (" counts don't match" );
2673
2693
}
2694
+ #endif
2674
2695
}
2675
2696
else if (current_style_element == qn (" spreadsheetml" , " numFmts" ))
2676
2697
{
2677
2698
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
+ }
2679
2705
2680
2706
while (in_element (qn (" spreadsheetml" , " numFmts" )))
2681
2707
{
@@ -2698,14 +2724,21 @@ void xlsx_consumer::read_stylesheet()
2698
2724
number_formats.push_back (nf);
2699
2725
}
2700
2726
2701
- if (count != number_formats.size ())
2727
+ #ifdef THROW_ON_INVALID_XML
2728
+ if (count.is_set () && count != number_formats.size ())
2702
2729
{
2703
2730
throw xlnt::exception (" counts don't match" );
2704
2731
}
2732
+ #endif
2705
2733
}
2706
2734
else if (current_style_element == qn (" spreadsheetml" , " cellStyles" ))
2707
2735
{
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
+ }
2709
2742
2710
2743
while (in_element (qn (" spreadsheetml" , " cellStyles" )))
2711
2744
{
@@ -2734,16 +2767,30 @@ void xlsx_consumer::read_stylesheet()
2734
2767
expect_end_element (qn (" spreadsheetml" , " cellStyle" ));
2735
2768
}
2736
2769
2737
- if (count != styles.size ())
2770
+ #ifdef THROW_ON_INVALID_XML
2771
+ if (count.is_set () && count != styles.size ())
2738
2772
{
2739
2773
throw xlnt::exception (" counts don't match" );
2740
2774
}
2775
+ #endif
2741
2776
}
2742
2777
else if (current_style_element == qn (" spreadsheetml" , " cellStyleXfs" )
2743
2778
|| current_style_element == qn (" spreadsheetml" , " cellXfs" ))
2744
2779
{
2745
2780
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
+ }
2747
2794
2748
2795
while (in_element (current_style_element))
2749
2796
{
@@ -2872,15 +2919,21 @@ void xlsx_consumer::read_stylesheet()
2872
2919
expect_end_element (qn (" spreadsheetml" , " xf" ));
2873
2920
}
2874
2921
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 ())))
2877
2925
{
2878
2926
throw xlnt::exception (" counts don't match" );
2879
2927
}
2928
+ #endif
2880
2929
}
2881
2930
else if (current_style_element == qn (" spreadsheetml" , " dxfs" ))
2882
2931
{
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
+ }
2884
2937
std::size_t processed = 0 ;
2885
2938
2886
2939
while (in_element (current_style_element))
@@ -2891,17 +2944,23 @@ void xlsx_consumer::read_stylesheet()
2891
2944
++processed;
2892
2945
}
2893
2946
2894
- if (count != processed)
2947
+ #ifdef THROW_ON_INVALID_XML
2948
+ if (count.is_set () && count != processed)
2895
2949
{
2896
2950
throw xlnt::exception (" counts don't match" );
2897
2951
}
2952
+ #endif
2898
2953
}
2899
2954
else if (current_style_element == qn (" spreadsheetml" , " tableStyles" ))
2900
2955
{
2901
2956
skip_attribute (" defaultTableStyle" );
2902
2957
skip_attribute (" defaultPivotStyle" );
2903
2958
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
+ }
2905
2964
std::size_t processed = 0 ;
2906
2965
2907
2966
while (in_element (qn (" spreadsheetml" , " tableStyles" )))
@@ -2912,10 +2971,12 @@ void xlsx_consumer::read_stylesheet()
2912
2971
++processed;
2913
2972
}
2914
2973
2915
- if (count != processed)
2974
+ #ifdef THROW_ON_INVALID_XML
2975
+ if (count.is_set () && count != processed)
2916
2976
{
2917
2977
throw xlnt::exception (" counts don't match" );
2918
2978
}
2979
+ #endif
2919
2980
}
2920
2981
else if (current_style_element == qn (" spreadsheetml" , " extLst" ))
2921
2982
{
0 commit comments