aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2016-10-03 20:06:46 -0500
committerTao Bao2016-10-04 16:24:32 -0500
commit59dcb9cbea8fb70ab933fd10d35582b08cd13f37 (patch)
tree64a8c1e78aa282460aebf39d3f4147d00ffa0fcc /edify/expr.cpp
parent38b923ff6caafbb92935e7d13640d6b05f2a7466 (diff)
downloadplatform-bootable-recovery-59dcb9cbea8fb70ab933fd10d35582b08cd13f37.tar.gz
platform-bootable-recovery-59dcb9cbea8fb70ab933fd10d35582b08cd13f37.tar.xz
platform-bootable-recovery-59dcb9cbea8fb70ab933fd10d35582b08cd13f37.zip
edify: Move State.script and State.errmsg to std::string.
This way we kill a few strdup() and free() calls. Test: 1. recovery_component_test still passes; 2. Applying an update with the new updater works; 3. The error code in a script with abort("E310: xyz") is recorded into last_install correctly. Change-Id: Ibda4da5937346e058a0d7cc81764d6f02920010a
Diffstat (limited to 'edify/expr.cpp')
-rw-r--r--edify/expr.cpp39
1 files changed, 13 insertions, 26 deletions
diff --git a/edify/expr.cpp b/edify/expr.cpp
index ecb1bea1..aa3a55a8 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -107,8 +107,7 @@ Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
107 107
108Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) { 108Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
109 if (argc != 2 && argc != 3) { 109 if (argc != 2 && argc != 3) {
110 free(state->errmsg); 110 state->errmsg = "ifelse expects 2 or 3 arguments";
111 state->errmsg = strdup("ifelse expects 2 or 3 arguments");
112 return NULL; 111 return NULL;
113 } 112 }
114 char* cond = Evaluate(state, argv[0]); 113 char* cond = Evaluate(state, argv[0]);
@@ -134,11 +133,10 @@ Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
134 if (argc > 0) { 133 if (argc > 0) {
135 msg = Evaluate(state, argv[0]); 134 msg = Evaluate(state, argv[0]);
136 } 135 }
137 free(state->errmsg);
138 if (msg) { 136 if (msg) {
139 state->errmsg = msg; 137 state->errmsg = msg;
140 } else { 138 } else {
141 state->errmsg = strdup("called abort()"); 139 state->errmsg = "called abort()";
142 } 140 }
143 return NULL; 141 return NULL;
144} 142}
@@ -153,15 +151,8 @@ Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
153 int b = BooleanString(v); 151 int b = BooleanString(v);
154 free(v); 152 free(v);
155 if (!b) { 153 if (!b) {
156 int prefix_len;
157 int len = argv[i]->end - argv[i]->start; 154 int len = argv[i]->end - argv[i]->start;
158 char* err_src = reinterpret_cast<char*>(malloc(len + 20)); 155 state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
159 strcpy(err_src, "assert failed: ");
160 prefix_len = strlen(err_src);
161 memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
162 err_src[prefix_len + len] = '\0';
163 free(state->errmsg);
164 state->errmsg = err_src;
165 return NULL; 156 return NULL;
166 } 157 }
167 } 158 }
@@ -279,8 +270,7 @@ Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
279 270
280Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) { 271Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
281 if (argc != 2) { 272 if (argc != 2) {
282 free(state->errmsg); 273 state->errmsg = "less_than_int expects 2 arguments";
283 state->errmsg = strdup("less_than_int expects 2 arguments");
284 return NULL; 274 return NULL;
285 } 275 }
286 276
@@ -314,8 +304,7 @@ Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
314Value* GreaterThanIntFn(const char* name, State* state, 304Value* GreaterThanIntFn(const char* name, State* state,
315 int argc, Expr* argv[]) { 305 int argc, Expr* argv[]) {
316 if (argc != 2) { 306 if (argc != 2) {
317 free(state->errmsg); 307 state->errmsg = "greater_than_int expects 2 arguments";
318 state->errmsg = strdup("greater_than_int expects 2 arguments");
319 return NULL; 308 return NULL;
320 } 309 }
321 310
@@ -499,20 +488,12 @@ Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
499 return args; 488 return args;
500} 489}
501 490
502static void ErrorAbortV(State* state, const char* format, va_list ap) {
503 std::string buffer;
504 android::base::StringAppendV(&buffer, format, ap);
505 free(state->errmsg);
506 state->errmsg = strdup(buffer.c_str());
507 return;
508}
509
510// Use printf-style arguments to compose an error message to put into 491// Use printf-style arguments to compose an error message to put into
511// *state. Returns nullptr. 492// *state. Returns nullptr.
512Value* ErrorAbort(State* state, const char* format, ...) { 493Value* ErrorAbort(State* state, const char* format, ...) {
513 va_list ap; 494 va_list ap;
514 va_start(ap, format); 495 va_start(ap, format);
515 ErrorAbortV(state, format, ap); 496 android::base::StringAppendV(&state->errmsg, format, ap);
516 va_end(ap); 497 va_end(ap);
517 return nullptr; 498 return nullptr;
518} 499}
@@ -520,8 +501,14 @@ Value* ErrorAbort(State* state, const char* format, ...) {
520Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { 501Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
521 va_list ap; 502 va_list ap;
522 va_start(ap, format); 503 va_start(ap, format);
523 ErrorAbortV(state, format, ap); 504 android::base::StringAppendV(&state->errmsg, format, ap);
524 va_end(ap); 505 va_end(ap);
525 state->cause_code = cause_code; 506 state->cause_code = cause_code;
526 return nullptr; 507 return nullptr;
527} 508}
509
510State::State(const std::string& script, void* cookie) :
511 script(script),
512 cookie(cookie) {
513}
514