diff mbox series

[v2,2/2] media: videobuf2: add WARN_ON if bytesused is bigger than buffer length

Message ID 20211111152640.1537-3-dafna.hirschfeld@collabora.com (mailing list archive)
State New, archived
Headers show
Series media: videobuf2: make sure bytesused is smaller than the buffer size | expand

Commit Message

Dafna Hirschfeld Nov. 11, 2021, 3:26 p.m. UTC
In function vb2_set_plane_payload, report if the
given bytesused is bigger than the buffer size,
and clamp it to the buffer size.

Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@collabora.com>
---
 include/media/videobuf2-core.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Laurent Pinchart Nov. 12, 2021, 12:10 a.m. UTC | #1
Hi Dafna,

Thank you for the patch.

On Thu, Nov 11, 2021 at 05:26:40PM +0200, Dafna Hirschfeld wrote:
> In function vb2_set_plane_payload, report if the
> given bytesused is bigger than the buffer size,
> and clamp it to the buffer size.
> 
> Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@collabora.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  include/media/videobuf2-core.h | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
> index 2467284e5f26..e06c190265f0 100644
> --- a/include/media/videobuf2-core.h
> +++ b/include/media/videobuf2-core.h
> @@ -1155,8 +1155,14 @@ static inline void *vb2_get_drv_priv(struct vb2_queue *q)
>  static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
>  				 unsigned int plane_no, unsigned long size)
>  {
> -	if (plane_no < vb->num_planes)
> +	/*
> +	 * size must never be larger than the buffer length, so
> +	 * warn and clamp to the buffer length if that's the case.
> +	 */
> +	if (plane_no < vb->num_planes) {
> +		WARN_ON_ONCE(size > vb->planes[plane_no].length);
>  		vb->planes[plane_no].bytesused = size;
> +	}
>  }
>  
>  /**
Hans Verkuil Nov. 24, 2021, 3:47 p.m. UTC | #2
On 11/11/2021 16:26, Dafna Hirschfeld wrote:
> In function vb2_set_plane_payload, report if the
> given bytesused is bigger than the buffer size,
> and clamp it to the buffer size.
> 
> Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@collabora.com>
> ---
>  include/media/videobuf2-core.h | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
> index 2467284e5f26..e06c190265f0 100644
> --- a/include/media/videobuf2-core.h
> +++ b/include/media/videobuf2-core.h
> @@ -1155,8 +1155,14 @@ static inline void *vb2_get_drv_priv(struct vb2_queue *q)
>  static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
>  				 unsigned int plane_no, unsigned long size)
>  {
> -	if (plane_no < vb->num_planes)
> +	/*
> +	 * size must never be larger than the buffer length, so
> +	 * warn and clamp to the buffer length if that's the case.
> +	 */
> +	if (plane_no < vb->num_planes) {
> +		WARN_ON_ONCE(size > vb->planes[plane_no].length);

This doesn't clamp size to the buffer length, so this needs to be added.

Also, the subject talks about WARN_ON instead of WARN_ON_ONCE.

Regards,

	Hans

>  		vb->planes[plane_no].bytesused = size;
> +	}
>  }
>  
>  /**
>
diff mbox series

Patch

diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 2467284e5f26..e06c190265f0 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -1155,8 +1155,14 @@  static inline void *vb2_get_drv_priv(struct vb2_queue *q)
 static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
 				 unsigned int plane_no, unsigned long size)
 {
-	if (plane_no < vb->num_planes)
+	/*
+	 * size must never be larger than the buffer length, so
+	 * warn and clamp to the buffer length if that's the case.
+	 */
+	if (plane_no < vb->num_planes) {
+		WARN_ON_ONCE(size > vb->planes[plane_no].length);
 		vb->planes[plane_no].bytesused = size;
+	}
 }
 
 /**