From 0b940c5823db66eca14ff990a4aeb7d4c4d88f33 Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Tue, 12 Dec 2017 14:40:15 +0000 Subject: [PATCH] lj_auditlog.c: Glibc memory stream workaround... --- src/lj_auditlog.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lj_auditlog.c b/src/lj_auditlog.c index ebde1e0509..42cb433c7e 100644 --- a/src/lj_auditlog.c +++ b/src/lj_auditlog.c @@ -15,10 +15,9 @@ char *membuffer; size_t membuffersize; -/* File where the audit log is written. */ -FILE *fp; -/* Have we been unable to initialize the log? */ -int error; +FILE *fp; /* File where the audit log is written. */ +int error; /* Have we been unable to initialize the log? */ +int memlog; /* are we logging into memory? */ /* -- msgpack writer - see http://msgpack.org/index.html ------------------ */ @@ -83,6 +82,7 @@ static int ensure_log_open() { ** JIT activity has ocurred.) */ if ((fp = open_memstream(&membuffer, &membuffersize)) != NULL) { + memlog = 1; lj_auditlog_vm_definitions(); return 1; } else { @@ -97,27 +97,33 @@ static int ensure_log_open() { */ int lj_auditlog_open(const char *path) { - char buffer[4096]; FILE *newfp; - int nread; if (!ensure_log_open()) return 0; newfp = fopen(path, "wb+"); /* Migrate the contents of the existing log. */ fflush(fp); - rewind(fp); - while ((nread = fread(&buffer, 1, sizeof(buffer), fp)) > 0) { - if (fwrite(&buffer, 1, nread, newfp) != nread) break; - } - if (!ferror(fp) && !ferror(newfp)) { - /* Migration succeeded. */ - fp = newfp; - return 1; + if (memlog) { + /* Migrate log from memory. + ** Special case: I don't trust glibc memory streams... + */ + fwrite(membuffer, 1, membuffersize, newfp); } else { - /* Migration failed: revert to the old log. */ - fclose(newfp); - fseek(fp, 0, SEEK_END); - return 0; + /* Migrate log from file. */ + char buffer[4096]; + int nread; + fseek(fp, 0, SEEK_SET); + while ((nread = fread(&buffer, 1, sizeof(buffer), fp)) > 0) { + if (fwrite(&buffer, 1, nread, newfp) != nread) break; + } + if (ferror(fp) || ferror(newfp)) { + /* Migration failed: revert to the old log. */ + fclose(newfp); + fseek(fp, 0, SEEK_END); + return 0; + } } + fp = newfp; + return 1; } /* -- high-level LuaJIT object logging ------------------------------------ */