From 312cb7f3a671357558b4803ae7ca7ec22f372f1c Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 20 Jun 2014 08:25:46 +0100 Subject: [PATCH] Fixed memory leak in ''.indexOf('.') --- ChangeLog | 1 + src/jswrap_string.c | 5 ++++- tests/test_string_indexof_2.js | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/test_string_indexof_2.js diff --git a/ChangeLog b/ChangeLog index 21f258e87..c648fa07a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,6 +32,7 @@ Fixed parsing of multiple shifts setWatch now reports the pin back (fix #275) Fixed memory leak in g.getPixel with arraybuffers + Fixed memory leak in "".indexOf(".") 1v64 : Fix 'a=[2,3,4];delete a[1];' Make sure parseInt("0x01",16)==1 and parseInt("0x01")==1 but parseInt("0b01",16)==0 diff --git a/src/jswrap_string.c b/src/jswrap_string.c index 11848bfb9..088cd2cd2 100644 --- a/src/jswrap_string.c +++ b/src/jswrap_string.c @@ -118,7 +118,10 @@ int jswrap_string_indexOf(JsVar *parent, JsVar *substring, JsVar *fromIndex, boo if (!substring) return 0; // out of memory int parentLength = (int)jsvGetStringLength(parent); int subStringLength = (int)jsvGetStringLength(substring); - if (subStringLength > parentLength) return -1; + if (subStringLength > parentLength) { + jsvUnLock(substring); + return -1; + } int lastPossibleSearch = parentLength - subStringLength; int idx, dir, end; if (!lastIndexOf) { // normal indexOf diff --git a/tests/test_string_indexof_2.js b/tests/test_string_indexof_2.js new file mode 100644 index 000000000..55152bfa9 --- /dev/null +++ b/tests/test_string_indexof_2.js @@ -0,0 +1,9 @@ +// test for memory leak that used to happen + +var i=0,b=0,lost=0; +b = process.memory().usage; + +for (i=0;i<10;i++) "".indexOf("."); + +lost = process.memory().usage-b; +result = lost < 10;