esgraph icon indicating copy to clipboard operation
esgraph copied to clipboard

Error when getting the CFG

Open sabaa opened this issue 6 years ago • 3 comments

Hello,

I get an exception when I try to get the CFG for the following code:

const fs = require('fs');
fs.open('/open/some/file.txt', 'r', (err, fd) => {
  if (err) throw err;
  fs.close(fd, (err) => {
    if (err) throw err;
  });
});

The error is:

TypeError: Cannot set property 'nextSibling' of undefined
    at backToFront (node_modules/esgraph/lib/index.js:369:56)
    at BlockOrProgram (node_modules/esgraph/lib/index.js:374:7)
    at recurse (node_modules/walkes/index.js:20:10)
    at /Users/saba/Documents/northeastern/research/async-miner/node_modules/walkes/index.js:40:10
    at Array.forEach (<anonymous>)
    at checkProps (node_modules/walkes/index.js:34:20)
    at recurse (node_modules/walkes/index.js:20:10)
    at Array.map (<anonymous>)
    at /Users/saba/Documents/northeastern/research/async-miner/node_modules/walkes/index.js:38:15
    at Array.forEach (<anonymous>)
    at checkProps (node_modules/walkes/index.js:34:20)
    at recurse (node_modules/walkes/index.js:20:10)
    at /Users/saba/Documents/northeastern/research/async-miner/node_modules/walkes/index.js:40:10
    at Array.forEach (<anonymous>)
    at checkProps (node_modules/walkes/index.js:34:20)
    at recurse (node_modules/walkes/index.js:20:10)
    at backToFront (node_modules/esgraph/lib/index.js:370:9)
    at BlockOrProgram (node_modules/esgraph/lib/index.js:374:7)
    at recurse (node_modules/walkes/index.js:20:10)
    at walker (node_modules/walkes/index.js:24:9)
    at linkSiblings (node_modules/esgraph/lib/index.js:376:5)
    at ControlFlowGraph (node_modules/esgraph/lib/index.js:47:3)
    at Parser.getCfg (app/parser.js:25:17)

I don't have any issues for calls that are not nested. E.g., if the fs.close() is not included, I won't have any issues.

I'd appreciate any help.

sabaa avatar Jul 31 '18 20:07 sabaa

a few years later: I am playing with this library at the moment a bit. What I found for your example:

  • it seems to work, if you replace all the arrow functions with normal functions with bounded context:

    const fs = require('fs');
    fs.open('/open/some/file.txt', 'r', (function(err, fd) {
      if (err) throw err;
      fs.close(fd, (function(err){
        if (err) throw err;
      })).bind(this);
    })).bind(this);
    
    image
  • Once you replace the inner functions with an arrow functions, is not detected correctly:

    const fs = require('fs');
    fs.open('/open/some/file.txt', 'r', (function(err, fd) {
      if (err) throw err;
      fs.close(fd, (err)=>{
        if (err) throw err;
      });
    })).bind(this);
    
    image

    Quickfix: replace all the arrow functions with bounded functions. Somehow the arrow functions are not processed correctly

byteSamurai avatar Feb 05 '22 18:02 byteSamurai

Well yes, this project is pretty much abandoned, and was never updated to work with newer JS syntax.

Swatinem avatar Feb 06 '22 12:02 Swatinem

Well, this is the (open source) way. A possible alternative is maybe Styx but it lacks of some features as well, but the software design is nice.

If you don't want to maintain esgraph, maybe flag the project and link to Styx? However both projects have their pros and cons.

byteSamurai avatar Feb 06 '22 18:02 byteSamurai