Removed memory leak in cleanUpCertificate when the certificate can't be cleaned.

Switched from char* to std::string, because the char* is unnecessary and wasn't freed on some returns.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3368 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-08-14 11:51:15 +00:00
parent 358c425986
commit c4167ba0ac

View file

@ -46,7 +46,7 @@ std::string cleanUpCertificate(std::string badCertificate)
Buffer for storing the cleaned certificate. In certain cases the Buffer for storing the cleaned certificate. In certain cases the
cleanCertificate can be larger than the badCertificate cleanCertificate can be larger than the badCertificate
*/ */
char * cleanCertificate=new char[badCertificate.length()+100]; std::string cleanCertificate;
//The entire certificate begin tag //The entire certificate begin tag
const char * beginCertTag="-----BEGIN"; const char * beginCertTag="-----BEGIN";
//The entire certificate end tag //The entire certificate end tag
@ -71,8 +71,6 @@ std::string cleanUpCertificate(std::string badCertificate)
size_t endCertEndIdx=0; size_t endCertEndIdx=0;
//The length of the bad certificate. //The length of the bad certificate.
size_t lengthOfCert=badCertificate.length(); size_t lengthOfCert=badCertificate.length();
//The current index value in the cleaned certificate.
size_t currCleanCertIdx=0;
//The current index value in the bad certificate //The current index value in the bad certificate
size_t currBadCertIdx=0; size_t currBadCertIdx=0;
//Temporary index value //Temporary index value
@ -171,12 +169,7 @@ std::string cleanUpCertificate(std::string badCertificate)
/* /*
Copying the begin tag(-----BEGIN) to the clean certificate Copying the begin tag(-----BEGIN) to the clean certificate
*/ */
for(size_t i=0;i<strlen(beginCertTag);i++) cleanCertificate += beginCertTag;
{
cleanCertificate[currCleanCertIdx+i]=beginCertTag[i];
}
currCleanCertIdx=currCleanCertIdx+strlen(beginCertTag);
currBadCertIdx=beginCertStartIdx2+strlen(beginTag); currBadCertIdx=beginCertStartIdx2+strlen(beginTag);
/* /*
Copying the name of the tag e.g XPGP CERTIFICATE. At the same time remove any white spaces and new line Copying the name of the tag e.g XPGP CERTIFICATE. At the same time remove any white spaces and new line
@ -194,28 +187,22 @@ std::string cleanUpCertificate(std::string badCertificate)
} }
else else
{ {
cleanCertificate[currCleanCertIdx]=badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currCleanCertIdx++;
currBadCertIdx++; currBadCertIdx++;
} }
} }
/* /*
If the last character is a space we need to remove it. If the last character is a space we need to remove it.
*/ */
if(cleanCertificate[currCleanCertIdx-1]==' ') if(cleanCertificate.substr(cleanCertificate.length()-1, 1) == " ")
{ {
currCleanCertIdx--; cleanCertificate.erase(cleanCertificate.length()-1);
} }
/* /*
Copying the end part of the certificate start tag(-----). Copying the end part of the certificate start tag(-----).
*/ */
for(size_t i=0;i<strlen(commonTag);i++) cleanCertificate += commonTag;
{ cleanCertificate += "\n";
cleanCertificate[currCleanCertIdx]='-';
currCleanCertIdx++;
}
cleanCertificate[currCleanCertIdx]='\n';
currCleanCertIdx++;
currBadCertIdx=currBadCertIdx+strlen(commonTag); currBadCertIdx=currBadCertIdx+strlen(commonTag);
/* /*
Remove the white spaces between the end of the certificate begin tag and the actual Remove the white spaces between the end of the certificate begin tag and the actual
@ -227,16 +214,14 @@ std::string cleanUpCertificate(std::string badCertificate)
} }
//keep the first line : gnupg version //keep the first line : gnupg version
cleanCertificate[currCleanCertIdx]= badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currBadCertIdx++; currBadCertIdx++;
while(badCertificate[currBadCertIdx]!='\n') while(badCertificate[currBadCertIdx]!='\n')
{ {
cleanCertificate[currCleanCertIdx]= badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currCleanCertIdx++;
currBadCertIdx++; currBadCertIdx++;
} }
cleanCertificate[currCleanCertIdx]= badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currCleanCertIdx++;
currBadCertIdx++; currBadCertIdx++;
//Start of the actual certificate. Remove spaces in the certificate //Start of the actual certificate. Remove spaces in the certificate
@ -247,8 +232,7 @@ std::string cleanUpCertificate(std::string badCertificate)
{ {
if(cntPerLine==64) if(cntPerLine==64)
{ {
cleanCertificate[currCleanCertIdx]='\n'; cleanCertificate += "\n";
currCleanCertIdx++;
cntPerLine=0; cntPerLine=0;
continue; continue;
} }
@ -262,17 +246,15 @@ std::string cleanUpCertificate(std::string badCertificate)
currBadCertIdx++; currBadCertIdx++;
continue; continue;
} }
cleanCertificate[currCleanCertIdx]=badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
cntPerLine++; cntPerLine++;
currCleanCertIdx++;
currBadCertIdx++; currBadCertIdx++;
} }
if(cleanCertificate[currCleanCertIdx-1]!='\n') if(cleanCertificate.substr(cleanCertificate.length()-1,1)!="\n")
{ {
cleanCertificate[currCleanCertIdx]='\n'; cleanCertificate += "\n";
currCleanCertIdx++; // std::cerr<<"zeeeee"<<std::endl;
// std::cerr<<"zeeeee"<<std::endl;
} }
else else
{ {
@ -282,12 +264,7 @@ std::string cleanUpCertificate(std::string badCertificate)
Copying the begining part of the certificate end tag. Copying Copying the begining part of the certificate end tag. Copying
-----END part of the tag. -----END part of the tag.
*/ */
for(size_t i=0;i<strlen(endCertTag);i++) cleanCertificate += endCertTag;
{
cleanCertificate[currCleanCertIdx+i]=endCertTag[i];
}
currCleanCertIdx=currCleanCertIdx+strlen(endCertTag);
currBadCertIdx=endCertStartIdx2+strlen(endTag); currBadCertIdx=endCertStartIdx2+strlen(endTag);
/* /*
Copying the name of the certificate e.g XPGP CERTIFICATE. The end tag also has the Copying the name of the certificate e.g XPGP CERTIFICATE. The end tag also has the
@ -305,8 +282,7 @@ std::string cleanUpCertificate(std::string badCertificate)
} }
else else
{ {
cleanCertificate[currCleanCertIdx]=badCertificate[currBadCertIdx]; cleanCertificate += badCertificate[currBadCertIdx];
currCleanCertIdx++;
currBadCertIdx++; currBadCertIdx++;
} }
@ -314,35 +290,17 @@ std::string cleanUpCertificate(std::string badCertificate)
/* /*
If the last character is a space we need to remove it. If the last character is a space we need to remove it.
*/ */
if(cleanCertificate[currCleanCertIdx-1]==' ') if(cleanCertificate.substr(cleanCertificate.length()-1,1)==" ")
{ {
currCleanCertIdx--; cleanCertificate.erase(cleanCertificate.length()-1);
} }
/* /*
Copying the end part(-----) of the end tag in the certificate. Copying the end part(-----) of the end tag in the certificate.
*/ */
for(size_t i=0;i<strlen(commonTag);i++) cleanCertificate += commonTag;
{ cleanCertificate += "\n";
cleanCertificate[currCleanCertIdx]='-';
currCleanCertIdx++;
}
cleanCertificate[currCleanCertIdx]='\n'; return cleanCertificate;
currCleanCertIdx++;
/*
Copying over the cleaned certificate to a new buffer.
*/
char * cleanCert=new char[currCleanCertIdx+1];
for(size_t i=0;i<currCleanCertIdx;i++ )
{
cleanCert[i]=cleanCertificate[i];
}
cleanCert[currCleanCertIdx]='\0';
std::string cleanCertificateStr=cleanCert;
delete[] cleanCertificate;
delete[] cleanCert;
return cleanCertificateStr;
} }
int findEndIdxOfCertStartTag(std::string badCertificate) int findEndIdxOfCertStartTag(std::string badCertificate)