Avoid possible memory leaks in case of exceptions

Change-Id: I4b2b439615db0ff4598f405d1e339eebbff7ae91
This commit is contained in:
Takeshi Abe 2014-04-21 22:29:09 +09:00
parent aa8820bdc2
commit 1a5457a11c
3 changed files with 19 additions and 31 deletions

View file

@ -46,6 +46,7 @@
#include "framework.hxx" #include "framework.hxx"
#include "fwkutil.hxx" #include "fwkutil.hxx"
#include <boost/scoped_array.hpp>
using namespace osl; using namespace osl;
@ -114,10 +115,10 @@ rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
static const char EncodingTable[] = static const char EncodingTable[] =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
sal_Int32 lenRaw = rawData.getLength(); sal_Int32 lenRaw = rawData.getLength();
char* pBuf = new char[lenRaw * 2]; boost::scoped_array<char> pBuf(new char[lenRaw * 2]);
const sal_Int8* arRaw = rawData.getConstArray(); const sal_Int8* arRaw = rawData.getConstArray();
char* pCurBuf = pBuf; char* pCurBuf = pBuf.get();
for (int i = 0; i < lenRaw; i++) for (int i = 0; i < lenRaw; i++)
{ {
unsigned char curChar = arRaw[i]; unsigned char curChar = arRaw[i];
@ -133,8 +134,7 @@ rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
pCurBuf++; pCurBuf++;
} }
rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2); rtl::ByteSequence ret((sal_Int8*) pBuf.get(), lenRaw * 2);
delete [] pBuf;
return ret; return ret;
} }
@ -144,7 +144,7 @@ rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
sal_Int32 lenData = data.getLength(); sal_Int32 lenData = data.getLength();
sal_Int32 lenBuf = lenData / 2; //always divisable by two sal_Int32 lenBuf = lenData / 2; //always divisable by two
unsigned char* pBuf = new unsigned char[lenBuf]; boost::scoped_array<unsigned char> pBuf(new unsigned char[lenBuf]);
const sal_Int8* pData = data.getConstArray(); const sal_Int8* pData = data.getConstArray();
for (sal_Int32 i = 0; i < lenBuf; i++) for (sal_Int32 i = 0; i < lenBuf; i++)
{ {
@ -173,8 +173,7 @@ rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
} }
pBuf[i] = nibble; pBuf[i] = nibble;
} }
rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf ); rtl::ByteSequence ret((sal_Int8*) pBuf.get(), lenBuf );
delete [] pBuf;
return ret; return ret;
} }

View file

