nimble-code / Cobra

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

in `cobra_prim.c`, assertion fails occur, `$ARGS` is empty and `$FLAGS` might be used in wrong order, version 3.8 #36

Closed yilmazdurmaz closed 2 years ago

yilmazdurmaz commented 2 years ago

Somewhere else in the code with update 3.8 changed the behavior for check_args, or they were silent bugs awaiting this moment.

I run the program with a dummy empty file with a few flags cobra -cpp -terse nospace.c.

if I try only !$ARGS, the following assertion error comes up.

Assertion failed: strlen(c)+strlen(p) < n (cobra_prim.c: check_args: 213)

I have added a printf statement before that line and also changed it to ... < n+1 to see a result. I have found that this assertion failure happens because $ARGS is now empty. the assertion passes only when there are more characters after $ARGS.

: !$ARGS                 // assertion fails
c:0 p:0 n:0

: !echo $ARGS         //assertion fails
c:0 p:5 n:5

: !echo $ARGS.       // assertion passes if there is any extra character after, includes a single space
c:0 p:5 n:6
.

for the use of $FLAGS, I found this by accident while trying the following command. at first, I thought it was $ARGS filled wrong, but since it is already empty, then it is only the $FLAGS being redirected in the wrong order

: !echo $FLAGS
-cpp -terse

: !echo C: $COBRA , A: $ARGS and F: $FLAGS.
c:37 p:6 n:72
C: /workspace/Cobra/rules/../bin , A: -cpp -terse and F:

lastly, if I use $FLAGS before $ARGS (which it normally should be), the command is cut short after the use of $FLAGS

: !echo C: $COBRA , F: $FLAGS , A: $ARGS
c:37 p:4 n:65
C: /workspace/Cobra/rules/../bin , F: -cpp -terse

I hope the solution will be easy and fast because especially the rulesets using scope_check and similar shell spawns are now compromised with this empty $ARGS.

PS: I was trying to see your changes for filenames containing spaces, but this problem happens with no spaces too.

nimble-code commented 2 years ago

wait, that's exactly the issue that I fixed in 3.8....: $ ./cobra -cpp -terse cobra.h : !echo $ARGS cobra.h cobra_fe.h cobra_prim.h : !$ARGS sh: cobra.h: command not found

yilmazdurmaz commented 2 years ago

@nimble-code

actually it is not, if you mean the one I posted before, issue #35 .

This time $ARGS comes empty at some point before line 213.

nimble-code commented 2 years ago

but all the examples you give just work on my system, using version 3.8: $ ./cobra -cpp -terse cobra.h : !echo $ARGS cobra.h cobra_fe.h cobra_prim.h : !echo C: $COBRA , A: $ARGS and F: $FLAGS C: /home/USER/Dropbox/GitHub/Cobra/rules/../bin , A: -cpp -terse cobra.h cobra_fe.h cobra_prim.h and F:

nimble-code commented 2 years ago

ah, but I see your point, the flags are grouped under A: ? is that the issue?

yilmazdurmaz commented 2 years ago

@nimble-code

hmm, now you mentioned it, it seems my "dummy" files cause the main problem. They have no content in them created by touch.

I thought the filename would be held in $ARGS, but it seems the current implementation does that only when there is content in it. I put a single non-space character in it and now $ARGS is filled. But now seeing this, I am not sure about the fix for file names with spaces as it does not show quotes.

: !echo $ARGS
with space.c

And then what you last wrote seems another problem: anything between $FLAG and $ARGS is removed, the , Args: part in the below case.

: !echo Cobra: $COBRA , Flags: $FLAGS , Args: $ARGS c:41 p:10 n:148 Cobra: /workspace/Cobra/rules/../bin , Flags: -cpp -terse Cobra/src/cobra_prim.h Cobra/src/cobra_fe.h Cobra/src/cobra.h

currently, I don't have a practical example, but this might be a problem for writing flexible rulesets using spawned processes.

by the way, we may move part of this issue to a new one, because apparently the main title gets solved by having a file with some content, or you can keep this open as at any time, some source repos may have empty placeholder files.

yilmazdurmaz commented 2 years ago

I take back the spaces-fix part. it does work! :)

nimble-code commented 2 years ago

ok, i'll close this one for now

yilmazdurmaz commented 2 years ago

C is not my main language, but I feel I am grasping bit by bit. I might come up with a solution later before you do.

Cheers, YILMAZ