Avoid overflow in ScColumn::GetOptimalColWidth
With -fsanitize=float-cast-overflow, opening csv/fdo40680-1.csv as obtained by
bin/get-bugzilla-attachments-by-mimetype (i.e., the attachment at
<https://bugs.documentfoundation.org/show_bug.cgi?id=40680#c0>) fails with
> sc/source/core/data/column2.cxx:741:53: runtime error: 833163 is outside the range of representable values of type 'unsigned short'
> #0 in ScColumn::GetOptimalColWidth(OutputDevice*, double, double, Fraction const&, Fraction const&, bool, unsigned short, ScMarkData const*, ScColWidthParam const*) const at sc/source/core/data/column2.cxx:741:53 (instdir/program/../program/libsclo.so +0xc9374aa)
> #1 in ScTable::GetOptimalColWidth(short, OutputDevice*, double, double, Fraction const&, Fraction const&, bool, ScMarkData const*, ScColWidthParam const*) at sc/source/core/data/table1.cxx:427:23 (instdir/program/../program/libsclo.so +0xe02deee)
> #2 in ScDocument::GetOptimalColWidth(short, short, OutputDevice*, double, double, Fraction const&, Fraction const&, bool, ScMarkData const*, ScColWidthParam const*) at sc/source/core/data/document.cxx:4242:30 (instdir/program/../program/libsclo.so +0xd18b457)
> #3 in ScDocShell::ConvertFrom(SfxMedium&) at sc/source/ui/docshell/docsh.cxx:1614:53 (instdir/program/../program/libsclo.so +0x110b9ad4)
> #4 in SfxObjectShell::DoLoad(SfxMedium*) at sfx2/source/doc/objstor.cxx:768:23 (instdir/program/libsfxlo.so +0x49d934a)
[...]
where nWidth = 5336 and nPPTX = 0.0647688. Given csv/fdo40680-1.csv has rather
much text in the 17th column, these values do not look completely implausible
---which of course begs the question whether sal_uInt16 is an appropriate data
type here.
But assuming sal_uInt16 is a useful choice, just clamp the calculated width
accordingly.
Change-Id: I9629b45183354d148d572a0850314c9a00548311
Reviewed-on: https://gerrit.libreoffice.org/73273
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
diff --git a/sc/source/core/data/column2.cxx b/sc/source/core/data/column2.cxx
index a4b52ee..a295055 100644
--- a/sc/source/core/data/column2.cxx
+++ b/sc/source/core/data/column2.cxx
@@ -60,6 +60,7 @@
#include <formula/vectortoken.hxx>
#include <algorithm>
#include <limits>
#include <memory>
#include <numeric>
@@ -738,7 +739,8 @@ sal_uInt16 ScColumn::GetOptimalColWidth(
if (bFound)
{
nWidth += 2;
sal_uInt16 nTwips = static_cast<sal_uInt16>(nWidth / nPPTX);
sal_uInt16 nTwips = static_cast<sal_uInt16>(
std::min(nWidth / nPPTX, double(std::numeric_limits<sal_uInt16>::max())));
return nTwips;
}
else