nimble-code / Cobra

An interactive (fast) static source code analyzer
136 stars 30 forks source link

trying to use $COBRA and $ARGS in background shell causes failed assertions #35

Closed yilmazdurmaz closed 2 years ago

yilmazdurmaz commented 2 years ago

Executing commands on host while in Cobra is nice

! c execute command(s) c in a background shell

But I have found this issue while checking for another one. So far I could run many shell commands with ! and had no issue other than trying to use $COBRA and $ARGS.

: !echo $ARGS
cobra: cobra_prim.c:235: check_args: Assertion `strlen(c)+strlen(f->s) < n' failed.
Aborted (core dumped)
----
: !echo $COBRA
cobra: cobra_prim.c:241: check_args: Assertion `strlen(c)+strlen(p) < n' failed.
Aborted (core dumped)

I have seen there is an old post from 2019. Since you marked it as solved and I could not relate that to these two, I opened this new issue.

My other issue, #34, might be irrelevant but also might be directly caused by this one, So you may want to check them together.

Thanks YILMAZ

nimble-code commented 2 years ago

will look into it

yilmazdurmaz commented 2 years ago

I had time to dive into the code a bit.

I could not follow pointers and sizes, yet I came to a point where a simple comparison change, namely from < to <=, fixes the issue.

In connection to my other issue, #34 , if the additions of quotes are good to go, then we need extra n+=1 in two places.

        {   strcat(c, " \""); n += 1;
            assert(strlen(c)+strlen(f->s) <= n);
            strcat(c, f->s);
            strcat(c, "\""); n += 1;
        }

Following are results after the above change (with quotes)

$ cobra -comments -terse test.py
: !$COBRA
lengths: 31 0 31
content: /home/ubuntu/Cobra/rules/../bin
: !$FLAGS
lengths: 0 0 14
content: 
sh: 1: -terse: not found
: !$ARGS
lengths: 2 7 9 (before adding file name and closing quote)
lengths:10 0 10 
content: "test.py"
sh: 1: test.py: not found

Please check if these changes are suitable to include in your codebase.

YILMAZ

yilmazdurmaz commented 2 years ago

Please be cautious about my above message.

I see that using this n, you are allocating a memory block before reaching the code I changed, and I admit I don't understand memory allocations.

my changes do work with a single file and relatively short names, but I guess with multiple files opened it will cause memory problems. (my shell script finds files and processes them one by one)

You are the expert, so I leave the rest in your hands. YILMAZ

nimble-code commented 2 years ago

I double checked, and agree with your assessment -- will update a corrected version with the update to version 3.8, tpday