Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Unified Diff: third_party/bspatch/mbspatch.cc

Issue 21049006: Improve error handling in bspatch. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/bspatch/README.chromium ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/bspatch/mbspatch.cc
===================================================================
--- third_party/bspatch/mbspatch.cc (revision 214181)
+++ third_party/bspatch/mbspatch.cc (working copy)
@@ -211,35 +211,64 @@
int ApplyBinaryPatch(const wchar_t *old_file, const wchar_t *patch_file,
const wchar_t *new_file) {
- int ret = 0;
+ int ret = OK;
+ int ofd = -1;
+ int nfd = -1;
+ unsigned char *buf = NULL;
+
int pfd = _wopen(patch_file, O_RDONLY | _O_BINARY);
if (pfd < 0) return READ_ERROR;
- MBSPatchHeader header;
- if (ret = MBS_ReadHeader(pfd, &header)) return ret;
+ do {
+ MBSPatchHeader header;
+ if ((ret = MBS_ReadHeader(pfd, &header)))
+ break;
- int ofd = _wopen(old_file, O_RDONLY | _O_BINARY);
- if (ofd < 0) return READ_ERROR;
+ ofd = _wopen(old_file, O_RDONLY | _O_BINARY);
+ if (ofd < 0) {
+ ret = READ_ERROR;
+ break;
+ }
- struct stat os;
- if (ret = fstat(ofd, &os)) return READ_ERROR;
- if (os.st_size != header.slen) return UNEXPECTED_ERROR;
+ struct stat os;
+ if ((ret = fstat(ofd, &os))) {
+ ret = READ_ERROR;
+ break;
+ }
- unsigned char *buf = (unsigned char*) malloc(header.slen);
- if (!buf) return MEM_ERROR;
+ if (os.st_size != header.slen) {
+ ret = UNEXPECTED_ERROR;
+ break;
+ }
- if (read(ofd, buf, header.slen) != header.slen) return READ_ERROR;
- if (CalculateCrc(buf, header.slen) != header.scrc32)
- return CRC_ERROR;
+ buf = (unsigned char*) malloc(header.slen);
+ if (!buf) {
+ ret = MEM_ERROR;
+ break;
+ }
- int nfd = _wopen(new_file, O_WRONLY | O_TRUNC | O_CREAT | _O_BINARY);
- if (nfd < 0) return READ_ERROR;
+ if (read(ofd, buf, header.slen) != header.slen) {
+ ret = READ_ERROR;
+ break;
+ }
- MBS_ApplyPatch(&header, pfd, buf, nfd);
+ if (CalculateCrc(buf, header.slen) != header.scrc32) {
+ ret = CRC_ERROR;
+ break;
+ }
+ nfd = _wopen(new_file, O_WRONLY | O_TRUNC | O_CREAT | _O_BINARY);
+ if (nfd < 0) {
+ ret = READ_ERROR;
+ break;
+ }
+
+ MBS_ApplyPatch(&header, pfd, buf, nfd);
+ } while (0);
Avi (use Gerrit) 2013/07/29 14:46:21 Huh. I don't think I've seen this style anywhere i
+
free(buf);
close(pfd);
- close(ofd);
- close(nfd);
- return OK;
+ if (ofd >= 0) close(ofd);
+ if (nfd >= 0) close(nfd);
+ return ret;
}
« no previous file with comments | « third_party/bspatch/README.chromium ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698