From 634c9b963c109c772f3ba913d4abd328093059bc Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 19 Feb 2013 16:32:39 -0800 Subject: handle malloc(0) and free(0) in corruption checker --- src/corruptionCheck.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/corruptionCheck.js') diff --git a/src/corruptionCheck.js b/src/corruptionCheck.js index ae2a0bdf..bd3b240b 100644 --- a/src/corruptionCheck.js +++ b/src/corruptionCheck.js @@ -19,8 +19,8 @@ var CorruptionChecker = { } }); }, malloc: function(size) { + if (size <= 0) size = 1; // malloc(0) sometimes happens - just allocate a larger area, no harm CorruptionChecker.checkAll(); - assert(size > 0); // some mallocs accept zero - fix your code if you want to use this tool size = (size+7)&(~7); var allocation = CorruptionChecker.realMalloc(size*(1+2*CorruptionChecker.BUFFER_FACTOR)); var ptr = allocation + size*CorruptionChecker.BUFFER_FACTOR; @@ -28,13 +28,17 @@ var CorruptionChecker = { CorruptionChecker.ptrs[ptr] = size; CorruptionChecker.fillBuffer(allocation, size*CorruptionChecker.BUFFER_FACTOR); CorruptionChecker.fillBuffer(allocation + size*(1+CorruptionChecker.BUFFER_FACTOR), size*CorruptionChecker.BUFFER_FACTOR); + //Module.print('malloc ' + size + ' ==> ' + [ptr, allocation]); return ptr; }, free: function(ptr) { + if (!ptr) return; // ok to free(NULL), does nothing CorruptionChecker.checkAll(); var size = CorruptionChecker.ptrs[ptr]; + //Module.print('free ' + ptr + ' of size ' + size); assert(size); var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; + //Module.print('free ' + ptr + ' of size ' + size + ' and allocation ' + allocation); delete CorruptionChecker.ptrs[ptr]; CorruptionChecker.realFree(allocation); }, -- cgit v1.2.3-18-g5258