src/gui/update_ext.cpp: Fix memleak (Thx SatBaby)

- rename enum's 'RESET_Fxx' => 'CLOSE_Fxx'
This commit is contained in:
Michael Liebmann
2013-07-19 11:59:54 +02:00
parent 9cb1a9c335
commit 61d36266c8
2 changed files with 34 additions and 20 deletions

View File

@@ -71,6 +71,7 @@ CExtUpdate::CExtUpdate()
fLogfile = "/tmp/update.log";
mountPkt = "/tmp/image_mount";
FileHelpers = NULL;
MTDBuf = NULL;
flashErrorFlag = false;
total = bsize = used = 0;
free1 = free2 = free3 = 0;
@@ -84,6 +85,8 @@ CExtUpdate::~CExtUpdate()
{
if(FileHelpers)
delete[] FileHelpers;
if(MTDBuf)
delete[] MTDBuf;
copyList.clear();
blackList.clear();
deleteList.clear();
@@ -105,12 +108,17 @@ bool CExtUpdate::ErrorReset(bool modus, const std::string & msg1, const std::str
umount(mountPkt.c_str());
// my_system(2,"rmmod", mtdramDriver.c_str());
}
if (modus & RESET_FD1)
if (modus & CLOSE_FD1)
close(fd1);
if (modus & RESET_FD2)
if (modus & CLOSE_FD2)
close(fd2);
if (modus & RESET_F1)
if (modus & CLOSE_F1)
fclose(f1);
if (modus & DELETE_MTDBUF) {
if (MTDBuf != NULL)
delete[] MTDBuf;
MTDBuf = NULL;
}
if (msg2 == "")
snprintf(buf, sizeof(buf), "%s\n", msg1.c_str());
@@ -276,19 +284,19 @@ bool CExtUpdate::applySettings()
}
int MTDBufSize = 0xFFFF;
char *MTDBuf = new char[MTDBufSize];
MTDBuf = new char[MTDBufSize];
// copy image to mtdblock
if (MTDBuf == NULL)
return ErrorReset(RESET_UNLOAD, "malloc error");
return ErrorReset(RESET_UNLOAD, "memory allocation error");
fd1 = open(imgFilename.c_str(), O_RDONLY);
if (fd1 < 0)
return ErrorReset(RESET_UNLOAD, "cannot read image file: " + imgFilename);
return ErrorReset(RESET_UNLOAD | DELETE_MTDBUF, "cannot read image file: " + imgFilename);
long filesize = lseek(fd1, 0, SEEK_END);
lseek(fd1, 0, SEEK_SET);
if(filesize == 0)
return ErrorReset(RESET_UNLOAD | RESET_FD1, "image filesize is 0");
return ErrorReset(RESET_UNLOAD | CLOSE_FD1 | DELETE_MTDBUF, "image filesize is 0");
if(filesize > mtdSize)
return ErrorReset(RESET_UNLOAD | RESET_FD1, "image filesize too large");
return ErrorReset(RESET_UNLOAD | CLOSE_FD1 | DELETE_MTDBUF, "image filesize too large");
fd2 = -1;
int tmpCount = 0;
while (fd2 < 0) {
@@ -299,7 +307,7 @@ bool CExtUpdate::applySettings()
sleep(1);
}
if (fd2 < 0)
return ErrorReset(RESET_UNLOAD | RESET_FD1, "cannot open mtdBlock");
return ErrorReset(RESET_UNLOAD | CLOSE_FD1 | DELETE_MTDBUF, "cannot open mtdBlock");
long fsize = filesize;
long block;
while(fsize > 0) {
@@ -316,12 +324,15 @@ bool CExtUpdate::applySettings()
FileHelpers->createDir(mountPkt.c_str(), 0755);
int res = mount(mtdBlockFileName.c_str(), mountPkt.c_str(), "jffs2", 0, NULL);
if (res)
return ErrorReset(RESET_UNLOAD, "mount error");
return ErrorReset(RESET_UNLOAD | DELETE_MTDBUF, "mount error");
if (get_fs_usage(mountPkt.c_str(), total, used, &bsize))
free1 = (total * bsize) / 1024 - (used * bsize) / 1024;
if (!readBackupList(mountPkt)) {
if (MTDBuf != NULL)
delete[] MTDBuf;
MTDBuf = NULL;
if (flashErrorFlag)
return false;
return ErrorReset(0, "error readBackupList");
@@ -329,20 +340,20 @@ bool CExtUpdate::applySettings()
res = umount(mountPkt.c_str());
if (res)
return ErrorReset(RESET_UNLOAD, "unmount error");
return ErrorReset(RESET_UNLOAD | DELETE_MTDBUF, "unmount error");
unlink(imgFilename.c_str());
// copy mtdblock to image
if (MTDBuf == NULL)
return ErrorReset(RESET_UNLOAD, "malloc error");
return ErrorReset(RESET_UNLOAD, "memory allocation error");
fd1 = open(mtdBlockFileName.c_str(), O_RDONLY);
if (fd1 < 0)
return ErrorReset(RESET_UNLOAD, "cannot read mtdBlock");
return ErrorReset(RESET_UNLOAD | DELETE_MTDBUF, "cannot read mtdBlock");
fsize = mtdRamSize;
fd2 = open(imgFilename.c_str(), O_WRONLY | O_CREAT);
if (fd2 < 0)
return ErrorReset(RESET_UNLOAD | RESET_FD1, "cannot open image file: ", imgFilename);
return ErrorReset(RESET_UNLOAD | CLOSE_FD1 | DELETE_MTDBUF, "cannot open image file: ", imgFilename);
while(fsize > 0) {
block = fsize;
if(block > (long)MTDBufSize)
@@ -358,7 +369,7 @@ bool CExtUpdate::applySettings()
// check image file size
if (mtdRamSize != fsizeDst) {
unlink(imgFilename.c_str());
return ErrorReset(0, "error file size: ", imgFilename);
return ErrorReset(DELETE_MTDBUF, "error file size: ", imgFilename);
}
// unload mtdramDriver only
@@ -367,7 +378,8 @@ bool CExtUpdate::applySettings()
if(hintBox)
hintBox->hide();
delete[] MTDBuf;
if (MTDBuf != NULL)
delete[] MTDBuf;
MTDBuf = NULL;
sync();
return true;
@@ -546,7 +558,7 @@ bool CExtUpdate::readBackupList(const std::string & dstPath)
fgetpos(f1, &fz);
fseek(f1, 0, SEEK_SET);
if (fz.__pos == 0)
return ErrorReset(RESET_F1, "backuplist filesize is 0");
return ErrorReset(CLOSE_F1, "backuplist filesize is 0");
size_t pos;
std::string line;

View File

@@ -39,15 +39,17 @@ class CExtUpdate
enum
{
RESET_UNLOAD = 1,
RESET_FD1 = 2,
RESET_FD2 = 4,
RESET_F1 = 8
CLOSE_FD1 = 2,
CLOSE_FD2 = 4,
CLOSE_F1 = 8,
DELETE_MTDBUF = 16
};
std::string imgFilename;
std::string mtdRamError;
int mtdNumber;
int fd1, fd2;
FILE *f1;
char *MTDBuf;
std::string mtdramDriver;
std::string backupList, defaultBackup;
std::string mountPkt;