video/hdmi: Change strncpy() into memcpy() in hdmi_spd_infoframe_init
PATCH 1/1 video/hdmi: Change strncpy() into memcpy() in hdmi_spd_infoframe_init
Using strncpy() is less than perfect since the destination buffers do not need to be NUL terminated. Replace strncpy() with memcpy() to address a warning triggered by gcc using W=1 and optimize the code a bit.
This commit removes the following warnings:
drivers/video/hdmi.c:234:2: warning: 'strncpy' specified bound 8 equals destination size [-Wstringop-truncation] drivers/video/hdmi.c:235:2: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
--- drivers/video/hdmi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/video/hdmi.c +2 -2
@@ -231,8 +231,8 @@ int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, frame->version =1 ; frame->length =HDMI_SPD_INFOFRAME_SIZE ; -strncpy (frame->vendor, vendor,sizeof (frame->vendor)); -strncpy (frame->product, product,sizeof (frame->product)); +memcpy (frame->vendor, vendor,sizeof (frame->vendor)); +memcpy (frame->product, product,sizeof (frame->product));return 0 ; }
Signature · 1 line
2.19.2
Quoted 8 lines · Mathieu Malaterre
Using
strncpy()is less than perfect since the destination buffers do not need to be NUL terminated. Replacestrncpy()withmemcpy()to address a warning triggered by gcc using W=1 and optimize the code a bit.This commit removes the following warnings:
drivers/video/hdmi.c:234:2: warning: 'strncpy' specified bound 8 equals destination size [-Wstringop-truncation]drivers/video/hdmi.c:235:2: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
[]
drivers/video/hdmi.c
Quoted 1 line
drivers/video/hdmi.c +0
-0
[]
drivers/video/hdmi.c int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, · line 235
Quoted 8 lines
drivers/video/hdmi.c +2
-2@@ -231,8 +231,8 @@ int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, frame->version =1 ; frame->length =HDMI_SPD_INFOFRAME_SIZE ; -strncpy (frame->vendor, vendor,sizeof (frame->vendor)); -strncpy (frame->product, product,sizeof (frame->product)); +memcpy (frame->vendor, vendor,sizeof (frame->vendor)); +memcpy (frame->product, product,sizeof (frame->product));
This is not good.
vendor can be any location and shorter than sizeof(frame->vendor) so this can copy from invalid memory locations.
You probably want strscpy.
This is called with at least "mediatek" and "broadcom", so perhaps it's better still to change the struct vendor size to something a bit larger. Maybe change vendor[8] to vendor[16];
include/linux/hdmi.h:struct hdmi_spd_infoframe {
include/linux/hdmi.h- enum hdmi_infoframe_type type; include/linux/hdmi.h- unsigned char version; include/linux/hdmi.h- unsigned char length; include/linux/hdmi.h- char vendor[8]; include/linux/hdmi.h- char product[16]; include/linux/hdmi.h- enum hdmi_spd_sdi sdi;
include/linux/hdmi.h-};
Quoted 24 lines · Joe Perches
Quoted 8 lines · Mathieu Malaterre
Using
strncpy()is less than perfect since the destination buffers do not need to be NUL terminated. Replacestrncpy()withmemcpy()to address a warning triggered by gcc using W=1 and optimize the code a bit.This commit removes the following warnings:
drivers/video/hdmi.c:234:2: warning: 'strncpy' specified bound 8 equals destination size [-Wstringop-truncation]drivers/video/hdmi.c:235:2: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation][]
drivers/video/hdmi.c
Quoted 1 line
drivers/video/hdmi.c +0
-0[]
drivers/video/hdmi.c int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, · line 235
Quoted 8 lines
drivers/video/hdmi.c +2
-2@@ -231,8 +231,8 @@ int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, frame->version =1 ; frame->length =HDMI_SPD_INFOFRAME_SIZE ; -strncpy (frame->vendor, vendor,sizeof (frame->vendor)); -strncpy (frame->product, product,sizeof (frame->product)); +memcpy (frame->vendor, vendor,sizeof (frame->vendor)); +memcpy (frame->product, product,sizeof (frame->product));This is not good.
Quoted 2 lines
vendor can be any location and shorter than
sizeof(frame->vendor)so this can copy from invalid memory locations.
Ah right. I did not realize that and know I see the call with "Intel", will re-spin.
Quoted 1 line
You probably want strscpy.
Right.
Quoted 3 lines
This is called with at least "mediatek" and "broadcom", so perhaps it's better still to change the struct vendor size to something a bit larger. Maybe change vendor[8] to vendor[16];
Looks like 8 bytes is required for call like hdmi_spd_infoframe_unpack()
Quoted 10 lines
include/linux/hdmi.h:struct
hdmi_spd_infoframe{include/linux/hdmi.h- enum hdmi_infoframe_type type; include/linux/hdmi.h- unsigned char version; include/linux/hdmi.h- unsigned char length; include/linux/hdmi.h- char vendor[8]; include/linux/hdmi.h- char product[16]; include/linux/hdmi.h- enum hdmi_spd_sdi sdi;
include/linux/hdmi.h-};