diff options
author | Tao Bao | 2017-11-04 02:08:08 -0500 |
---|---|---|
committer | Tao Bao | 2017-11-07 14:50:02 -0600 |
commit | 6798315327690fdbe93add15159be5b925779bfe (patch) | |
tree | 521c5e1d88928db38f3a7aaa8e3dea76e307e78d /updater | |
parent | 73dca3e983d5ec2eaf01572eac57dbdb55367f22 (diff) | |
download | platform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.tar.gz platform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.tar.xz platform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.zip |
otautil: Remove the aborts in RangeSet::Parse().
We used to CHECK and abort on parsing errors. While it works fine for
the updater use case (because recovery starts updater in a forked
process and collects the process exit code), it's difficult for other
clients to use RangeSet as a library (e.g. update_verifier).
This CL switches the aborts to returning empty RangeSet instead. Callers
need to check the parsing results explicitly.
The CL also separates RangeSet::PushBack() into a function, and moves
SortedRangeSet::Clear() into RangeSet.
Test: recovery_unit_test
Test: Sideload an OTA package with the new updater on angler.
Test: Sideload an OTA package with injected range string errors. The
updater aborts from the explicit checks.
Change-Id: If2b7f6f41dc93af917a21c7877a83e98dc3fd016
Diffstat (limited to 'updater')
-rw-r--r-- | updater/blockimg.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index 6c7b3efc..1c931afe 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp | |||
@@ -492,6 +492,10 @@ static void PrintHashForCorruptedSourceBlocks(const CommandParameters& params, | |||
492 | } | 492 | } |
493 | 493 | ||
494 | RangeSet src = RangeSet::Parse(params.tokens[pos++]); | 494 | RangeSet src = RangeSet::Parse(params.tokens[pos++]); |
495 | if (!src) { | ||
496 | LOG(ERROR) << "Failed to parse range in " << params.cmdline; | ||
497 | return; | ||
498 | } | ||
495 | 499 | ||
496 | RangeSet locs; | 500 | RangeSet locs; |
497 | // If there's no stashed blocks, content in the buffer is consecutive and has the same | 501 | // If there's no stashed blocks, content in the buffer is consecutive and has the same |
@@ -936,6 +940,7 @@ static int LoadSourceBlocks(CommandParameters& params, const RangeSet& tgt, size | |||
936 | params.cpos++; | 940 | params.cpos++; |
937 | } else { | 941 | } else { |
938 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); | 942 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); |
943 | CHECK(static_cast<bool>(src)); | ||
939 | *overlap = src.Overlaps(tgt); | 944 | *overlap = src.Overlaps(tgt); |
940 | 945 | ||
941 | if (ReadBlocks(src, params.buffer, params.fd) == -1) { | 946 | if (ReadBlocks(src, params.buffer, params.fd) == -1) { |
@@ -948,6 +953,7 @@ static int LoadSourceBlocks(CommandParameters& params, const RangeSet& tgt, size | |||
948 | } | 953 | } |
949 | 954 | ||
950 | RangeSet locs = RangeSet::Parse(params.tokens[params.cpos++]); | 955 | RangeSet locs = RangeSet::Parse(params.tokens[params.cpos++]); |
956 | CHECK(static_cast<bool>(locs)); | ||
951 | MoveRange(params.buffer, locs, params.buffer); | 957 | MoveRange(params.buffer, locs, params.buffer); |
952 | } | 958 | } |
953 | 959 | ||
@@ -970,6 +976,7 @@ static int LoadSourceBlocks(CommandParameters& params, const RangeSet& tgt, size | |||
970 | } | 976 | } |
971 | 977 | ||
972 | RangeSet locs = RangeSet::Parse(tokens[1]); | 978 | RangeSet locs = RangeSet::Parse(tokens[1]); |
979 | CHECK(static_cast<bool>(locs)); | ||
973 | MoveRange(params.buffer, locs, stash); | 980 | MoveRange(params.buffer, locs, stash); |
974 | } | 981 | } |
975 | 982 | ||
@@ -1034,6 +1041,7 @@ static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t* | |||
1034 | 1041 | ||
1035 | // <tgt_range> | 1042 | // <tgt_range> |
1036 | tgt = RangeSet::Parse(params.tokens[params.cpos++]); | 1043 | tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
1044 | CHECK(static_cast<bool>(tgt)); | ||
1037 | 1045 | ||
1038 | std::vector<uint8_t> tgtbuffer(tgt.blocks() * BLOCKSIZE); | 1046 | std::vector<uint8_t> tgtbuffer(tgt.blocks() * BLOCKSIZE); |
1039 | if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) { | 1047 | if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) { |
@@ -1146,6 +1154,7 @@ static int PerformCommandStash(CommandParameters& params) { | |||
1146 | } | 1154 | } |
1147 | 1155 | ||
1148 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); | 1156 | RangeSet src = RangeSet::Parse(params.tokens[params.cpos++]); |
1157 | CHECK(static_cast<bool>(src)); | ||
1149 | 1158 | ||
1150 | allocate(src.blocks() * BLOCKSIZE, params.buffer); | 1159 | allocate(src.blocks() * BLOCKSIZE, params.buffer); |
1151 | if (ReadBlocks(src, params.buffer, params.fd) == -1) { | 1160 | if (ReadBlocks(src, params.buffer, params.fd) == -1) { |
@@ -1196,6 +1205,7 @@ static int PerformCommandZero(CommandParameters& params) { | |||
1196 | } | 1205 | } |
1197 | 1206 | ||
1198 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); | 1207 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
1208 | CHECK(static_cast<bool>(tgt)); | ||
1199 | 1209 | ||
1200 | LOG(INFO) << " zeroing " << tgt.blocks() << " blocks"; | 1210 | LOG(INFO) << " zeroing " << tgt.blocks() << " blocks"; |
1201 | 1211 | ||
@@ -1238,6 +1248,7 @@ static int PerformCommandNew(CommandParameters& params) { | |||
1238 | } | 1248 | } |
1239 | 1249 | ||
1240 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); | 1250 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
1251 | CHECK(static_cast<bool>(tgt)); | ||
1241 | 1252 | ||
1242 | if (params.canwrite) { | 1253 | if (params.canwrite) { |
1243 | LOG(INFO) << " writing " << tgt.blocks() << " blocks of new data"; | 1254 | LOG(INFO) << " writing " << tgt.blocks() << " blocks of new data"; |
@@ -1368,6 +1379,7 @@ static int PerformCommandErase(CommandParameters& params) { | |||
1368 | } | 1379 | } |
1369 | 1380 | ||
1370 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); | 1381 | RangeSet tgt = RangeSet::Parse(params.tokens[params.cpos++]); |
1382 | CHECK(static_cast<bool>(tgt)); | ||
1371 | 1383 | ||
1372 | if (params.canwrite) { | 1384 | if (params.canwrite) { |
1373 | LOG(INFO) << " erasing " << tgt.blocks() << " blocks"; | 1385 | LOG(INFO) << " erasing " << tgt.blocks() << " blocks"; |
@@ -1773,6 +1785,7 @@ Value* RangeSha1Fn(const char* name, State* state, const std::vector<std::unique | |||
1773 | } | 1785 | } |
1774 | 1786 | ||
1775 | RangeSet rs = RangeSet::Parse(ranges->data); | 1787 | RangeSet rs = RangeSet::Parse(ranges->data); |
1788 | CHECK(static_cast<bool>(rs)); | ||
1776 | 1789 | ||
1777 | SHA_CTX ctx; | 1790 | SHA_CTX ctx; |
1778 | SHA1_Init(&ctx); | 1791 | SHA1_Init(&ctx); |
@@ -1884,6 +1897,11 @@ Value* BlockImageRecoverFn(const char* name, State* state, | |||
1884 | ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name); | 1897 | ErrorAbort(state, kArgsParsingFailure, "ranges argument to %s must be string", name); |
1885 | return StringValue(""); | 1898 | return StringValue(""); |
1886 | } | 1899 | } |
1900 | RangeSet rs = RangeSet::Parse(ranges->data); | ||
1901 | if (!rs) { | ||
1902 | ErrorAbort(state, kArgsParsingFailure, "failed to parse ranges: %s", ranges->data.c_str()); | ||
1903 | return StringValue(""); | ||
1904 | } | ||
1887 | 1905 | ||
1888 | // Output notice to log when recover is attempted | 1906 | // Output notice to log when recover is attempted |
1889 | LOG(INFO) << filename->data << " image corrupted, attempting to recover..."; | 1907 | LOG(INFO) << filename->data << " image corrupted, attempting to recover..."; |
@@ -1909,7 +1927,7 @@ Value* BlockImageRecoverFn(const char* name, State* state, | |||
1909 | } | 1927 | } |
1910 | 1928 | ||
1911 | uint8_t buffer[BLOCKSIZE]; | 1929 | uint8_t buffer[BLOCKSIZE]; |
1912 | for (const auto& range : RangeSet::Parse(ranges->data)) { | 1930 | for (const auto& range : rs) { |
1913 | for (size_t j = range.first; j < range.second; ++j) { | 1931 | for (size_t j = range.first; j < range.second; ++j) { |
1914 | // Stay within the data area, libfec validates and corrects metadata | 1932 | // Stay within the data area, libfec validates and corrects metadata |
1915 | if (status.data_size <= static_cast<uint64_t>(j) * BLOCKSIZE) { | 1933 | if (status.data_size <= static_cast<uint64_t>(j) * BLOCKSIZE) { |