Browse Source

Avoid overwrite of the error message in AbortFn

The AbortFn() used to overwrite the error message, hiding the real
failure reported in ErrorAbort(). And we will miss the failure in
the script patterns like 'blockimageupdate() || abort()'

We will ensure there's one line break at the end of ErrorAbort's
error message; and append to the existing error message when calling
abort().

Test: Message from ErrorAbort shows up in the log
Change-Id: I3aebd06629c5129330250c7fe5e8cdead2ae85bc
Tianjie Xu 3 years ago
parent
commit
5ad802839d
3 changed files with 26 additions and 23 deletions
  1. 11
    8
      edify/expr.cpp
  2. 9
    9
      updater/blockimg.cpp
  3. 6
    6
      updater/install.cpp

+ 11
- 8
edify/expr.cpp View File

@@ -114,9 +114,9 @@ Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_pt
114 114
 Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
115 115
     std::string msg;
116 116
     if (!argv.empty() && Evaluate(state, argv[0], &msg)) {
117
-        state->errmsg = msg;
117
+      state->errmsg += msg;
118 118
     } else {
119
-        state->errmsg = "called abort()";
119
+      state->errmsg += "called abort()";
120 120
     }
121 121
     return nullptr;
122 122
 }
@@ -410,12 +410,15 @@ Value* ErrorAbort(State* state, const char* format, ...) {
410 410
 }
411 411
 
412 412
 Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
413
-    va_list ap;
414
-    va_start(ap, format);
415
-    android::base::StringAppendV(&state->errmsg, format, ap);
416
-    va_end(ap);
417
-    state->cause_code = cause_code;
418
-    return nullptr;
413
+  std::string err_message;
414
+  va_list ap;
415
+  va_start(ap, format);
416
+  android::base::StringAppendV(&err_message, format, ap);
417
+  va_end(ap);
418
+  // Ensure that there's exactly one line break at the end of the error message.
419
+  state->errmsg = android::base::Trim(err_message) + "\n";
420
+  state->cause_code = cause_code;
421
+  return nullptr;
419 422
 }
420 423
 
421 424
 State::State(const std::string& script, void* cookie)

+ 9
- 9
updater/blockimg.cpp View File

@@ -817,7 +817,7 @@ static int CreateStash(State* state, size_t maxblocks, const std::string& blockd
817 817
   size_t max_stash_size = maxblocks * BLOCKSIZE;
818 818
 
819 819
   if (res == -1 && errno != ENOENT) {
820
-    ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s\n", dirname.c_str(),
820
+    ErrorAbort(state, kStashCreationFailure, "stat \"%s\" failed: %s", dirname.c_str(),
821 821
                strerror(errno));
822 822
     return -1;
823 823
   } else if (res != 0) {
@@ -825,19 +825,19 @@ static int CreateStash(State* state, size_t maxblocks, const std::string& blockd
825 825
     res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
826 826
 
827 827
     if (res != 0) {
828
-      ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s\n", dirname.c_str(),
828
+      ErrorAbort(state, kStashCreationFailure, "mkdir \"%s\" failed: %s", dirname.c_str(),
829 829
                  strerror(errno));
830 830
       return -1;
831 831
     }
832 832
 
833 833
     if (chown(dirname.c_str(), AID_SYSTEM, AID_SYSTEM) != 0) {  // system user
834
-      ErrorAbort(state, kStashCreationFailure, "chown \"%s\" failed: %s\n", dirname.c_str(),
834
+      ErrorAbort(state, kStashCreationFailure, "chown \"%s\" failed: %s", dirname.c_str(),
835 835
                  strerror(errno));
836 836
       return -1;
837 837
     }
838 838
 
839 839
     if (CacheSizeCheck(max_stash_size) != 0) {
840
-      ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)\n",
840
+      ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)",
841 841
                  max_stash_size);
842 842
       return -1;
843 843
     }
@@ -869,7 +869,7 @@ static int CreateStash(State* state, size_t maxblocks, const std::string& blockd
869 869
   if (max_stash_size > existing) {
870 870
     size_t needed = max_stash_size - existing;
871 871
     if (CacheSizeCheck(needed) != 0) {
872
-      ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)\n",
872
+      ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)",
873 873
                  needed);
874 874
       return -1;
875 875
     }
@@ -1517,7 +1517,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state,
1517 1517
 
1518 1518
   std::vector<std::string> lines = android::base::Split(transfer_list_value->data, "\n");
1519 1519
   if (lines.size() < 2) {
1520
-    ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]\n",
1520
+    ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zd]",
1521 1521
                lines.size());
1522 1522
     return StringValue("");
1523 1523
   }
@@ -1533,7 +1533,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state,
1533 1533
   // Second line in transfer list is the total number of blocks we expect to write.
1534 1534
   size_t total_blocks;
1535 1535
   if (!android::base::ParseUint(lines[1], &total_blocks)) {
1536
-    ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]\n", lines[1].c_str());
1536
+    ErrorAbort(state, kArgsParsingFailure, "unexpected block count [%s]", lines[1].c_str());
1537 1537
     return StringValue("");
1538 1538
   }
1539 1539
 
@@ -1543,7 +1543,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state,
1543 1543
 
1544 1544
   size_t start = 2;
1545 1545
   if (lines.size() < 4) {
1546
-    ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]\n",
1546
+    ErrorAbort(state, kArgsParsingFailure, "too few lines in the transfer list [%zu]",
1547 1547
                lines.size());
1548 1548
     return StringValue("");
1549 1549
   }
@@ -1554,7 +1554,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state,
1554 1554
   // Fourth line is the maximum number of blocks that will be stashed simultaneously
1555 1555
   size_t stash_max_blocks;
1556 1556
   if (!android::base::ParseUint(lines[3], &stash_max_blocks)) {
1557
-    ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]\n",
1557
+    ErrorAbort(state, kArgsParsingFailure, "unexpected maximum stash blocks [%s]",
1558 1558
                lines[3].c_str());
1559 1559
     return StringValue("");
1560 1560
   }

+ 6
- 6
updater/install.cpp View File

@@ -268,7 +268,7 @@ Value* FormatFn(const char* name, State* state, const std::vector<std::unique_pt
268 268
 
269 269
   int64_t size;
270 270
   if (!android::base::ParseInt(fs_size, &size)) {
271
-    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
271
+    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
272 272
                       fs_size.c_str());
273 273
   }
274 274
 
@@ -352,12 +352,12 @@ Value* ShowProgressFn(const char* name, State* state,
352 352
 
353 353
   double frac;
354 354
   if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
355
-    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
355
+    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
356 356
                       frac_str.c_str());
357 357
   }
358 358
   int sec;
359 359
   if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
360
-    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
360
+    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
361 361
                       sec_str.c_str());
362 362
   }
363 363
 
@@ -380,7 +380,7 @@ Value* SetProgressFn(const char* name, State* state, const std::vector<std::uniq
380 380
 
381 381
   double frac;
382 382
   if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
383
-    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
383
+    return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
384 384
                       frac_str.c_str());
385 385
   }
386 386
 
@@ -574,8 +574,8 @@ Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::
574 574
 
575 575
   size_t bytes;
576 576
   if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
577
-    return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
578
-                      name, bytes_str.c_str());
577
+    return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
578
+                      bytes_str.c_str());
579 579
   }
580 580
 
581 581
   // Skip the cache size check if the update is a retry.