Skip to content

Commit

Permalink
fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tintinweb committed Nov 19, 2021
1 parent 1a66d45 commit 3211f28
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
24 changes: 13 additions & 11 deletions bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,21 @@ for(let path of argv._){


let vdb = new SolGrep('::memory::', rules, callbacks);
vdb.analyzeDir(path);
vdb.analyzeDir(path).then(() => {
console.log(" ──────────────────────────── Results")
console.log(vdb.results)
console.log(" ────────────────────────────")

console.log(" ──────────────────────────── Results")
console.log(vdb.results)
console.log(" ────────────────────────────")
if(argv.output){
require('fs').writeFileSync(argv.output, JSON.stringify(vdb.results, null, 2));
}

if(argv.output){
require('fs').writeFileSync(argv.output, JSON.stringify(vdb.results, null, 2));
}
vdb.close();
console.log(" ────────────────────────────")
bar1.stop();
exitProcess(vdb.results.length);
})

vdb.close();
console.log(" ────────────────────────────")
bar1.stop();

}

exitProcess(0);
8 changes: 4 additions & 4 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IsInitializable extends BaseRule{
check(sourceUnit){
Object.values(sourceUnit.contracts).forEach(contract => {
//for every contract in the SU
const found = contract.functions.filter(f => f.name === "initialize" && f._node.body !== null && (f._node.visibility === "public" || f._node.visibility === "external") && !f._node.modifiers.some(m => m.name === "onlyOwner"));
const found = contract.functions.filter(f => f.name === "initialize" && f.ast.body !== null && (f.ast.visibility === "public" || f.ast.visibility === "external") && !f.ast.modifiers.some(m => m.name === "onlyOwner"));

if(found.length == 0){
return;
Expand Down Expand Up @@ -50,7 +50,7 @@ class IsMultipleBalanceOfSameFunc extends BaseRule{
Object.values(sourceUnit.contracts).forEach(contract => {
//for every contract in the SU
contract.functions.forEach(f => {
if(f._node.modifiers.includes("nonReentrant")) return; //ignore nonReentrant
if(f.ast.modifiers.includes("nonReentrant")) return; //ignore nonReentrant
const funcbody = f.getSource();
if( (funcbody.split('.balanceOf').length -1 >= 2) && funcbody.split('diff').length -1 >= 2){
this.solgrep.report(sourceUnit, this, "DBL_BALANCEOF", `${f.name} - balanceOf() called multiple times within same func`);
Expand Down Expand Up @@ -169,7 +169,7 @@ class Stats extends BaseRule {
check(sourceUnit){
this.stats.sourceUnits += 1;
Object.values(sourceUnit.contracts).forEach(contract => {
switch(contract._node.kind){
switch(contract.ast.kind){
case "contract":
this.stats.contracts.total += 1; //num contracts
this.stats.contracts.names[contract.name] = this.stats.contracts.names[contract.name] === undefined ? 1 : this.stats.contracts.names[contract.name] + 1; //num contracts with same name
Expand All @@ -187,7 +187,7 @@ class Stats extends BaseRule {
this.stats.abstract.names[contract.name] = this.stats.abstract.names[contract.name] === undefined ? 1 : this.stats.abstract.names[contract.name] + 1; //num contracts with same name
break;
default:
throw new Error(`Unknown contract kind: ${contract._node.kind}`);
throw new Error(`Unknown contract kind: ${contract.ast.kind}`);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/solgrep.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SolGrep {

report(sourceUnit, rule, tag, info){
this.callbacks.onReport && this.callbacks.onReport(sourceUnit, rule, tag, info);
let key = sourceUnit ? sourceUnit.filePath : undefined
let key = sourceUnit ? sourceUnit.filePath : "__general__"
let result = this.results[key] === undefined ? this.results[key] = [] : this.results[key];
result.push({rule:rule.constructor.name, tag:tag, info:info})
}
Expand Down
13 changes: 11 additions & 2 deletions src/solidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const fs = require('fs');
const path = require('path');
const parser = require('@solidity-parser/parser');

const prxAttribForwarder = {
get: function(target, prop, receiver) {
return target[prop] === undefined ? target.ast[prop] : target[prop];
}
}

class SourceUnit {
constructor() {
Expand All @@ -20,6 +25,10 @@ class SourceUnit {
this.imports = [];
}

getSource(){
return this.content;
}

static getFileContent(fpath) {
if (!fs.existsSync(fpath)) {
throw Error(`File '${fpath}' does not exist.`);
Expand Down Expand Up @@ -65,7 +74,7 @@ class SourceUnit {
PragmaDirective(node) { this_sourceUnit.pragmas.push(node); },
ImportDirective(node) { this_sourceUnit.imports.push(node); },
ContractDefinition(node) {
this_sourceUnit.contracts[node.name] = new Contract(this_sourceUnit, node);
this_sourceUnit.contracts[node.name] = new Proxy(new Contract(this_sourceUnit, node), prxAttribForwarder);
},
});
/*** also import dependencies? */
Expand Down Expand Up @@ -166,7 +175,7 @@ class Contract {

},
FunctionDefinition(_node) {
let newFunc = new FunctionDef(current_contract, _node);
let newFunc = new Proxy(new FunctionDef(current_contract, _node), prxAttribForwarder);
current_contract.functions.push(newFunc);
current_contract.names[_node.name] = newFunc;

Expand Down

0 comments on commit 3211f28

Please sign in to comment.