Semgrep: Go patterns that matches functions incorrectly handle the function return type

Created on 30 Sep 2020  路  2Comments  路  Source: returntocorp/semgrep

In https://semgrep.dev/s/disconnect3d:go-func-return-types-bug the $RETTYPES incorrectly matches two return types and I would expect it to match functions with a single return type.

Another buggy case is that removing $RETTYPES from the pattern, still matches the function (it should not since we want to match functions that do not return?).

bug golang wontfix

Most helpful comment

That's by design. Regarding the first point, this is because RETTYPES is matched against a tuple type (a pair type). A metavariable type can match any type, including tuple types.

Regarding the second point, this is again by design and one of the core principle of semgrep which I call "unspecified matches more", meaning that even if your pattern does not specify certain things (decorators, return type, inheritance parents, etc.) it will match things which have those things. In C, we want 'int foo(int x)' to also match code like 'static int foo(int register x)'.

That has the disadvantage that is not easy to match the absence of things, for example here the abscence of a return type. However you can use the boolean composition operator in the yaml file (pattern-not:) to emulate it.

All 2 comments

That's by design. Regarding the first point, this is because RETTYPES is matched against a tuple type (a pair type). A metavariable type can match any type, including tuple types.

Regarding the second point, this is again by design and one of the core principle of semgrep which I call "unspecified matches more", meaning that even if your pattern does not specify certain things (decorators, return type, inheritance parents, etc.) it will match things which have those things. In C, we want 'int foo(int x)' to also match code like 'static int foo(int register x)'.

That has the disadvantage that is not easy to match the absence of things, for example here the abscence of a return type. However you can use the boolean composition operator in the yaml file (pattern-not:) to emulate it.

@disconnect3d Thank you for filing an issue, and @aryx thank you for the reply! For now I'd like to close this issue as wont-fix, and if there are additional questions we can certainly reopen it.

Was this page helpful?
0 / 5 - 0 ratings