Fix format::number_format

In case number_format() is called on a format that does not have
format ID set, number_format() dereferences
d_->parent->number_formats.end() causing a crash.
This might happen when calling cell::is_date() on a cell that has type
'number' without a number format but some other styling set.
This might never occur in spreadsheet files created by commercial
applications but xlnt certainly allows to create such a file.
We therefore fix number_format here by returning the general number
format as a fallback.
This commit is contained in:
Markus Färber 2022-05-10 14:50:20 +02:00
parent d88c901faa
commit cef67bdc45

View File

@ -124,13 +124,28 @@ format format::font(const xlnt::font &new_font, optional<bool> applied)
xlnt::number_format format::number_format() const xlnt::number_format format::number_format() const
{ {
if (number_format::is_builtin_format(d_->number_format_id.get())) if (d_->number_format_id.is_set())
{ {
return number_format::from_builtin_id(d_->number_format_id.get()); const auto number_format_id = d_->number_format_id.get();
if (number_format::is_builtin_format(number_format_id))
{
return number_format::from_builtin_id(number_format_id);
} }
return *std::find_if(d_->parent->number_formats.begin(), d_->parent->number_formats.end(), const auto it = std::find_if(d_->parent->number_formats.begin(),
[&](const xlnt::number_format nf) { return nf.id() == d_->number_format_id.get(); }); d_->parent->number_formats.end(),
[number_format_id](const xlnt::number_format &nf)
{
return nf.id() == number_format_id;
});
if (it != d_->parent->number_formats.end())
{
return *it;
}
}
return xlnt::number_format();
} }
format format::number_format(const xlnt::number_format &new_number_format, optional<bool> applied) format format::number_format(const xlnt::number_format &new_number_format, optional<bool> applied)