Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in forEach usage of eval() #25

Open
anvidal opened this issue Nov 9, 2013 · 3 comments
Open

bug in forEach usage of eval() #25

anvidal opened this issue Nov 9, 2013 · 3 comments

Comments

@anvidal
Copy link

anvidal commented Nov 9, 2013

forEach suffers from the same bug that was mentioned and closed in #4 :

line 1167:

  window.forEach = function(ary, funct) {
    fun = eval("function(index,item) { "+funct+"; }");
    for (var i=0; i<ary.length; i++) {
      try {
        fun.call(ary[i], i, ary[i]);
      } catch (e) {
        err("jsawk: js error: "+e);
        quit(3);
      }
    }
  };

replacing the eval() with

fun = eval("(function(index,item) { "+funct+"; })");

fixes the issue by wrapping the anonymous function creation in parentheses. I haven't reviewed other usages of eval() but it may be worth taking a look.

@micha
Copy link
Owner

micha commented Nov 9, 2013

@anvidal Thanks, I'll look into it.

@dentarg
Copy link

dentarg commented Jan 24, 2014

Am I doing something wrong or is forEach broken?

Using jsawk from 7da2143:

$ curl -s http://example.com/json | ./jsawk 'forEach(this, "out(item.count)") ; return'
jsawk: js error: SyntaxError: syntax error

Using 7da2143 + the suggested change:

$ git diff
diff --git a/jsawk b/jsawk
index 28aaccd..45aae4c 100755
--- a/jsawk
+++ b/jsawk
@@ -1165,7 +1165,7 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
     IS = IS.slice(0, $_+1).concat([record]).concat(IS.slice($_+1));
   };
   window.forEach = function(ary, fun) {
-    fun = eval("function(index,item) { "+fun+"; }");
+    fun = eval("(function(index,item) { "+funct+"; })");
     for (var i=0; i<ary.length; i++) {
       try {
         fun.call(ary[i], i, ary[i]);
$ curl -s http://example.com/json | ./jsawk 'forEach(this, "out(item.count)") ; return'
jsawk: js error: ReferenceError: funct is not defined

This is the JSON from http://example.com/json:

[{"time":1356998400000,"count":1257},{"time":1359676800000,"count":4007},{"time":1362096000000,"count":3807},{"time":1364774400000,"count":3813},{"time":1367366400000,"count":4450},{"time":1370044800000,"count":4284},{"time":1372636800000,"count":3885},{"time":1375315200000,"count":3925},{"time":1377993600000,"count":3369},{"time":1380585600000,"count":3315},{"time":1383264000000,"count":3790},{"time":1385856000000,"count":4625}]

@dentarg
Copy link

dentarg commented Jan 24, 2014

Oh, never mind me. I didn't read the docs carefully enough.

Also found a good comment about it elsewhere:

If the outermost element of your JSON data is an array, then jsawk automatically loops over each element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants