From 9ca19666511b1d44ba554a70cbbc0c8e2db844c6 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 31 Jan 2017 14:46:38 +0200 Subject: loplugin:useuniqueptr extend to check local vars just the simple and obvious case for now, of a local var being allocated and deleted inside a single local block, and the delete happening at the end of the block Reviewed-on: https://gerrit.libreoffice.org/33749 Tested-by: Jenkins Reviewed-by: Noel Grandin (cherry picked from commit 2489000d3fd66319a8355fd4e37cfdfda47296d0) Change-Id: I3a7a094da543debdcd2374737c2ecff91d644625 (cherry picked from commit e540ccfb5eb43bd4a3d6920074dce8436720ba8e) --- sal/qa/osl/process/osl_Thread.cxx | 10 +++---- sal/qa/rtl/oustring/rtl_OUString2.cxx | 5 +--- sal/qa/rtl/process/rtl_Process.cxx | 5 ++-- sal/qa/rtl/random/rtl_random.cxx | 50 +++++++++++++++-------------------- 4 files changed, 27 insertions(+), 43 deletions(-) (limited to 'sal') diff --git a/sal/qa/osl/process/osl_Thread.cxx b/sal/qa/osl/process/osl_Thread.cxx index 32840a69c80c..911125a6f029 100644 --- a/sal/qa/osl/process/osl_Thread.cxx +++ b/sal/qa/osl/process/osl_Thread.cxx @@ -28,17 +28,14 @@ // include files #include - #include - #include - #include - #include #include #include +#include #include #include @@ -1695,13 +1692,12 @@ public: private: void SAL_CALL run() override { - oslThreadIdentifier* pId = new oslThreadIdentifier; + std::unique_ptr pId( new oslThreadIdentifier ); *pId = getIdentifier(); - idData.setData(pId); + idData.setData(pId.get()); oslThreadIdentifier* pIdData = static_cast(idData.getData()); //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData); m_Id = *pIdData; - delete pId; } public: diff --git a/sal/qa/rtl/oustring/rtl_OUString2.cxx b/sal/qa/rtl/oustring/rtl_OUString2.cxx index 84859d1a4789..6a3e32eb6523 100644 --- a/sal/qa/rtl/oustring/rtl_OUString2.cxx +++ b/sal/qa/rtl/oustring/rtl_OUString2.cxx @@ -893,9 +893,7 @@ public: // Test interning lots of things int i; static const int nSequence = 4096; - rtl::OUString *pStrs; - - pStrs = new rtl::OUString[nSequence]; + std::unique_ptr pStrs(new rtl::OUString[nSequence]); for (i = 0; i < nSequence; i++) { pStrs[i] = rtl::OUString::number( sqrt( static_cast(i) ) ).intern(); @@ -906,7 +904,6 @@ public: CPPUNIT_ASSERT_EQUAL_MESSAGE("double intern failed", pStrs[i].pData, aNew.pData); } - delete [] pStrs; } CPPUNIT_TEST_SUITE(construction); diff --git a/sal/qa/rtl/process/rtl_Process.cxx b/sal/qa/rtl/process/rtl_Process.cxx index ae0eb32500ba..92ff8a5950bc 100644 --- a/sal/qa/rtl/process/rtl_Process.cxx +++ b/sal/qa/rtl/process/rtl_Process.cxx @@ -119,11 +119,11 @@ public: // "osl_joinProcess returned with failure", // osl_Process_E_None == osl_error // ); - oslProcessInfo* pInfo = new oslProcessInfo; + std::unique_ptr pInfo( new oslProcessInfo ); //please pay attention to initial the Size to sizeof(oslProcessInfo), or else //you will get unknown error when call osl_getProcessInfo pInfo->Size = sizeof(oslProcessInfo); - osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo ); + osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo.get() ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "osl_getProcessInfo returned with failure", @@ -132,7 +132,6 @@ public: printf("the exit code is %" SAL_PRIuUINT32 ".\n", pInfo->Code ); CPPUNIT_ASSERT_EQUAL_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", static_cast(2), pInfo->Code); - delete pInfo; } CPPUNIT_TEST_SUITE(getAppCommandArg); diff --git a/sal/qa/rtl/random/rtl_random.cxx b/sal/qa/rtl/random/rtl_random.cxx index 14421d652560..16e880d9657d 100644 --- a/sal/qa/rtl/random/rtl_random.cxx +++ b/sal/qa/rtl/random/rtl_random.cxx @@ -98,8 +98,8 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 4; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; - memset(pBuffer, 0, nBufLen); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); + memset(pBuffer.get(), 0, nBufLen); rtlRandomError aError = rtl_random_addBytes(nullptr, nullptr, 0); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); @@ -107,12 +107,10 @@ public: /* rtlRandomError */ aError = rtl_random_addBytes(aPool, nullptr, 0); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); - /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen); + /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer.get(), nBufLen); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); rtl_random_destroyPool(aPool); - delete [] pBuffer; - } void addBytes_001() @@ -120,16 +118,15 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 4; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); - memset(pBuffer, 0, nBufLen); + memset(pBuffer.get(), 0, nBufLen); - rtl_random_addBytes(aPool, pBuffer, nBufLen); + rtl_random_addBytes(aPool, pBuffer.get(), nBufLen); printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); rtl_random_destroyPool(aPool); - delete [] pBuffer; } // Change the following lines only, if you add, remove or rename @@ -221,8 +218,8 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 4; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; - memset(pBuffer, 0, nBufLen); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); + memset(pBuffer.get(), 0, nBufLen); rtlRandomError aError = rtl_random_getBytes(nullptr, nullptr, 0); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); @@ -230,11 +227,10 @@ public: /* rtlRandomError */ aError = rtl_random_getBytes(aPool, nullptr, 0); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); - /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); + /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); rtl_random_destroyPool(aPool); - delete [] pBuffer; } void getBytes_001() @@ -242,16 +238,15 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 4; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; - memset(pBuffer, 0, nBufLen); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); + memset(pBuffer.get(), 0, nBufLen); - rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); + rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); rtl_random_destroyPool(aPool); - delete [] pBuffer; } void getBytes_002() @@ -259,12 +254,12 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 4; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ]; - memset(pBuffer, 0, nBufLen << 1); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen << 1 ] ); + memset(pBuffer.get(), 0, nBufLen << 1); CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); - rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); + rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); printf("%2x %2x %2x %2x %2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3], pBuffer[4], pBuffer[5], pBuffer[6], pBuffer[7]); @@ -272,7 +267,6 @@ public: CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); rtl_random_destroyPool(aPool); - delete [] pBuffer; } void getBytes_003() @@ -280,8 +274,8 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 1; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; - memset(pBuffer, 0, nBufLen); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); + memset(pBuffer.get(), 0, nBufLen); Statistics aStat; @@ -292,7 +286,7 @@ public: int nCountMax = 1000000; for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...) { - /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); + /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */ aStat.addValue(pBuffer[0], 1); @@ -304,7 +298,6 @@ public: CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); rtl_random_destroyPool(aPool); - delete [] pBuffer; } void getBytes_003_1() @@ -312,8 +305,8 @@ public: rtlRandomPool aPool = rtl_random_createPool(); sal_uInt32 nBufLen = 256; - sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; - memset(pBuffer, 0, nBufLen); + std::unique_ptr pBuffer( new sal_uInt8[ nBufLen ] ); + memset(pBuffer.get(), 0, nBufLen); Statistics aStat; @@ -324,7 +317,7 @@ public: int nCountMax = 10000; for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...) { - /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); + /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); for (sal_uInt32 i=0;i