develooper Front page | perl.perl5.porters | Postings from June 2015

Coverity finding: shift by negative

Thread Next
From:
Jarkko Hietaniemi
Date:
June 23, 2015 00:03
Subject:
Coverity finding: shift by negative
Message ID:
5588A260.8070804@iki.fi
Coverity flags the pp_left_shift and pp_right_shift in pp.c.

Either of them can shift by a negative amount, called (ta-dah!) shift.

The result of shifting by negative amount is the infamous undefined 
behavior territory (of the nasal demons kind).  (As is shifting by more 
than the number of bits in the integer, but Coverity doesn't flag that. 
  Yet.)

Now, the question is, what should we do?   What should we return?

(Yes, I'm asking should we define a semantics for this thing which is 
undefined in C.)

undef?

shift left by negative n == shift right by -n ?
shift right by negative n == shift left by -n?

shift by too many bits == shift by n modulo wordbits?
(with a sane definition of modulo for negative -n...)

for use integer we would need to think about signed behavior.

warn? (of "portable" kind?)

croak?

At the very minimum, I would suggest undef for the negative shift case.
(To get Coverity off our case.)




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About