Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: v4l2-fwnode: suppress a warning at OF parsing logic

smatch produce this warning:
drivers/media/v4l2-core/v4l2-fwnode.c:76 v4l2_fwnode_endpoint_parse_csi_bus() error: buffer overflow 'array' 5 <= u16max

That's because, in thesis, the routine might have called with
some value at bus->num_data_lanes. That's not the current
case.

Yet, better to shut up this warning, and make the code more
reliable if some future changes might cause a bug.

While here, simplify the code a little bit by reading only
once from lanes-properties array.

Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>

+18 -20
+14 -18
drivers/media/v4l2-core/v4l2-fwnode.c
··· 48 48 49 49 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0); 50 50 if (rval > 0) { 51 - u32 array[ARRAY_SIZE(bus->data_lanes)]; 51 + u32 array[MAX_DATA_LANES + 1]; 52 52 53 - bus->num_data_lanes = 54 - min_t(int, ARRAY_SIZE(bus->data_lanes), rval); 53 + bus->num_data_lanes = min_t(int, MAX_DATA_LANES, rval); 55 54 56 55 fwnode_property_read_u32_array(fwnode, "data-lanes", array, 57 56 bus->num_data_lanes); ··· 63 64 64 65 bus->data_lanes[i] = array[i]; 65 66 } 66 - } 67 67 68 - rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL, 69 - 0); 70 - if (rval > 0) { 71 - u32 array[ARRAY_SIZE(bus->lane_polarities)]; 68 + rval = fwnode_property_read_u32_array(fwnode, 69 + "lane-polarities", array, 70 + 1 + bus->num_data_lanes); 71 + if (rval > 0) { 72 + if (rval != 1 + bus->num_data_lanes /* clock + data */) { 73 + pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n", 74 + 1 + bus->num_data_lanes, rval); 75 + return -EINVAL; 76 + } 72 77 73 - if (rval < 1 + bus->num_data_lanes /* clock + data */) { 74 - pr_warn("too few lane-polarities entries (need %u, got %u)\n", 75 - 1 + bus->num_data_lanes, rval); 76 - return -EINVAL; 78 + 79 + for (i = 0; i < 1 + bus->num_data_lanes; i++) 80 + bus->lane_polarities[i] = array[i]; 77 81 } 78 - 79 - fwnode_property_read_u32_array(fwnode, "lane-polarities", array, 80 - 1 + bus->num_data_lanes); 81 - 82 - for (i = 0; i < 1 + bus->num_data_lanes; i++) 83 - bus->lane_polarities[i] = array[i]; 84 82 } 85 83 86 84 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
+4 -2
include/media/v4l2-fwnode.h
··· 26 26 27 27 struct fwnode_handle; 28 28 29 + #define MAX_DATA_LANES 4 30 + 29 31 /** 30 32 * struct v4l2_fwnode_bus_mipi_csi2 - MIPI CSI-2 bus data structure 31 33 * @flags: media bus (V4L2_MBUS_*) flags ··· 39 37 */ 40 38 struct v4l2_fwnode_bus_mipi_csi2 { 41 39 unsigned int flags; 42 - unsigned char data_lanes[4]; 40 + unsigned char data_lanes[MAX_DATA_LANES]; 43 41 unsigned char clock_lane; 44 42 unsigned short num_data_lanes; 45 - bool lane_polarities[5]; 43 + bool lane_polarities[MAX_DATA_LANES + 1]; 46 44 }; 47 45 48 46 /**