Add flex-array size helper

-----BEGIN PGP SIGNATURE-----
 
 iQJKBAABCgA0FiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAl7sJLcWHGtlZXNjb29r
 QGNocm9taXVtLm9yZwAKCRCJcvTf3G3AJux3D/9RCaL24L5lNMZrfWo6lHhXUain
 EdUUdnzKB2uschPXcFPm7olaAGrwh/nlbJc51uKWrdEaLbM2xausC6T+tg5K/fJc
 1o/Rhe74GhuMlZqcJwKG90pM/11lHQsoYavNl3SZLiDwgIIXxOzdP/ZMN3K+Ug75
 W/5eVhwdU5A87tCXU50UwcAWvLw2kBQzS7HDa4CWuiV4cACNBaTtiUomJ/yCFE5V
 gZW139HM/XGFKMNxLvICT00ZznePDcvYbXlXdRNGKKC8O8t0Si6Xb7TLMUhcBNIQ
 Npvz2g+hofhiFlVDMOpOeAxRrkb7gdYaOqTHk9/YcxarsHkJA5EN7HQnjMnNScNf
 5HIFnP5IOrcnjA009qM+tvFCMJo3FglBSr3pLCcnUoFwnut7sY8+z67JaeVRp2Hc
 3SOcXWamh4HJW+YsDR7bAgBkUPq0FmMY6oGVqIqwTWQJUwGHS4RljFl43HqFn7Fm
 Bggk1OBjm4Kifj0EwujBdLDNuaaNMb2bmSFr4L6CJcQBU7gOyT87TuSOHR5oI7U7
 ywQDr9sQIB70wmQ27z9OqZwEnTIV0LzlD0ZfrhkNS4bcK42livS0qv3hx8YnPaTg
 a0leNmHB6qwkIxoFdCMY2E6ZmpCPUAwPk8NqTPrPmwfMoDbSlMW/6ENjYSyiFB8r
 nbpx2KU0JjsRlYfkhA==
 =94sc
 -----END PGP SIGNATURE-----

Merge tag 'overflow-v5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull flex-array size helper from Kees Cook:
 "During the treewide clean-ups of zero-length "flexible arrays", the
  struct_size() helper was heavily used, but it was noticed that many
  times it would have been nice to have an additional helper to get the
  size of just the flexible array itself.

  This need appears to be even more common when cleaning up the 1-byte
  array "flexible arrays", so Gustavo implemented it.

  I'd love to get this landed early so it can be used during the v5.9
  dev cycle to ease the 1-byte array cleanups."

* tag 'overflow-v5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
  overflow.h: Add flex_array_size() helper
This commit is contained in:
Linus Torvalds 2020-06-19 11:45:03 -07:00
commit 98b769942c

View File

@ -304,16 +304,33 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
* struct_size() - Calculate size of structure with trailing array.
* @p: Pointer to the structure.
* @member: Name of the array member.
* @n: Number of elements in the array.
* @count: Number of elements in the array.
*
* Calculates size of memory needed for structure @p followed by an
* array of @n @member elements.
* array of @count number of @member elements.
*
* Return: number of bytes needed or SIZE_MAX on overflow.
*/
#define struct_size(p, member, n) \
__ab_c_size(n, \
#define struct_size(p, member, count) \
__ab_c_size(count, \
sizeof(*(p)->member) + __must_be_array((p)->member),\
sizeof(*(p)))
/**
* flex_array_size() - Calculate size of a flexible array member
* within an enclosing structure.
*
* @p: Pointer to the structure.
* @member: Name of the flexible array member.
* @count: Number of elements in the array.
*
* Calculates size of a flexible array of @count number of @member
* elements, at the end of structure @p.
*
* Return: number of bytes needed or SIZE_MAX on overflow.
*/
#define flex_array_size(p, member, count) \
array_size(count, \
sizeof(*(p)->member) + __must_be_array((p)->member))
#endif /* __LINUX_OVERFLOW_H */