util: Ensure ARRAY_SIZE() will only take arrays
This code is inspired by a similar feature found in ccan[1], that enforces that the parameter passed to ARRAY_SIZE() is always an array and not a generic pointer. This is a slightly modified version that will work if the macro is expanded outside of a function body. The check is performed by comparing if typeof(array) and typeof(&array[0]) are of a different type. Due to the way arrays decays to pointers in C, if one passes a pointer, the types won't be compatible and a compile time assertion will fail. No bugs have been found with this change, but since there's no runtime or size overheads, there's no reason to not enable it. [1] https://github.com/rustyrussell/ccan/blob/master/ccan/array_size/\ array_size.h Change-Id: I6c321714d0024298e593176be43b2d0b5362cc0d Signed-off-by:Leandro Pereira <leandro.pereira@intel.com>
Loading
Please sign in to comment