about summary refs log tree commit diff stats
path: root/src/teliva.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-01-04 22:27:10 -0800
committerKartik K. Agaram <vc@akkartik.com>2022-01-04 22:33:07 -0800
commitffd600b11109e3aeffb9268093420cc95ecbd3b5 (patch)
tree85d61de3ecc236f51dbc25b12743a71e5dd1fdd0 /src/teliva.c
parent855eafd1d9fea90aaaea74b95986e437d9d88a38 (diff)
downloadteliva-ffd600b11109e3aeffb9268093420cc95ecbd3b5.tar.gz
try running permissions advice after editing
This implies it must be side-effect free. We still need to figure out
how to convey that to the computer owner.
Diffstat (limited to 'src/teliva.c')
-rw-r--r--src/teliva.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/teliva.c b/src/teliva.c
index 0beff0d..0c5862a 100644
--- a/src/teliva.c
+++ b/src/teliva.c
@@ -1305,6 +1305,23 @@ static void render_permissions_screen() {
   refresh();
 }
 
+/* Try running the function to test for errors. If code has an error, leave it
+ * on the stack and return non-zero */
+int validate_file_operations_predicate() {
+  lua_getglobal(trustedL, "file_operation_permitted");
+  lua_pushstring(trustedL, "foo");
+  lua_pushstring(trustedL, "r");
+  if (lua_pcall(trustedL, 2 /*args*/, 1 /*result*/, /*errfunc*/0)) {
+    /* TODO: error handling. Or should we use errfunc above? */
+  }
+  int status = 1;
+  if (lua_isboolean(trustedL, -1)) {
+    lua_pop(trustedL, 1);
+    status = 0;
+  }
+  return status;
+}
+
 static int load_file_operations_predicate(const char* body) {
   char buffer[1024] = {0};
   strcpy(buffer, "function file_operation_permitted(filename, mode)\n");
@@ -1313,11 +1330,12 @@ static int load_file_operations_predicate(const char* body) {
     strncat(buffer, "\n", 1020);
   strncat(buffer, "end\n", 1020);
   return luaL_loadbuffer(trustedL, buffer, strlen(buffer), "file_operation_permitted")
-          || docall(trustedL, 0, 1);
+          || docall(trustedL, 0, 1)
+          || validate_file_operations_predicate();
 }
 
 extern void editNonCode2(char* filename);
-extern void resumeNonCodeEdit();
+extern void resumeNonCodeEdit2();
 static void edit_file_operations_predicate_body() {
   static char file_operations_predicate_body_buffer[512];
   /* save to disk */
@@ -1348,7 +1366,7 @@ static void edit_file_operations_predicate_body() {
       break;
     Previous_error = lua_tostring(trustedL, -1);
     if (Previous_error == NULL) Previous_error = "(error object is not a string)";
-    resumeNonCodeEdit();
+    resumeNonCodeEdit2();
     lua_pop(trustedL, 1);
   }
   file_operations_predicate_body = file_operations_predicate_body_buffer;