Blog post: Bugs!? - static analysis to the rescue
Tags: perl, bug, staticanalysis
Published: 2019-01-31 07:57:15

---
title: Blog post: Bugs!? - static analysis to the rescue
published: true
description: A Perl::Critic policy to capture subtle bugs in error handling 
tags: perl, bug, static-analysis
---
Dave Cross wrote an insightful [blog post](https://perlhacks.com/2019/01/a-subtle-bug/) "A Subtle Bug" describing a anti-pattern in Perl resulting in a bug, which is quite subtle (hence the title of the original post).

Yes it is possible to do a scan/search of your code base and spot this bug. But a one-off analysis poses some issues, like keeping the same bugs out of our code base - permanently. 

And this is exactly what [static program analysis](https://en.wikipedia.org/wiki/Static_program_analysis) (or just static analysis) tools are for, especially when integrated with your source control system or CI/CD system.

The dominant static-analysis tool for Perl is named [Perl::Critic](https://metacpan.org/pod/Perl::Critic). I have written several policies for Perl::Critic to enforce my own coding policies and I am quite keen on the concept of static analysis. The reason for my enthusiasm is that I really really like code reviews, but I have often observed that we tend to discuss the obvious, so if we want to get the most out of a human review, which takes time and effort, we leave the _boring_ stuff to the computers, meaning static analysis of your code base and the humans can do what human are quite good at and that is spotting the other stuff using creativity, pattern matching and experience.

Based on Dave's post, which immediately triggered something with me, I have developed a first shot at a policy for discovering the anti-pattern and possible bug in your Perl source code.

Introducing: [Perl::Critic::Policy::InputOutput::ProhibitHighPrecedentLogicalOperatorErrorHandling ](https://github.com/jonasbn/perl-critic-policy-inputoutput-prohibithighprecedentlogicaloperatorerrorhandling)

```perl
sub _is_logical_operator {
    my ( $self, $sibling ) = @_;


    if ($sibling) {
        if ($sibling->class eq 'PPI::Token::Operator') {
            if ($sibling->content eq q{||}) {
                return $TRUE;
            }
        }
        return $self->_is_logical_operator($sibling->snext_sibling());
    }


    return $FALSE;
}
```

I do not like the name myself and it is somewhat wrong since the remedy of using `or` over `||` is also a logical operator. And secondly you can still use the `||` operator in combination with parentheses.

First implementation was very elegant relying only on [the recursive function `_is_logical_operator`](https://github.com/jonasbn/perl-critic-policy-inputoutput-prohibithighprecedentlogicaloperatorerrorhandling/blob/master/lib/Perl/Critic/Policy/InputOutput/ProhibitHighPrecedentLogicalOperatorErrorHandling.pm#L42-L50
), but this did not handle the presence of parentheses.

So I had to [introduce a regular expression](https://github.com/jonasbn/perl-critic-policy-inputoutput-prohibithighprecedentlogicaloperatorerrorhandling/blob/master/lib/Perl/Critic/Policy/InputOutput/ProhibitHighPrecedentLogicalOperatorErrorHandling.pm#L42-L50) :-/

```perl
    return if $elem->snext_sibling()->content() =~ m/^[\s]*[(]/xism;
```

Under the banner of clean code, I would love to at least isolate the regular expression - handling it differently would be AWESOME, so I am still pondering a solution.

I have notified Dave of my implementation and tweeted about it and now I am blogging, so hopefully I will get some reviews or feedback - did I mention I love code reviews? :-)

When I have stabilized the code and finalized the documentation, I am thinking about making a pull request to the [Perl::Critic distribution](https://github.com/Perl-Critic/Perl-Critic), since I am not really interested in maintaining yet another stand-alone policy, but more on this in another blog post.

Please check it out at let me know what you think:

- Are there scenarios I do not catch?
- Are there any bugs I have not spotted in my implementation?
- Is the policy implementation worth while?
- Anything else?