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/>