Thread (3 messages) 3 messages, 2 authors, 2024-08-08

Re: [PATCH v5 0/9] Improve the copy of task comm

From: Alejandro Colomar <alx@kernel.org>
Date: 2024-08-08 07:58:55
Also in: bpf, dri-devel, linux-fsdevel, linux-trace-kernel, netdev, selinux

Hi Yafang,

On Thu, Aug 08, 2024 at 10:49:17AM GMT, Yafang Shao wrote:
quoted
quoted
Now, it might be a good idea to also verify that 'buf' is an actual
array, and that this code doesn't do some silly "sizeof(ptr)" thing.
I decided to use NITEMS() instead of sizeof() for that reason.
(NITEMS() is just our name for ARRAY_SIZE().)

        $ grepc -h NITEMS .
        #define NITEMS(a)            (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
quoted
We do have a helper for that, so we could do something like

   #define get_task_comm(buf, tsk) \
        strscpy_pad(buf, __must_be_array(buf)+sizeof(buf), (tsk)->comm)
We have SIZEOF_ARRAY() for when you want the size of an array:

        $ grepc -h SIZEOF_ARRAY .
        #define SIZEOF_ARRAY(a)      (sizeof(a) + must_be_array(a))
There is already a similar macro in Linux:

  /**
   * ARRAY_SIZE - get the number of elements in array @arr
   * @arr: array to be sized
   */
  #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
__must_be_array(arr))
This is actually the same as our NITEMS(), not SIZEOF_ARRAY().
will use it instead of the sizeof().
But yeah, indeed I think you should use ARRAY_SIZE() in
get_task_comm().  :)
Good point.
I will avoid using the _pad().
Nice.  :)

Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es/>

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help