changeset 2296:5104e480b3e3

Fix a few C memory bugs
author Adam Chlipala <adam@chlipala.net>
date Thu, 19 Nov 2015 10:31:47 -0500
parents e6c5bb62fef8
children 6d56080f495c
files src/c/urweb.c src/lru_cache.sml
diffstat 2 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/urweb.c	Thu Nov 19 03:45:39 2015 -0500
+++ b/src/c/urweb.c	Thu Nov 19 10:31:47 2015 -0500
@@ -602,6 +602,8 @@
 
   ctx->remoteSock = -1;
 
+  ctx->cacheUnlock = NULL;
+
   return ctx;
 }
 
@@ -3681,7 +3683,7 @@
   if (r == 0) {
     uw_ensure_transaction(ctx);
     ctx->app->initializer(ctx);
-    if (ctx->app->db_commit(ctx))
+    if (uw_commit(ctx))
       uw_error(ctx, FATAL, "Error running SQL COMMIT");
   }
 
@@ -4626,7 +4628,7 @@
   while (numKeys-- > 0) {
     char* k = keys[numKeys];
     if (!k) {
-      // Can only happen when flushihg, in which case we don't need anything past the null key.
+      // Can only happen when flushing, in which case we don't need anything past the null key.
       break;
     }
     // Leave room for separator.
@@ -4695,7 +4697,7 @@
   if (numKeys == 0) {
     entry = cache->table;
     if (!entry) {
-      entry = malloc(sizeof(uw_Sqlcache_Entry));
+      entry = calloc(1, sizeof(uw_Sqlcache_Entry));
       entry->key = NULL;
       entry->value = NULL;
       entry->timeInvalid = 0;
@@ -4709,7 +4711,7 @@
       size_t len = buf - key;
       entry = uw_Sqlcache_find(cache, key, len, 1);
       if (!entry) {
-        entry = malloc(sizeof(uw_Sqlcache_Entry));
+        entry = calloc(1, sizeof(uw_Sqlcache_Entry));
         entry->key = strdup(key);
         entry->value = NULL;
         entry->timeInvalid = 0;
--- a/src/lru_cache.sml	Thu Nov 19 03:45:39 2015 -0500
+++ b/src/lru_cache.sml	Thu Nov 19 10:31:47 2015 -0500
@@ -111,16 +111,16 @@
              (* If the output is null, it means we had too much recursion, so it's a miss. *)
              string "  if (v && v->output != NULL) {",
              newline,
-             (* string ("    puts(\"SQLCACHE: hit " ^ i ^ ".\");"), *)
-             (* newline, *)
+             (*string ("    puts(\"SQLCACHE: hit " ^ i ^ ".\");"),
+             newline,*)
              string "    uw_write(ctx, v->output);",
              newline,
              string "    return v->result;",
              newline,
              string "  } else {",
              newline,
-             (* string ("    puts(\"SQLCACHE: miss " ^ i ^ ".\");"), *)
-             (* newline, *)
+             (*string ("    puts(\"SQLCACHE: miss " ^ i ^ ".\");"),
+             newline,*)
              string "    uw_recordingStart(ctx);",
              newline,
              string "    return NULL;",
@@ -136,14 +136,16 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string ("  uw_Sqlcache_Value *v = malloc(sizeof(uw_Sqlcache_Value));"),
+             string ("  uw_Sqlcache_Value *v = calloc(1, sizeof(uw_Sqlcache_Value));"),
              newline,
              string "  v->result = strdup(s);",
              newline,
              string "  v->output = uw_recordingRead(ctx);",
              newline,
-             (* string ("  puts(\"SQLCACHE: stored " ^ i ^ ".\");"), *)
-             (* newline, *)
+             string "  v->timeValid = 0;",
+             newline,
+             (*string ("  puts(\"SQLCACHE: stored " ^ i ^ ".\");"),
+             newline,*)
              string ("  uw_Sqlcache_store(ctx, cache" ^ i ^ ", ks, v);"),
              newline,
              string "  return uw_unit_v;",