Skip to content

A cross-function checking possibility #468

Open
@jongleb

Description

@jongleb

Please support to check if functions are called from the other. Let me show an example. Let me show an example.

This is the code I need to check. I need to check if I use an argument in concurrent mode.

  let use_thing thing = 
    let%bind () = fn1 thing
    and _ = fn2 thing in
    return ()

  let other_fn () = get_thing use_thing

And it works when this function is inside the function where get_thing is called.

rules:
  - id: concurrent-use
    patterns:
      - pattern-inside:
          pattern-either:
            - pattern: get_thing (fun $THING -> ...)
            - pattern: get_thing @@ fun $THING -> ...
            - pattern: let $FN_NAME $THING = ... in get_thing  $FN_NAME
      - pattern-either:
          - pattern: let $X = ... $THING ... and $Y = ... $THING ... in ...
          - pattern: let () = ... $THING ... and () = ... $THING ... in ...
          - pattern: let $X = ... $THING ... and () = ... $THING ... in ...
          - pattern: let () = ... $THING ... and $Y = ... $THING ... in ...
    languages:
      - ocaml
    severity: ERROR

And this could be rewritten with the following pattern (at least the same idea works for typescript and python)

rules:
  - id: concurrent-use
    patterns:
      - pattern:
            let $F $THING = ...
            ...
            get_thing  $F
      - pattern-either:
          - pattern: let $X = ... $THING ... and $Y = ... $THING ... in ...
          - pattern: let () = ... $THING ... and () = ... $THING ... in ...
          - pattern: let $X = ... $THING ... and () = ... $THING ... in ...
          - pattern: let () = ... $THING ... and $Y = ... $THING ... in ...
    message: Semgrep found a match
    languages:
      - ocaml
    severity: ERROR

But nothing is caught.

I tried this version (which is worked for typescript too):

- pattern:
            let $F $THING = ...
            ...
            let $ANOTHER_FN ... = get_thing  $F

But I caught: Invalid pattern for OCaml:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions