javainteger-overflowklocwork

Integer Overflow klocwork analysis


I have the following lines of code and when I run klocwork analysis on my project I get the following error

SV.INT_OVF: Tainted data 'Long.parseLong(...)' that comes from 'br.readLine()' is used in an arithmetic operation and can cause an integer overflow or unexpected result

My code

 while (line = br.readLine() != null) {
     long timestamp = timescale * Long.parseLong(line.substring(1, line.length()));
 }

How can I refactor this code to avoid possible overflow

Thanks


Solution

  • You can use BigInteger to avoid an overflow.

    Whether you should is another question.

    I would look at what is a sane range for these values and validate your inputs first. Most likely the widest range of sane values won't produce an overflow (or if it does, you have to use BigInteger)