@ -33,6 +33,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <boost/scoped_array.hpp>
using namespace std; using namespace std;
@ -327,13 +328,12 @@ void XRMResExport::WorkOnDesc(
ifstream file (sDescFileName.getStr(), ios::in|ios::binary|ios::ate); ifstream file (sDescFileName.getStr(), ios::in|ios::binary|ios::ate);
if (file.is_open()) { if (file.is_open()) {
int size = static_cast<int>(file.tellg()); int size = static_cast<int>(file.tellg());
char* memblock = new char [size+1]; boost::scoped_array<char> memblock(new char [size+1]);
file.seekg (0, ios::beg); file.seekg (0, ios::beg);
file.read (memblock, size); file.read (memblock.get(), size);
file.close(); file.close();
memblock[size] = '\0'; memblock[size] = '\0';
rText = OString(memblock); rText = OString(memblock.get());
delete[] memblock;
} }
WorkOnText( rOpenTag, rText ); WorkOnText( rOpenTag, rText );
EndOfText( rOpenTag, rOpenTag ); EndOfText( rOpenTag, rOpenTag );

View file

@ -47,6 +47,7 @@
#include <list> #include <list>
#include <set> #include <set>
#include <boost/scoped_array.hpp>
using namespace utl; using namespace utl;
using namespace osl; using namespace osl;
@ -253,8 +254,6 @@ Reference< XHyphenatedWord > SAL_CALL Hyphenator::hyphenate( const OUString& aWo
int nHyphenationPosAlt = -1; int nHyphenationPosAlt = -1;
int nHyphenationPosAltHyph = -1; int nHyphenationPosAltHyph = -1;
int wordlen; int wordlen;
char *hyphens;
char *lcword;
int k = 0; int k = 0;
PropertyHelper_Hyphenation& rHelper = GetPropHelper(); PropertyHelper_Hyphenation& rHelper = GetPropHelper();
@ -341,15 +340,15 @@ Reference< XHyphenatedWord > SAL_CALL Hyphenator::hyphenate( const OUString& aWo
OString encWord(OU2ENC(nTerm,eEnc)); OString encWord(OU2ENC(nTerm,eEnc));
wordlen = encWord.getLength(); wordlen = encWord.getLength();
lcword = new char[wordlen + 1]; boost::scoped_array<char> lcword(new char[wordlen + 1]);
hyphens = new char[wordlen + 5]; boost::scoped_array<char> hyphens(new char[wordlen + 5]);
char ** rep = NULL; // replacements of discretionary hyphenation char ** rep = NULL; // replacements of discretionary hyphenation
int * pos = NULL; // array of [hyphenation point] minus [deletion position] int * pos = NULL; // array of [hyphenation point] minus [deletion position]
int * cut = NULL; // length of deletions in original word int * cut = NULL; // length of deletions in original word
// copy converted word into simple char buffer // copy converted word into simple char buffer
strcpy(lcword,encWord.getStr()); strcpy(lcword.get(),encWord.getStr());
// now strip off any ending periods // now strip off any ending periods
int n = wordlen-1; int n = wordlen-1;
@ -358,15 +357,13 @@ Reference< XHyphenatedWord > SAL_CALL Hyphenator::hyphenate( const OUString& aWo
n++; n++;
if (n > 0) if (n > 0)
{ {
const bool bFailed = 0 != hnj_hyphen_hyphenate3( dict, lcword, n, hyphens, NULL, const bool bFailed = 0 != hnj_hyphen_hyphenate3( dict, lcword.get(), n, hyphens.get(), NULL,
&rep, &pos, &cut, minLead, minTrail, &rep, &pos, &cut, minLead, minTrail,
Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))), Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))),
Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))) ); Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))) );
if (bFailed) if (bFailed)
{ {
// whoops something did not work // whoops something did not work
delete[] hyphens;
delete[] lcword;
if (rep) if (rep)
{ {
for(int j = 0; j < n; j++) for(int j = 0; j < n; j++)
@ -480,8 +477,6 @@ Reference< XHyphenatedWord > SAL_CALL Hyphenator::hyphenate( const OUString& aWo
} }
} }
delete[] lcword;
delete[] hyphens;
if (rep) if (rep)
{ {
for(int j = 0; j < n; j++) for(int j = 0; j < n; j++)
@ -603,14 +598,14 @@ Reference< XPossibleHyphens > SAL_CALL Hyphenator::createPossibleHyphens( const
OString encWord(OU2ENC(nTerm,eEnc)); OString encWord(OU2ENC(nTerm,eEnc));
int wordlen = encWord.getLength(); int wordlen = encWord.getLength();
char *lcword = new char[wordlen+1]; boost::scoped_array<char> lcword(new char[wordlen+1]);
char *hyphens = new char[wordlen+5]; boost::scoped_array<char> hyphens(new char[wordlen+5]);
char ** rep = NULL; // replacements of discretionary hyphenation char ** rep = NULL; // replacements of discretionary hyphenation
int * pos = NULL; // array of [hyphenation point] minus [deletion position] int * pos = NULL; // array of [hyphenation point] minus [deletion position]
int * cut = NULL; // length of deletions in original word int * cut = NULL; // length of deletions in original word
// copy converted word into simple char buffer // copy converted word into simple char buffer
strcpy(lcword,encWord.getStr()); strcpy(lcword.get(),encWord.getStr());
// first remove any trailing periods // first remove any trailing periods
int n = wordlen-1; int n = wordlen-1;
@ -619,15 +614,12 @@ Reference< XPossibleHyphens > SAL_CALL Hyphenator::createPossibleHyphens( const
n++; n++;
if (n > 0) if (n > 0)
{ {
const bool bFailed = 0 != hnj_hyphen_hyphenate3(dict, lcword, n, hyphens, NULL, const bool bFailed = 0 != hnj_hyphen_hyphenate3(dict, lcword.get(), n, hyphens.get(), NULL,
&rep, &pos, &cut, minLead, minTrail, &rep, &pos, &cut, minLead, minTrail,
Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))), Max(dict->clhmin, Max(dict->clhmin, 2) + Max(0, minLead - Max(dict->lhmin, 2))),
Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))) ); Max(dict->crhmin, Max(dict->crhmin, 2) + Max(0, minTrail - Max(dict->rhmin, 2))) );
if (bFailed) if (bFailed)
{ {
delete[] hyphens;
delete[] lcword;
if (rep) if (rep)
{ {
for(int j = 0; j < n; j++) for(int j = 0; j < n; j++)
@ -678,9 +670,6 @@ Reference< XPossibleHyphens > SAL_CALL Hyphenator::createPossibleHyphens( const
Reference< XPossibleHyphens > xRes = PossibleHyphens::CreatePossibleHyphens( Reference< XPossibleHyphens > xRes = PossibleHyphens::CreatePossibleHyphens(
aWord, LinguLocaleToLanguage( aLocale ), hyphenatedWord, aHyphPos); aWord, LinguLocaleToLanguage( aLocale ), hyphenatedWord, aHyphPos);
delete[] hyphens;
delete[] lcword;
if (rep) if (rep)
{ {
for(int j = 0; j < n; j++) for(int j = 0; j < n; j++)