Re: [patch cr 2/4] [RFC] syscalls, x86: Add __NR_kcmp syscall v7

H. Peter Anvin
2012-02-03T17:35:56Z

On 02/03/2012 09:32 AM, H. Peter Anvin wrote:
> On 02/03/2012 01:28 AM, Cyrill Gorcunov wrote:
>> static __init int kcmp_cookie_init(void)
>> {
>> int i, j;
>>
>> for (i = 0; i< KCMP_TYPES; i++) {
>> for (j = 0; j< 2; j++)
>> get_random_bytes(&cookies[i][j], sizeof(long));
>> cookies[i][1] |= (~(~0UL>> 1) | 1);
>> }
>>
>> return 0;
>> }
>>
>> I thought in first place that sizeof(cookies[i][j]) would allow me
>> to change type of cookies in one place (ie at declaration) but
>> if cookies type will be changed -- the code will need careful review
>> anway, so sizeof(long) will be enough I think.
>>
>> On the other hands, maybe more clean variant will be
>>
>> static __init int kcmp_cookie_init(void)
>> {
>> const int size = sizeof(cookies[0][0]);
>> int i, j;
>>
>> for (i = 0; i< KCMP_TYPES; i++) {
>> for (j = 0; j< 2; j++)
>> get_random_bytes(&cookies[i][j], size);
>> cookies[i][1] |= (~(~0UL>> 1) | 1);
>> }
>>
>
> How about:
>
> const int size = sizeof(cookies[0]);
>
> get_random_bytes(&cookies[i], size);
>
> ... and skip the completely unnecessary for loop?
>

Even better:

static __init int kcmp_cookie_init(void)
{
int i;

get_random_bytes(cookies, sizeof cookies);

for (i = 0; i < KCMP_TYPES; i++)
cookies[i][1] |= ~(~0UL >> 1) | 1;
}