cgag / loc

Count lines of code quickly.
MIT License
2.31k stars 126 forks source link

Integer overflow issue? #37

Closed Posnet closed 5 years ago

Posnet commented 7 years ago

When running loc as loc . --files on the phpSimpleSAML project I get the following output

--------------------------------------------------------------------------------
 Language             Files        Lines        Blank      Comment         Code
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 Bourne Shell             2           86           19            6           61
--------------------------------------------------------------------------------
|./bin/build-release.sh              62           15            3           44
|./bin/check-syntax.sh               24            4            3           17
--------------------------------------------------------------------------------
 PHP                    473        65104        10377        18319        36408
--------------------------------------------------------------------------------
|templates/authsources.php          402           58          461   4294967179
|es/saml/lib/IdP/SAML2.php         1149          180          188          781
|L/Metadata/SAMLParser.php         1498          267          493          738
|tlibinc/base_facebook.php         1437          140          561          736
|pleSAML/Configuration.php         1369          205          528          636
...

There seems to be something going wrong with 4294967179

This is on: MacOS El Capitan loc version 0.3.4 rustc 1.12.1 (d4f39402a 2016-10-19)

cgag commented 7 years ago

Ah, very interesting. I suspect it has to do with the combination of nested comments and multiple comment syntaxes. This actually might force me to reconsider a lot of things. Good catch.

ryangreenberg commented 6 years ago

I ran into this issue as well. Here's a short file that reproduces the problem:

<?php
    function fn1_xpath($a, $cls){
        return $a->query("//*[contains(@class, '$cls')]");
    }

    # Removes <script></script> tag.
    #
    #
    #
    function f2($b){ return $b; }
$ loc sample.php 
--------------------------------------------------------------------------------
 Language             Files        Lines        Blank      Comment         Code
--------------------------------------------------------------------------------
 PHP                      1           10            1           10   4294967295
--------------------------------------------------------------------------------
 Total                    1           10            1           10   4294967295
--------------------------------------------------------------------------------
$ loc --version
count 0.4.1
cgag commented 5 years ago

Should be fixed as it now handles nested and multiline comments in a sane way, though the count for your short reproduction file is still wrong because loc doesn't understand strings yet.