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

View File

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