From bca7d9fcb23698d918f9f65dfc0e742af7f9516d Mon Sep 17 00:00:00 2001 From: Jeon Sang-Heon Date: Mon, 9 Mar 2020 00:56:14 +0000 Subject: [PATCH] Revert heap to stack memory array in function Change-Id: I6a3df69f6bfa207c2438b92884b7d5f96ef9f05d --- bsdiff/ss_bspatch_common.c | 31 +++++++------------------------ ss_engine/SS_FSUpdate.c | 14 ++------------ 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/bsdiff/ss_bspatch_common.c b/bsdiff/ss_bspatch_common.c index af6e20b..e7bd45e 100755 --- a/bsdiff/ss_bspatch_common.c +++ b/bsdiff/ss_bspatch_common.c @@ -96,23 +96,12 @@ SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream, { int thereIsSize = (*unpackSize != (UInt64)(Int64) - 1); UInt64 offset = 0; - Byte *inBuf = NULL; - Byte *outBuf = NULL; + Byte inBuf[IN_BUF_SIZE]; + Byte outBuf[OUT_BUF_SIZE]; size_t inPos = 0, inSize = 0, outPos = 0; - SRes res; - - inBuf = (Byte *)malloc(sizeof(Byte) * IN_BUF_SIZE); - if (!inBuf) { - res = SZ_ERROR_UNSUPPORTED; - goto clean; - } - outBuf = (Byte *)malloc(sizeof(Byte) * OUT_BUF_SIZE); - if (!outBuf) { - res = SZ_ERROR_UNSUPPORTED; - goto clean; - } LzmaDec_Init(state); + offset = 0; for (;;) { @@ -122,6 +111,7 @@ SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream, inPos = 0; } + SRes res; SizeT inProcessed = inSize - inPos; SizeT outProcessed = OUT_BUF_SIZE - outPos; ELzmaFinishMode finishMode = LZMA_FINISH_ANY; @@ -143,21 +133,14 @@ SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream, outPos = 0; if ((res != SZ_OK) || (thereIsSize && *unpackSize == 0)) - goto clean; + return res; if (inProcessed == 0 && outProcessed == 0) { if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK) - res = SZ_ERROR_DATA; - goto clean; + return SZ_ERROR_DATA; + return res; } } - -clean: - if (inBuf) - free(inBuf); - if (outBuf) - free(outBuf); - return res; } int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char **dest_buf, ssize_t *dest_size) diff --git a/ss_engine/SS_FSUpdate.c b/ss_engine/SS_FSUpdate.c index a4150b7..ee8c6d0 100755 --- a/ss_engine/SS_FSUpdate.c +++ b/ss_engine/SS_FSUpdate.c @@ -134,8 +134,7 @@ SS_CopyFile(const char *strFromPath, const char *strToPath) { int fd1, fd2; int readCount = 0, writeCount = 0; - char *buf; // copy 32KB wise - int buf_size = (1 << 15); + char buf[1 << 15]; // copy 32KB wise int ret = 0; char path1[MAX_PATH] = { '\0' }; @@ -161,15 +160,7 @@ SS_CopyFile(const char *strFromPath, const char *strToPath) return E_SS_WRITE_ERROR; } - buf = (char *)malloc(sizeof(char) * buf_size); - if (!buf) { - close(fd1); - close(fd2); - LOGE("Cannot allocate buf"); - return -1; - } - - while ((readCount = read(fd1, buf, buf_size)) > 0) { + while ((readCount = read(fd1, buf, sizeof(buf))) > 0) { writeCount = write(fd2, buf, readCount); if (writeCount != readCount) { LOGE(" read %d, but write %d, abort.\n", readCount, @@ -179,7 +170,6 @@ SS_CopyFile(const char *strFromPath, const char *strToPath) } } - free(buf); close(fd1); fsync(fd2); close(fd2); -- 2.7.4