tdf#130725: use strtod by David M. Gay to make sure we get the nearest

... representation of given decimal.
Use dtoa.c from https://www.netlib.org/fp/dtoa.c to build a custom
static library that doesn't use current locale (unlike strtod from
stdlib.h).  This is the implementation used by e.g. python and nss
(search for "dtoa.c" under UnpackedTarball).

To avoid name clash with the standard strtod, rename the function
to strtod_nolocale.

Size of buffer on stack in ImpSvNumberInputScan::StringToDouble is
256 characters. Logging function usage in make check, of ~124 600
invocations, the longest string was 14 characters, average being
2.1 characters. So heap allocation is unlikely in scenarios with
intensive function usage.

After std::from_chars is available in baseline compilers, external
library can be dropped, and call to strtod_nolocale replaced with
the standard function.

The artifact at https://dev-www.libreoffice.org/src/dtoa-20180411.tgz
is created with

  mkdir dtoa && mkdir dtoa/src && wget https://www.netlib.org/fp/dtoa.c -O dtoa/src/dtoa.c && \
   printf 'd8bab255476f39ea495c8c8ed164f9077da926e6ca7afb9ad3c56d337c4484fe dtoa/src/dtoa.c' | sha256sum -c && \
   tar -c --owner=0 --group=0 --mode=go=r,u=rw --mtime='Wed, 11 Apr 2018 15:59:39 GMT' dtoa/src/dtoa.c | gzip -n > dtoa-20180411.tgz && \
   printf '0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4 dtoa-20180411.tgz' | sha256sum -c

(where the date "Wed, 11 Apr 2018 15:59:39 GMT" is from
`wget -S https://www.netlib.org/fp/dtoa.c`
"Last-Modified: Wed, 11 Apr 2018 15:59:39 GMT" header).

Change-Id: Ia61b7678e257c4bc1ff193f3f856d611aa5c1a21
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88854
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski 2020-02-23 19:19:10 +03:00
parent b623684c66
commit f3e7004794
13 changed files with 201 additions and 26 deletions

View file

@ -112,6 +112,7 @@ $(WORKDIR)/download: $(BUILDDIR)/config_$(gb_Side).mk $(SRCDIR)/download.lst $(S
$(call fetch_Optional,CAIRO,PIXMAN_TARBALL) \
$(call fetch_Optional,CDR,CDR_TARBALL) \
$(call fetch_Optional,CLUCENE,CLUCENE_TARBALL) \
DTOA_TARBALL \
$(call fetch_Optional,LIBCMIS,LIBCMIS_TARBALL) \
$(call fetch_Optional,COINMP,COINMP_TARBALL) \
$(call fetch_Optional,CPPUNIT,CPPUNIT_TARBALL) \

View file

@ -4206,6 +4206,23 @@ endef
endif # SYSTEM_QRCODEGEN
define gb_LinkTarget__use_dtoa
$(call gb_LinkTarget_use_unpacked,$(1),dtoa)
$(call gb_LinkTarget_set_include,$(1),\
-I$(call gb_UnpackedTarball_get_dir,dtoa/include/)\
$$(INCLUDE) \
)
$(call gb_LinkTarget_use_static_libraries,$(1),\
dtoa \
)
endef
define gb_ExternalProject__use_dtoa
$(call gb_ExternalProject_use_static_libraries,$(1),dtoa)
endef
$(eval $(call gb_Helper_register_packages_for_install,ucrt_binarytable,\
$(if $(UCRT_REDISTDIR),ucrt) \
))

View file

@ -21,6 +21,8 @@ export CDR_SHA256SUM := 01cd00b04a030977e544433c2d127c997205332cd9b8e35ec0ee1711
export CDR_TARBALL := libcdr-0.1.6.tar.xz
export CLUCENE_SHA256SUM := ddfdc433dd8ad31b5c5819cc4404a8d2127472a3b720d3e744e8c51d79732eab
export CLUCENE_TARBALL := 48d647fbd8ef8889e5a7f422c1bfda94-clucene-core-2.3.3.4.tar.gz
export DTOA_SHA256SUM := 0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4
export DTOA_TARBALL := dtoa-20180411.tgz
export LIBCMIS_SHA256SUM := d7b18d9602190e10d437f8a964a32e983afd57e2db316a07d87477a79f5000a2
export LIBCMIS_TARBALL := libcmis-0.5.2.tar.xz
export COINMP_SHA256SUM := 86c798780b9e1f5921fe4efe651a93cb420623b45aa1fdff57af8c37f116113f

View file

@ -31,6 +31,7 @@ $(eval $(call gb_Module_add_moduledirs,external,\
$(call gb_Helper_optional,CPPUNIT,cppunit) \
$(call gb_Helper_optional,CT2N,ct2n) \
$(call gb_Helper_optional,CURL,curl) \
dtoa \
$(call gb_Helper_optional,EBOOK,libebook) \
$(call gb_Helper_optional,EPM,epm) \
$(call gb_Helper_optional,EPOXY,epoxy) \

17
external/dtoa/Module_dtoa.mk vendored Normal file
View file

@ -0,0 +1,17 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_Module_Module,dtoa))
$(eval $(call gb_Module_add_targets,dtoa,\
UnpackedTarball_dtoa \
StaticLibrary_dtoa \
))
# vim: set noet sw=4 ts=4:

10
external/dtoa/README vendored Normal file
View file

@ -0,0 +1,10 @@
dtoa is available from [ https://www.netlib.org/fp/ ].
Used to convert a decimal string to double (until std::from_chars is available on used compilers).
Packaged using
mkdir dtoa && mkdir dtoa/src && wget https://www.netlib.org/fp/dtoa.c -O dtoa/src/dtoa.c && \
printf 'd8bab255476f39ea495c8c8ed164f9077da926e6ca7afb9ad3c56d337c4484fe dtoa/src/dtoa.c' | sha256sum -c && \
tar -c --owner=0 --group=0 --mode=go=r,u=rw --mtime='Wed, 11 Apr 2018 15:59:39 GMT' dtoa/src/dtoa.c | gzip -n > dtoa-20180411.tgz && \
printf '0082d0684f7db6f62361b76c4b7faba19e0c7ce5cb8e36c4b65fea8281e711b4 dtoa-20180411.tgz' | sha256sum -c
(where the date "Wed, 11 Apr 2018 15:59:39 GMT" is from `wget -S https://www.netlib.org/fp/dtoa.c` "Last-Modified: Wed, 11 Apr 2018 15:59:39 GMT" header).

22
external/dtoa/StaticLibrary_dtoa.mk vendored Normal file
View file

@ -0,0 +1,22 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_StaticLibrary_StaticLibrary,dtoa))
$(eval $(call gb_StaticLibrary_use_unpacked,dtoa,dtoa))
$(eval $(call gb_StaticLibrary_add_cflags,dtoa,-DIEEE_8087))
$(eval $(call gb_StaticLibrary_set_warnings_disabled,dtoa))
$(eval $(call gb_StaticLibrary_add_generated_cobjects,dtoa,\
UnpackedTarball/dtoa/src/dtoa \
))
# vim: set noet sw=4 ts=4:

