From b36b24ecfbb7e9fc6c5d5d0a789fabd2a0cb18b3 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 26 Oct 2023 08:54:31 +0200 Subject: [PATCH] Use std::from_chars (which also makes readInt32 report failure, instead of running into UB, when the read value is out of range) Change-Id: I8449e4fe696c9b0ab5b868127ef517dc080c31ba Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158489 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- xmlhelp/source/cxxhelp/provider/db.cxx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/xmlhelp/source/cxxhelp/provider/db.cxx b/xmlhelp/source/cxxhelp/provider/db.cxx index bf3c269b99ca..d60bff78ee63 100644 --- a/xmlhelp/source/cxxhelp/provider/db.cxx +++ b/xmlhelp/source/cxxhelp/provider/db.cxx @@ -20,28 +20,23 @@ #include "db.hxx" +#include +#include #include +#include #include #include -#include using namespace com::sun::star::uno; using namespace com::sun::star::io; namespace { -//TODO: Replace with C++17 std::from_chars once available: std::pair readInt32(char const * begin, char const * end) { sal_Int32 n = 0; - for (; begin != end; ++begin) { - auto const w = INetMIME::getHexWeight(static_cast(*begin)); - if (w == -1) { - break; - } - n = 16 * n + w; - } - return {n, begin}; + auto const [ptr, ec] = std::from_chars(begin, end, n, 16); + return {std::max(n, sal_Int32(0)), ec == std::errc{} && n >= 0 ? ptr : begin}; } }