Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some compiler edge cases. #1709

Merged
merged 3 commits into from
May 17, 2022

Commits on May 16, 2022

  1. Fix some compiler edge cases.

    Add a warning when "0 of them" is used. Point to some new documentation that
    explains why "none of them" is preferred.
    
    Handle some edge cases, which are all now errors:
    
    * -1 of them
    * "foo" of them
    * /foo/ of them
    
    And this one is also an error if the external variable "x" is a negative integer
    or a string:
    
    * x of them
    
    I've also made sure this works with a construct like "0 + x of them" where x is
    defined as a negative integer.
    
    Add test cases for as many of these as I can. I don't have test cases for the
    external variable case, but it does work as expected. Here is the output of a
    rule using external variables where the variable is defined to be 1, 0, -1 and
    foo:
    
    ```
    wxs@mbp yara % cat rules/test.yara
    rule a {
      strings:
        $a = "FreeBSD"
      condition:
        x of them
    }
    wxs@mbp yara % ./yara -d x=1 rules/test.yara /bin/ls
    a /bin/ls
    wxs@mbp yara % ./yara -d x=0 rules/test.yara /bin/ls
    warning: rule "a" in rules/test.yara(5): Consider using "none" keyword, it is less ambiguous. Please see https://yara.readthedocs.io/en/stable/writingrules.html#sets-of-strings-1 for an explanation.
    wxs@mbp yara % ./yara -d x=-1 rules/test.yara /bin/ls
    error: rule "a" in rules/test.yara(5): invalid value in condition: "-1"
    wxs@mbp yara % ./yara -d x=foo rules/test.yara /bin/ls
    error: rule "a" in rules/test.yara(5): invalid value in condition: "string in for_expression is invalid"
    wxs@mbp yara %
    ```
    wxsBSD committed May 16, 2022
    Configuration menu
    Copy the full SHA
    ce821fd View commit details
    Browse the repository at this point in the history

Commits on May 17, 2022

  1. More compiler warnings and errors.

    If you specify "2 of ($a)" it is now a warning because it will always evaluate
    to false, and is quite possibly not what you wanted to write. This is true for
    string sets, rule sets and string sets in a range like "2 of ($a) in (0..10)".
    
    If you are using an integer range it is now an error if the lower bound is
    greater than the upper bound.
    
    To support rule sets I needed to modify yr_parser_emit_pushes_for_rules() to
    use a count parameter, which it will update with the number of pushed rules.
    This is identical to how yr_parser_emit_pushes_for_strings() works.
    
    While I'm here, remove the link from the warning message for the "none" keyword.
    wxsBSD committed May 17, 2022
    Configuration menu
    Copy the full SHA
    c62043d View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    61f578e View commit details
    Browse the repository at this point in the history