From 49f32a1a782427e550a995e038da14219bcce0db Mon Sep 17 00:00:00 2001 From: Torsten Schulz Date: Wed, 9 Nov 2022 09:49:43 -0800 Subject: [PATCH] [decoder/pose] Fix SEGV, ignore pixel when coordinates are out of bounds. Under some circumstances, setpixel() is called with y==data.height which causes a SEGV. While this might be a rounding error in the caller draw(), the added code gracefully ignores such out-of-bound pixels gracefully. Signed-off-by: Torsten Schulz --- ext/nnstreamer/tensor_decoder/tensordec-pose.c | 41 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/ext/nnstreamer/tensor_decoder/tensordec-pose.c b/ext/nnstreamer/tensor_decoder/tensordec-pose.c index 19bb02c..4995085 100644 --- a/ext/nnstreamer/tensor_decoder/tensordec-pose.c +++ b/ext/nnstreamer/tensor_decoder/tensordec-pose.c @@ -514,6 +514,25 @@ typedef struct } pose; /** + * @brief Check if a value is within lower and upper bounds + * @param value the value to check + * @param lower_b the lower bound (inclusive) + * @param upper_b the uppoer bound (exlcusive) + * @return TRUE if the value is within the bounds, otherwise FALSE + */ +static gboolean +is_value_within(int value, int lower_b, int upper_b) +{ + if (value < lower_b) { + return FALSE; + } else if (value >= upper_b) { + return FALSE; + } else { + return TRUE; + } +} + +/** * @brief Fill in pixel with PIXEL_VALUE at x,y position. Make thicker (x+1, y+1) * @param[out] out_info The output buffer (RGBA plain) * @param[in] bdata The bouding-box internal data. @@ -522,17 +541,19 @@ typedef struct static void setpixel (uint32_t * frame, pose_data * data, int x, int y) { - uint32_t *pos = &frame[y * data->width + x]; - *pos = PIXEL_VALUE; + if (is_value_within(x, 0, data->width) && is_value_within(y, 0, data->height)) { + uint32_t *pos = &frame[y * data->width + x]; + *pos = PIXEL_VALUE; - if (x + 1 < (int) data->width) { - pos = &frame[y * data->width + x + 1]; - *pos = PIXEL_VALUE; - } - if (y + 1 < (int) data->height) { - pos = &frame[(y + 1) * data->width + x]; - *pos = PIXEL_VALUE; - } + if (x + 1 < (int) data->width) { + pos = &frame[y * data->width + x + 1]; + *pos = PIXEL_VALUE; + } + if (y + 1 < (int) data->height) { + pos = &frame[(y + 1) * data->width + x]; + *pos = PIXEL_VALUE; + } + } } /** -- 2.7.4