summaryrefslogtreecommitdiffstats
path: root/applypatch/bspatch.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2016-02-05 23:54:36 +0100
committerGerrit Code Review <noreply-gerritcodereview@google.com>2016-02-05 23:54:36 +0100
commit6b89aa7662186f940f2ae35d479598601dc28a07 (patch)
tree9d174f1c2cc75d239d0034853f372f590085c24d /applypatch/bspatch.cpp
parentMerge "Refactor existing tests to use gtest" (diff)
parentapplypatch: fix memory leaks reported by static analysis. (diff)
downloadandroid_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar.gz
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar.bz2
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar.lz
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar.xz
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.tar.zst
android_bootable_recovery-6b89aa7662186f940f2ae35d479598601dc28a07.zip
Diffstat (limited to 'applypatch/bspatch.cpp')
-rw-r--r--applypatch/bspatch.cpp37
1 files changed, 14 insertions, 23 deletions
diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp
index 25171170a..ebb55f1d1 100644
--- a/applypatch/bspatch.cpp
+++ b/applypatch/bspatch.cpp
@@ -24,7 +24,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
-#include <malloc.h>
#include <unistd.h>
#include <string.h>
@@ -103,26 +102,22 @@ int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset,
SinkFn sink, void* token, SHA_CTX* ctx) {
- unsigned char* new_data;
- ssize_t new_size;
- if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset,
- &new_data, &new_size) != 0) {
+ std::vector<unsigned char> new_data;
+ if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset, &new_data) != 0) {
return -1;
}
- if (sink(new_data, new_size, token) < new_size) {
+ if (sink(new_data.data(), new_data.size(), token) < static_cast<ssize_t>(new_data.size())) {
printf("short write of output: %d (%s)\n", errno, strerror(errno));
return 1;
}
- if (ctx) SHA1_Update(ctx, new_data, new_size);
- free(new_data);
-
+ if (ctx) SHA1_Update(ctx, new_data.data(), new_data.size());
return 0;
}
int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset,
- unsigned char** new_data, ssize_t* new_size) {
+ std::vector<unsigned char>* new_data) {
// Patch data format:
// 0 8 "BSDIFF40"
// 8 8 X
@@ -141,12 +136,12 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
return 1;
}
- ssize_t ctrl_len, data_len;
+ ssize_t ctrl_len, data_len, new_size;
ctrl_len = offtin(header+8);
data_len = offtin(header+16);
- *new_size = offtin(header+24);
+ new_size = offtin(header+24);
- if (ctrl_len < 0 || data_len < 0 || *new_size < 0) {
+ if (ctrl_len < 0 || data_len < 0 || new_size < 0) {
printf("corrupt patch file header (data lengths)\n");
return 1;
}
@@ -183,18 +178,14 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
printf("failed to bzinit extra stream (%d)\n", bzerr);
}
- *new_data = reinterpret_cast<unsigned char*>(malloc(*new_size));
- if (*new_data == NULL) {
- printf("failed to allocate %zd bytes of memory for output file\n", *new_size);
- return 1;
- }
+ new_data->resize(new_size);
off_t oldpos = 0, newpos = 0;
off_t ctrl[3];
off_t len_read;
int i;
unsigned char buf[24];
- while (newpos < *new_size) {
+ while (newpos < new_size) {
// Read control data
if (FillBuffer(buf, 24, &cstream) != 0) {
printf("error while reading control stream\n");
@@ -210,13 +201,13 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
}
// Sanity check
- if (newpos + ctrl[0] > *new_size) {
+ if (newpos + ctrl[0] > new_size) {
printf("corrupt patch (new file overrun)\n");
return 1;
}
// Read diff string
- if (FillBuffer(*new_data + newpos, ctrl[0], &dstream) != 0) {
+ if (FillBuffer(new_data->data() + newpos, ctrl[0], &dstream) != 0) {
printf("error while reading diff stream\n");
return 1;
}
@@ -233,13 +224,13 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
oldpos += ctrl[0];
// Sanity check
- if (newpos + ctrl[1] > *new_size) {
+ if (newpos + ctrl[1] > new_size) {
printf("corrupt patch (new file overrun)\n");
return 1;
}
// Read extra string
- if (FillBuffer(*new_data + newpos, ctrl[1], &estream) != 0) {
+ if (FillBuffer(new_data->data() + newpos, ctrl[1], &estream) != 0) {
printf("error while reading extra stream\n");
return 1;
}