20
external/dtoa/UnpackedTarball_dtoa.mk vendored Normal file
View file

@ -0,0 +1,20 @@
#-*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_UnpackedTarball_UnpackedTarball,dtoa))
$(eval $(call gb_UnpackedTarball_set_tarball,dtoa,$(DTOA_TARBALL)))
$(eval $(call gb_UnpackedTarball_set_patchlevel,dtoa,1))
$(eval $(call gb_UnpackedTarball_add_patches,dtoa, \
external/dtoa/include_header.patch \
))
# vim: set noet sw=4 ts=4:

37
external/dtoa/include_header.patch vendored Normal file
View file

@ -0,0 +1,37 @@
--- /dev/null
+++ dtoa/include/dtoa.h
@@ -0,0 +1,3 @@
+extern "C" double strtod_nolocale(const char *s00, char **se);
+extern "C" char *dtoa_nolocale(double d, int mode, int ndigits,
+ int *decpt, int *sign, char **rve);
--- dtoa/src/dtoa.c.orig
+++ dtoa/src/dtoa.c
@@ -1502,8 +1502,8 @@ static unsigned int maxthreads = 0;
#define Kmax 7
#ifdef __cplusplus
-extern "C" double strtod(const char *s00, char **se);
-extern "C" char *dtoa(double d, int mode, int ndigits,
+extern "C" double strtod_nolocale(const char *s00, char **se);
+extern "C" char *dtoa_nolocale(double d, int mode, int ndigits,
int *decpt, int *sign, char **rve);
#endif
@@ -3429,7 +3429,7 @@ retlow1:
#endif /* NO_STRTOD_BIGCOMP */
double
-strtod(const char *s00, char **se)
+strtod_nolocale(const char *s00, char **se)
{
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
@@ -6185,7 +6185,7 @@ dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char
}
char *
-dtoa(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
+dtoa_nolocale(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
{
/* Sufficient space is allocated to the return value
to hold the suppressed trailing zeros.

View file

@ -1849,6 +1849,25 @@
Software.</p>
</blockquote>
</div>
<h2>dtoa</h2>
<p>The following software may be included in this product: dtoa.</p>
<p>dtoa code is covered by the MIT license:</p>
<blockquote>
<p> The author of this software is David M. Gay.</p>
<p> Copyright (c) 1991, 2000, 2001 by Lucent Technologies.</p>
<p>Permission to use, copy, modify, and distribute this software for any
purpose without fee is hereby granted, provided that this entire notice
is included in all copies of any software which is or includes a copy
or modification of this software and in all copies of the supporting
documentation for such software.</p>
<p>THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.</p>
</blockquote>
<h2>Random123: a Library of Counter-Based Random Number Generators</h2>
<p>The following software may be included in this product: Random123: a Library of Counter-Based Random Number
Generators. Use of any of this software is governed by the terms of the license below:</p>

View file

@ -47,6 +47,7 @@ public:
void testTdf31231();
void testTdf128951();
void testTdf129789();
void testTdf130725();
CPPUNIT_TEST_SUITE(ScFiltersTest);
CPPUNIT_TEST(testTdf64229);
@ -64,6 +65,7 @@ public:
CPPUNIT_TEST(testTdf31231);
CPPUNIT_TEST(testTdf128951);
CPPUNIT_TEST(testTdf129789);
CPPUNIT_TEST(testTdf130725);
CPPUNIT_TEST_SUITE_END();
private:
uno::Reference<uno::XInterface> m_xCalcComponent;
@ -470,6 +472,34 @@ void ScFiltersTest::testTdf129789()
xDocSh->DoClose();
}
void ScFiltersTest::testTdf130725()
{
css::uno::Reference<css::frame::XDesktop2> xDesktop
= css::frame::Desktop::create(comphelper::getProcessComponentContext());
CPPUNIT_ASSERT(xDesktop.is());
// 1. Create spreadsheet
css::uno::Sequence<css::beans::PropertyValue> aHiddenArgList(1);
aHiddenArgList[0].Name = "Hidden";
aHiddenArgList[0].Value <<= true;
css::uno::Reference<css::lang::XComponent> xComponent
= xDesktop->loadComponentFromURL("private:factory/scalc", "_blank", 0, aHiddenArgList);
css::uno::Reference<css::sheet::XSpreadsheetDocument> xDoc(xComponent,
css::uno::UNO_QUERY_THROW);
// 2. Insert 0.0042 into a cell as a formula, to force the conversion from string to double
css::uno::Reference<css::sheet::XCellRangesAccess> xSheets(xDoc->getSheets(),
css::uno::UNO_QUERY_THROW);
css::uno::Reference<css::table::XCell> xCell = xSheets->getCellByPosition(0, 0, 0);
xCell->setFormula("0.0042"); // this assumes en-US locale
// 3. Check that the value is the nearest double-precision representation of the decimal 0.0042
// (it was 0.0042000000000000006 instead of 0.0041999999999999997).
CPPUNIT_ASSERT_EQUAL_MESSAGE("Value must be the nearest representation of decimal 0.0042",
0.0042, xCell->getValue()); // strict equality
}
ScFiltersTest::ScFiltersTest()
: ScBootstrapFixture( "sc/qa/unit/data" )
{

View file

@ -23,6 +23,7 @@ $(eval $(call gb_Library_use_externals,svl,\
boost_headers \
$(if $(filter LINUX MACOSX ANDROID %BSD SOLARIS HAIKU,$(OS)), \
curl) \
dtoa \
icu_headers \
icuuc \
mdds_headers \

View file

@ -18,6 +18,7 @@
*/
#include <cstdlib>
#include <dtoa.h>
#include <float.h>
#include <comphelper/string.hxx>
#include <sal/log.hxx>
@ -36,6 +37,8 @@
#include "zforscan.hxx"
#include <svl/zformat.hxx>
#include <memory>
#include "zforfind.hxx"
#ifndef DBG_UTIL
@ -151,35 +154,30 @@ static void TransformInput( SvNumberFormatter const * pFormatter, OUString& rStr
*/
double ImpSvNumberInputScan::StringToDouble( const OUString& rStr, bool bForceFraction )
{
double fNum = 0.0;
double fFrac = 0.0;
int nExp = 0;
sal_Int32 nPos = 0;
sal_Int32 nLen = rStr.getLength();
bool bPreSep = !bForceFraction;
while (nPos < nLen)
std::unique_ptr<char[]> bufInHeap;
constexpr int bufOnStackSize = 256;
char bufOnStack[bufOnStackSize];
char* buf = bufOnStack;
const sal_Int32 bufsize = rStr.getLength() + (bForceFraction ? 2 : 1);
if (bufsize > bufOnStackSize)
{
if (rStr[nPos] == '.')
{
bPreSep = false;
}
else if (bPreSep)
{
fNum = fNum * 10.0 + static_cast<double>(rStr[nPos] - '0');
}
bufInHeap = std::make_unique<char[]>(bufsize);
buf = bufInHeap.get();
}
char* p = buf;
if (bForceFraction)
*p++ = '.';
for (sal_Int32 nPos = 0; nPos < rStr.getLength(); ++nPos)
{
sal_Unicode c = rStr[nPos];
if (c == '.' || (c >= '0' && c <= '9'))
*p++ = static_cast<char>(c);
else
{
fFrac = fFrac * 10.0 + static_cast<double>(rStr[nPos] - '0');
--nExp;
}
nPos++;
break;
}
if ( fFrac )
{
return fNum + ::rtl::math::pow10Exp( fFrac, nExp );
}
return fNum;
*p = '\0';
return strtod_nolocale(buf, nullptr);
}
namespace {