[PATCH] drivers/video: Fix -Wstringop-truncation in hdmi.c

Thomas Zimmermann tzimmermann at suse.de
Sun Nov 1 16:43:55 UTC 2020


Hi

Am 01.11.20 um 10:50 schrieb Sam Ravnborg:
> On Wed, Oct 21, 2020 at 02:12:41PM +0200, Thomas Zimmermann wrote:
>> Trying to copy into the string fields with strncpy() gives a warning from
>> gcc. Both fields are part of a packed HDMI header and do not require a
>> terminating \0 character.
>>
>> ../drivers/video/hdmi.c: In function 'hdmi_spd_infoframe_init':
>> ../drivers/video/hdmi.c:230:2: warning: 'strncpy' specified bound 8 equals destination size [-Wstringop-truncation]
>>   230 |  strncpy(frame->vendor, vendor, sizeof(frame->vendor));
>>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> ../drivers/video/hdmi.c:231:2: warning: 'strncpy' specified bound 16 equals destination size [-Wstringop-truncation]
>>   231 |  strncpy(frame->product, product, sizeof(frame->product));
>>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Just use memcpy() instead.
>>
>> Signed-off-by: Thomas Zimmermann <tzimmermann at suse.de>
> I assume vendor is guaranteed to be 0-termindated.

It's a fixed-length field in the protocol. There's similar code in
radeon IIRC. So it would be nice to disable the warning in these cases.
But that's probably to much fiddling.

> Reviewed-by: Sam Ravnborg <sam at ravnborg.org>

Thanks.

Best regards
Thomas

> 
>> ---
>>  drivers/video/hdmi.c | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/video/hdmi.c b/drivers/video/hdmi.c
>> index b7a1d6fae90d..1e4cb63d0d11 100644
>> --- a/drivers/video/hdmi.c
>> +++ b/drivers/video/hdmi.c
>> @@ -221,14 +221,18 @@ EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
>>  int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
>>  			    const char *vendor, const char *product)
>>  {
>> +	size_t len;
>> +
>>  	memset(frame, 0, sizeof(*frame));
>>  
>>  	frame->type = HDMI_INFOFRAME_TYPE_SPD;
>>  	frame->version = 1;
>>  	frame->length = HDMI_SPD_INFOFRAME_SIZE;
>>  
>> -	strncpy(frame->vendor, vendor, sizeof(frame->vendor));
>> -	strncpy(frame->product, product, sizeof(frame->product));
>> +	len = strlen(vendor);
>> +	memcpy(frame->vendor, vendor, min(len, sizeof(frame->vendor)));
>> +	len = strlen(product);
>> +	memcpy(frame->product, product, min(len, sizeof(frame->product)));
>>  
>>  	return 0;
>>  }
>> -- 
>> 2.28.0
>>
>> _______________________________________________
>> dri-devel mailing list
>> dri-devel at lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/dri-devel

-- 
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Felix Imendörffer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0x680DC11D530B7A23.asc
Type: application/pgp-keys
Size: 4201 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/dri-devel/attachments/20201101/55b24577/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 495 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/dri-devel/attachments/20201101/55b24577/attachment.sig>


More information about the dri-devel mailing list