summaryrefslogtreecommitdiffstats
path: root/applypatch
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2017-01-04 23:05:26 +0100
committerandroid-build-merger <android-build-merger@google.com>2017-01-04 23:05:26 +0100
commit4d37763c698b65a709f4d462e3afe2c00c4990c5 (patch)
treeffa173ead27e8e35e3078c19ed464c8a2e105220 /applypatch
parentMerge "Write aliases before ffs mount in recovery" (diff)
parentMerge "imgdiff: Fix an edge case that leads to infinite loop." (diff)
downloadandroid_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar.gz
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar.bz2
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar.lz
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar.xz
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.tar.zst
android_bootable_recovery-4d37763c698b65a709f4d462e3afe2c00c4990c5.zip
Diffstat (limited to 'applypatch')
-rw-r--r--applypatch/Android.mk1
-rw-r--r--applypatch/imgdiff.cpp25
2 files changed, 11 insertions, 15 deletions
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index 61e110617..ec3c6ee38 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -124,6 +124,7 @@ libimgdiff_cflags := \
libimgdiff_static_libraries := \
libbsdiff \
+ libbase \
libz
# libimgdiff (static library)
diff --git a/applypatch/imgdiff.cpp b/applypatch/imgdiff.cpp
index 18a15a164..62de726a4 100644
--- a/applypatch/imgdiff.cpp
+++ b/applypatch/imgdiff.cpp
@@ -124,6 +124,7 @@
#include "applypatch/imgdiff.h"
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -131,6 +132,9 @@
#include <sys/types.h>
#include <unistd.h>
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+
#include <bsdiff.h>
#include <zlib.h>
@@ -382,19 +386,12 @@ unsigned char* ReadImage(const char* filename, int* num_chunks, ImageChunk** chu
}
size_t sz = static_cast<size_t>(st.st_size);
- unsigned char* img = static_cast<unsigned char*>(malloc(sz + 4));
- FILE* f = fopen(filename, "rb");
- if (fread(img, 1, sz, f) != sz) {
+ unsigned char* img = static_cast<unsigned char*>(malloc(sz));
+ android::base::unique_fd fd(open(filename, O_RDONLY));
+ if (!android::base::ReadFully(fd, img, sz)) {
printf("failed to read \"%s\" %s\n", filename, strerror(errno));
- fclose(f);
- return NULL;
+ return nullptr;
}
- fclose(f);
-
- // append 4 zero bytes to the data so we can always search for the
- // four-byte string 1f8b0800 starting at any point in the actual
- // file data, without special-casing the end of the data.
- memset(img+sz, 0, 4);
size_t pos = 0;
@@ -518,10 +515,8 @@ unsigned char* ReadImage(const char* filename, int* num_chunks, ImageChunk** chu
curr->data = p;
for (curr->len = 0; curr->len < (sz - pos); ++curr->len) {
- if (p[curr->len] == 0x1f &&
- p[curr->len+1] == 0x8b &&
- p[curr->len+2] == 0x08 &&
- p[curr->len+3] == 0x00) {
+ if (sz - pos >= 4 && p[curr->len] == 0x1f && p[curr->len + 1] == 0x8b &&
+ p[curr->len + 2] == 0x08 && p[curr->len + 3] == 0x00) {
break;
}
}