warning: left shift count >= width of type (i386)

Andreas Ericsson ae at op5.se
Tue Sep 4 09:59:57 CEST 2012


On 09/04/2012 09:15 AM, Andreas Ericsson wrote:
> On 09/03/2012 10:23 PM, Ricardo Jose Maraschini wrote:
>> * Ricardo Jose Maraschini (ricardo.maraschini at opservices.com.br) wrote:
>>> OK, I'm not pretty sure about following, but here we go.
>>> I tried to compile current nagios on a i386 arch and received the
>>> following warning:
>>>
>>> squeue.c:34:  warning: left shift count >= width of type
>>>
>>> If i'm right, long is four bytes long on i386 arch, and as tv_sec is
>>> a long(accordingly to gettimeofday(2)) the result of line 34 on squeue.c
>>> is unpredictable.
>>>
>>> Am I right or I'm missing something?
>>>
>>> Compiling on a sparc64 and in a x86_64 environment i haven't received
>>> the same warning. On these architectures, sizeof(long) shows me 8 bytes.
>>>
>>> What are the performance implications of my patch below?
>>>
>>> -rm
>>
>> The patch below seems to be much more appropriate than the previous one.
>>
>> ===================================================================
>> --- lib/squeue.c	(revision 2138)
>> +++ lib/squeue.c	(working copy)
>> @@ -31,7 +31,7 @@
>>    
>>    static pqueue_pri_t evt_compute_pri(struct timeval *tv)
>>    {
>> -	return (tv->tv_sec << 32) | tv->tv_usec;
>> +	return ((pqueue_pri_t)tv->tv_sec << 32) | tv->tv_usec;
>>    }
>>    
> 
> That's odd. Casting to pqueue_pri_t shouldn't be necessary when the
> function return pqueue_pri_t. I worry a bit that this might break
> on systems with less-than-totally-sane compiler, where the value
> will first be calculated as a normal word and then the result cast
> explicitly to a longword. I've done a different change:
> 
> 	pqueue_pri_t ret;
> 	ret = tv->tv_sec;
> 	ret <<= 32;
> 	ret |= tv->tv_usec;
> 	return ret;
> 
> which should keep even the most retarded compiler from doing anything
> wrong.
> 
> Many thanks for the bugreport and the patch though :)
> 

And now it's pushed, so feel free to check out the latest and greatest
version from svn or git and give it a whirl.

-- 
Andreas Ericsson                   andreas.ericsson at op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/




More information about the Developers mailing list