From 7a49a90f8b092e1c2e58d3e754578cff3bf06b18 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 19 Nov 2015 10:31:47 -0500 Subject: Fix a few C memory bugs --- src/c/urweb.c | 10 ++++++---- src/lru_cache.sml | 16 +++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/c/urweb.c b/src/c/urweb.c index c1cfe94c..945a6890 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -602,6 +602,8 @@ uw_context uw_init(int id, uw_loggers *lg) { ctx->remoteSock = -1; + ctx->cacheUnlock = NULL; + return ctx; } @@ -3681,7 +3683,7 @@ failure_kind uw_initialize(uw_context ctx) { 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 @@ static char *uw_Sqlcache_allocKeyBuffer(char **keys, size_t numKeys) { 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 @@ static void uw_Sqlcache_storeCommitOne(uw_Sqlcache_Cache *cache, char **keys, uw 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 @@ static void uw_Sqlcache_storeCommitOne(uw_Sqlcache_Cache *cache, char **keys, uw 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; diff --git a/src/lru_cache.sml b/src/lru_cache.sml index e9ed5f73..5c05b261 100644 --- a/src/lru_cache.sml +++ b/src/lru_cache.sml @@ -111,16 +111,16 @@ fun setupQuery {index, params} = (* 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 @@ fun setupQuery {index, params} = 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;", -- cgit v1.2.3