From d5b7627a0e738c0866b819910153b96b611813f8 Mon Sep 17 00:00:00 2001 From: Andreas Heinisch Date: Thu, 26 Sep 2019 07:55:37 +0200 Subject: tdf#62326 - Macros: Converting Hex strings of negative value If the value of the hex string lies within the range of 0x8000 (SbxMAXINT + 1) and 0xFFFF (SbxMAXUINT) inclusive, cast the value to 16 bit in order to get signed integers, e.g., SbxMININT through SbxMAXINT. Moved unit test to test_scanner.cxx in order to test basic hex convertations. Removed old vba unit tests. Change-Id: I247b41c40197afc5328ef5685c758c1dd1cefae5 Reviewed-on: https://gerrit.libreoffice.org/79583 Reviewed-by: Stephan Bergmann Tested-by: Stephan Bergmann --- .../test_hexliteral_negIntLimit-2.vb | 18 ------ .../basic_coverage/test_hexliteral_negIntLimit.vb | 18 ------ .../basic_coverage/test_hexliteral_posIntLimit.vb | 18 ------ .../basic_coverage/test_hexliteral_zeroIntLimit.vb | 18 ------ basic/qa/cppunit/test_scanner.cxx | 65 ++++++++++++++++++++++ basic/source/comp/scanner.cxx | 5 +- 6 files changed, 69 insertions(+), 73 deletions(-) delete mode 100644 basic/qa/basic_coverage/test_hexliteral_negIntLimit-2.vb delete mode 100644 basic/qa/basic_coverage/test_hexliteral_negIntLimit.vb delete mode 100644 basic/qa/basic_coverage/test_hexliteral_posIntLimit.vb delete mode 100644 basic/qa/basic_coverage/test_hexliteral_zeroIntLimit.vb (limited to 'basic') diff --git a/basic/qa/basic_coverage/test_hexliteral_negIntLimit-2.vb b/basic/qa/basic_coverage/test_hexliteral_negIntLimit-2.vb deleted file mode 100644 index c660486a5618..000000000000 --- a/basic/qa/basic_coverage/test_hexliteral_negIntLimit-2.vb +++ /dev/null @@ -1,18 +0,0 @@ -' -' 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/. -' - - -Function doUnitTest as Integer - Dim lngDecimal as Long - lngDecimal = Clng(&H80000000) - If lngDecimal = -2147483648 Then - doUnitTest = 1 - Else - doUnitTest = 0 - End If -End Function diff --git a/basic/qa/basic_coverage/test_hexliteral_negIntLimit.vb b/basic/qa/basic_coverage/test_hexliteral_negIntLimit.vb deleted file mode 100644 index f33d74cf8dd9..000000000000 --- a/basic/qa/basic_coverage/test_hexliteral_negIntLimit.vb +++ /dev/null @@ -1,18 +0,0 @@ -' -' 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/. -' - - -Function doUnitTest as Integer - Dim lngDecimal as Long - lngDecimal = Clng(&HFFFFFFFF) - If lngDecimal = -1 Then - doUnitTest = 1 - Else - doUnitTest = 0 - End If -End Function diff --git a/basic/qa/basic_coverage/test_hexliteral_posIntLimit.vb b/basic/qa/basic_coverage/test_hexliteral_posIntLimit.vb deleted file mode 100644 index a03dadc42095..000000000000 --- a/basic/qa/basic_coverage/test_hexliteral_posIntLimit.vb +++ /dev/null @@ -1,18 +0,0 @@ -' -' 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/. -' - - -Function doUnitTest as Integer - Dim lngDecimal as Long - lngDecimal = Clng(&H7FFFFFFF) - If lngDecimal = 2147483647 Then - doUnitTest = 1 - Else - doUnitTest = 0 - End If -End Function diff --git a/basic/qa/basic_coverage/test_hexliteral_zeroIntLimit.vb b/basic/qa/basic_coverage/test_hexliteral_zeroIntLimit.vb deleted file mode 100644 index 1a02bdebfb6f..000000000000 --- a/basic/qa/basic_coverage/test_hexliteral_zeroIntLimit.vb +++ /dev/null @@ -1,18 +0,0 @@ -' -' 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/. -' - - -Function doUnitTest as Integer - Dim lngDecimal as Long - lngDecimal = Clng(&H0) - If lngDecimal = 0 Then - doUnitTest = 1 - Else - doUnitTest = 0 - End If -End Function diff --git a/basic/qa/cppunit/test_scanner.cxx b/basic/qa/cppunit/test_scanner.cxx index e81af6106542..511be7b486e0 100644 --- a/basic/qa/cppunit/test_scanner.cxx +++ b/basic/qa/cppunit/test_scanner.cxx @@ -779,6 +779,15 @@ namespace const OUString source8("&O123000000000000000000000"); const OUString source9("&H1.23"); + // tdf#62323, tdf#62326 - conversion of Hex literals to basic signed Integers + const OUString source10("&H0"); + const OUString source11("&H8000"); + const OUString source12("&H80000000"); + const OUString source13("&HFFFF"); + const OUString source14("&HFFFFFFFF"); + const OUString source15("&H7FFF"); + const OUString source16("&H7FFFFFFF"); + std::vector symbols; symbols = getSymbols(source1); @@ -860,6 +869,62 @@ namespace CPPUNIT_ASSERT_EQUAL(OUString(), symbols[1].text); CPPUNIT_ASSERT_EQUAL(SbxDOUBLE, symbols[1].type); CPPUNIT_ASSERT_EQUAL(cr, symbols[2].text); + + // &H0 = 0 + symbols = getSymbols(source10); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxINTEGER, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &H8000 = -32768 + symbols = getSymbols(source11); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(SbxMININT, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxINTEGER, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &H80000000 = -2147483648 + symbols = getSymbols(source12); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(SbxMINLNG, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxLONG, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &HFFFF = -1 + symbols = getSymbols(source13); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.0, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxINTEGER, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &HFFFFFFFF = -1 + symbols = getSymbols(source14); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.0, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxINTEGER, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &H7FFF = 32767 + symbols = getSymbols(source15); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(SbxMAXINT, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxINTEGER, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); + + // &H7FFFFFFF = 2147483647 + symbols = getSymbols(source16); + CPPUNIT_ASSERT_EQUAL(size_t(2), symbols.size()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(SbxMAXLNG, symbols[0].number, 1E-12); + CPPUNIT_ASSERT_EQUAL(OUString(), symbols[0].text); + CPPUNIT_ASSERT_EQUAL(SbxLONG, symbols[0].type); + CPPUNIT_ASSERT_EQUAL(cr, symbols[1].text); } void ScannerTest::testTdf103104() diff --git a/basic/source/comp/scanner.cxx b/basic/source/comp/scanner.cxx index 77424d4a369f..f08c0178d38e 100644 --- a/basic/source/comp/scanner.cxx +++ b/basic/source/comp/scanner.cxx @@ -487,7 +487,10 @@ bool SbiScanner::NextSym() ++nLineIdx; ++nCol; } - sal_Int32 ls = static_cast(lu); + // tdf#62326 - If the value of the hex string lies within the range of 0x8000 (SbxMAXINT + 1) + // and 0xFFFF (SbxMAXUINT) inclusive, cast the value to 16 bit in order to get + // signed integers, e.g., SbxMININT through SbxMAXINT + sal_Int32 ls = (lu > SbxMAXINT && lu <= SbxMAXUINT) ? static_cast(lu) : static_cast(lu); nVal = static_cast(ls); eScanType = ( ls >= SbxMININT && ls <= SbxMAXINT ) ? SbxINTEGER : SbxLONG; if( bOverflow ) -- cgit