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

Coverity finding: shift by negative

Thread Next
Jarkko Hietaniemi
June 23, 2015 00:03
Coverity finding: shift by negative
Message ID:
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. 

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.)


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?)


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

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About