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
diff --git a/edify/expr.cpp b/edify/expr.cpp
index ecb1bea..aa3a55a 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -107,8 +107,7 @@
 
 Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
     if (argc != 2 && argc != 3) {
-        free(state->errmsg);
-        state->errmsg = strdup("ifelse expects 2 or 3 arguments");
+        state->errmsg = "ifelse expects 2 or 3 arguments";
         return NULL;
     }
     char* cond = Evaluate(state, argv[0]);
@@ -134,11 +133,10 @@
     if (argc > 0) {
         msg = Evaluate(state, argv[0]);
     }
-    free(state->errmsg);
     if (msg) {
         state->errmsg = msg;
     } else {
-        state->errmsg = strdup("called abort()");
+        state->errmsg = "called abort()";
     }
     return NULL;
 }
@@ -153,15 +151,8 @@
         int b = BooleanString(v);
         free(v);
         if (!b) {
-            int prefix_len;
             int len = argv[i]->end - argv[i]->start;
-            char* err_src = reinterpret_cast<char*>(malloc(len + 20));
-            strcpy(err_src, "assert failed: ");
-            prefix_len = strlen(err_src);
-            memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
-            err_src[prefix_len + len] = '\0';
-            free(state->errmsg);
-            state->errmsg = err_src;
+            state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
             return NULL;
         }
     }
@@ -279,8 +270,7 @@
 
 Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
     if (argc != 2) {
-        free(state->errmsg);
-        state->errmsg = strdup("less_than_int expects 2 arguments");
+        state->errmsg = "less_than_int expects 2 arguments";
         return NULL;
     }
 
@@ -314,8 +304,7 @@
 Value* GreaterThanIntFn(const char* name, State* state,
                         int argc, Expr* argv[]) {
     if (argc != 2) {
-        free(state->errmsg);
-        state->errmsg = strdup("greater_than_int expects 2 arguments");
+        state->errmsg = "greater_than_int expects 2 arguments";
         return NULL;
     }
 
@@ -499,20 +488,12 @@
     return args;
 }
 
-static void ErrorAbortV(State* state, const char* format, va_list ap) {
-    std::string buffer;
-    android::base::StringAppendV(&buffer, format, ap);
-    free(state->errmsg);
-    state->errmsg = strdup(buffer.c_str());
-    return;
-}
-
 // Use printf-style arguments to compose an error message to put into
 // *state.  Returns nullptr.
 Value* ErrorAbort(State* state, const char* format, ...) {
     va_list ap;
     va_start(ap, format);
-    ErrorAbortV(state, format, ap);
+    android::base::StringAppendV(&state->errmsg, format, ap);
     va_end(ap);
     return nullptr;
 }
@@ -520,8 +501,14 @@
 Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
     va_list ap;
     va_start(ap, format);
-    ErrorAbortV(state, format, ap);
+    android::base::StringAppendV(&state->errmsg, format, ap);
     va_end(ap);
     state->cause_code = cause_code;
     return nullptr;
 }
+
+State::State(const std::string& script, void* cookie) :
+    script(script),
+    cookie(cookie) {
+}
+