Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1512089

Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer

From Manfred Spraul <manfred@colorfullife.com>
Newsgroups linux.kernel
Subject Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
Date 2016-10-30 16:40 +0100
Message-ID <sy0ng-6qQ-15@gated-at.bofh.it> (permalink)
References <sxjUZ-3o4-7@gated-at.bofh.it> <sxl0J-408-1@gated-at.bofh.it> <sxl0J-408-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On 10/28/2016 09:29 PM, Colin Ian King wrote:
> On 28/10/16 20:21, Manfred Spraul wrote:
>> Hi Colin,
>>
>> On 10/28/2016 08:11 PM, Colin King wrote:
>> [...]
>>> --- a/ipc/sem.c
>>> +++ b/ipc/sem.c
>>> @@ -1839,7 +1839,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct
>>> sembuf __user *, tsops,
>>>          max = 0;
>>>        for (sop = sops; sop < sops + nsops; sop++) {
>>> -        unsigned long mask = 1 << ((sop->sem_num) % BITS_PER_LONG);
>>> +        unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
>>>    
>> Why 1ULL? Is 1UL not sufficient?
> For example, 1UL i386 is 32 bits, where as 1ULL is 64.
Exactly: on i386, 'unsigned long" is 32 bits. BITS_PER_LONG is 32.
Thus with 1UL, the code should be correct.
With 1ULL & -Wconversion, gcc would even report a warning:
> gcc -m32 -Wall -Wconversion -O1 test.c
> test.c: In function ‘main’:
> test.c:13:6: warning: conversion to ‘long unsigned int’ from ‘long 
> long unsigned int’ may alter its value [-Wconversion]
>    j= 1ULL << k;
>       ^~~~

test.c:
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char **argv)
> {
>     unsigned long j;
>     int i;
>
>     for (i=1;i<argc;i++) {
>         long k;
>
>         k=atoi(argv[i]);
>         j= 1ULL << k;
>         printf("%d: %lu %ld.\n", i, j, k);
>     }
>     return 0;
> }
>

--
     Manfred
(still thinks "1UL" is what is required)

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer Colin King <colin.king@canonical.com> - 2016-10-28 20:20 +0200
  Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit  integer Colin Ian King <colin.king@canonical.com> - 2016-10-28 21:30 +0200
    Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit  integer Manfred Spraul <manfred@colorfullife.com> - 2016-10-30 16:40 +0100
  Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit  integer Manfred Spraul <manfred@colorfullife.com> - 2016-10-28 21:30 +0200
  Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit  integer Davidlohr Bueso <dave@stgolabs.net> - 2016-10-29 00:20 +0200

csiph-web