Re: [patch] memusage.1, bind.2, eventfd.2, futex.2, open_by_handle_at.2, perf_event_open.2, poll.2, signalfd.2, sysctl.2, timerfd_create.2, bsearch.3, cmsg.3, getaddrinfo.3, getaddrinfo_a.3 getgrouplist.3, insque.3, malloc_info.3, mbsinit.3, mbstowcs.3, pthread_create.3, pthread_setaffinity_np.3, queue.3, rtnetlink.3, shm_open.3, strptime.3, tsearch.3, aio.7, fanotify.7, inotify.7, unix.7: Use sizeof consistently

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Alex,

On 8/24/20 3:29 PM, Alejandro Colomar wrote:
>>From 5df5cae0fb6973df0ab8b3629934f808487112b0 Mon Sep 17 00:00:00 2001
> From: Alejandro Colomar <colomar.6.4.3@xxxxxxxxx>
> Date: Fri, 21 Aug 2020 16:58:12 +0200
> Subject: [PATCH] memusage.1, bind.2, eventfd.2, futex.2,
> open_by_handle_at.2,
>  perf_event_open.2, poll.2, signalfd.2, sysctl.2, timerfd_create.2,
> bsearch.3,
>  cmsg.3, getaddrinfo.3, getaddrinfo_a.3 getgrouplist.3, insque.3,
>  malloc_info.3, mbsinit.3, mbstowcs.3, pthread_create.3,
>  pthread_setaffinity_np.3, queue.3, rtnetlink.3, shm_open.3, strptime.3,
>  tsearch.3, aio.7, fanotify.7, inotify.7, unix.7: Use sizeof consistently
> 
> Use ``sizeof`` consistently through all the examples, in the following
> way:

I would really have preferred three patches here, since:

> - Never use a space after ``sizeof``, and always use parentheses
>   instead.
> 
> 	Rationale:
> 
> 	https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces

(1) This is completely unproblematic from my point of view.

> - Use the name of the variable instead of the type as argument
>   for ``sizeof``, wherever possible.
> 
> 	Rationale:
> 
> 	https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory

(2) This one is up for debate. In many cases it makes sense to do
this. However, there are cases where I think that using the struct
name can actually help readability. And when I grep through the kernel
source, of around 139k lines that use "sizeof", some 37k take a 'struct type'
as an argument. SI, I think this kind of change may need to be considered on
a case by case basis, rather than as a blanket change.
 
> - When the result of ``sizeof`` is multiplied (or otherwise modified),
>   write ``sizeof`` in the first place.
> 
> 	Rationale:
> 
> 	``(sizeof(x) * INT_MAX * 2)`` doesn't overflow.
> 
> 	``(INT_MAX * 2 * sizeof(x))`` overflows, giving incorrect
> 	results.

(3) Is this true? "gcc -Wall" does not complain about this. And, I
thought that in both cases, all operands in the expression
would be promoted to the largest type. And, on my x86-64 system,

sizeof((sizeof(x) * INT_MAX * 2)) == 8
sizeof(INT_MAX * 2 * sizeof(x)) == 8

But, I will say tht I'm not a language lawyer, and C still
sometimes has surprises for me. At the least, I'd like to know
more about this point.

Thanks,

Michael



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux