Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid exceptions when count does not match the number of elements #1

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions source/detail/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ struct XLNT_API constants
static const column_t max_column();

/// <summary>
/// Returns the maximum amount of elements that functions like std::vector::reserve are allowed to allocate.
/// Information like a "count" is often saved in XLSX files and can be used by std::vector::reserve to
/// allocate the memory right away and thus improve performance. However, malicious or broken files
/// Returns the maximum amount of elements that functions like std::vector::reserve (or other containers) are allowed to allocate.
/// Information like a "count" is often saved in XLSX files and can be used by std::vector::reserve (or other containers)
/// to allocate the memory right away and thus improve performance. However, malicious or broken files
/// might then cause XLNT to allocate extreme amounts of memory. This function sets a limit to protect against such issues.
/// </summary>
static const size_t max_elements_for_reserve();
Expand Down
44 changes: 44 additions & 0 deletions source/detail/limits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2014-2021 Thomas Fussell
m7913d marked this conversation as resolved.
Show resolved Hide resolved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file

#pragma once

#include "constants.hpp"

namespace xlnt {
namespace detail {

/// <summary>
/// Clips the maximum number of reserved elements to a certain upper limit.
/// Information like a "count" is often saved in XLSX files and can be used by std::vector::reserve (or other containers)
/// to allocate the memory right away and thus improve performance. However, malicious or broken files
/// might then cause XLNT to allocate extreme amounts of memory. This function clips the number of elements
/// to an upper limit to protect against such issues, but still allow the caller to pre-allocate memory.
/// </summary>
inline size_t clip_reserve_elements(size_t num_elements)
{
return std::min(num_elements, xlnt::constants::max_elements_for_reserve());
}

} // namespace detail
} // namespace xlnt
41 changes: 12 additions & 29 deletions source/detail/serialization/xlsx_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <detail/serialization/vector_streambuf.hpp>
#include <detail/serialization/xlsx_consumer.hpp>
#include <detail/serialization/zstream.hpp>
#include <detail/limits.hpp>

namespace {
/// string_equal
Expand Down Expand Up @@ -2323,10 +2324,7 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
{
borders.reserve(count.get());
}
borders.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}

while (in_element(qn("spreadsheetml", "borders")))
Expand Down Expand Up @@ -2394,10 +2392,7 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
{
fills.reserve(count.get());
}
fills.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}

while (in_element(qn("spreadsheetml", "fills")))
Expand Down Expand Up @@ -2489,10 +2484,7 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
{
fonts.reserve(count.get());
}
fonts.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}

if (parser().attribute_present(qn("x14ac", "knownFonts")))
Expand Down Expand Up @@ -2642,10 +2634,7 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
{
number_formats.reserve(count.get());
}
number_formats.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}

while (in_element(qn("spreadsheetml", "numFmts")))
Expand Down Expand Up @@ -2682,10 +2671,7 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
{
styles.reserve(count.get());
}
styles.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}

while (in_element(qn("spreadsheetml", "cellStyles")))
Expand Down Expand Up @@ -2730,16 +2716,13 @@ void xlsx_consumer::read_stylesheet()
if (parser().attribute_present("count"))
{
count = parser().attribute<std::size_t>("count");
if (count.get() <= xlnt::constants::max_elements_for_reserve())
if (in_style_records)
{
if (in_style_records)
{
style_records.reserve(count.get());
}
else
{
format_records.reserve(count.get());
}
style_records.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}
else
{
format_records.reserve(xlnt::detail::clip_reserve_elements(count.get()));
}
}

Expand Down