From fa8141f31fe7726ff00fbbe00b26b297c98f5ca2 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 3 Mar 2019 19:41:41 -0800 Subject: [PATCH] Fixes #33: Remove invalid use of strncpy(). This was causing a stringop-truncation compiler warning in gcc 8 when the #defined values being copied from were string literals. `error: 'strncpy' output truncated before terminating nul copying 8 bytes from a string of the same length [-Werror=stringop-truncation]` These fields aren't NUL terminated C strings, they are a fixed width buffer that is supposed to be space (0x20) padded. --- src/class/msc/msc_device.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index d8bb7221..abc05125 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -265,14 +265,20 @@ int32_t proc_builtin_scsi(msc_cbw_t const * p_cbw, uint8_t* buffer, uint32_t buf .is_removable = 1, .version = 2, .response_data_format = 2, - .vendor_id = "Adafruit", - .product_id = "Feather52840", - .product_rev = "1.0" + .vendor_id = " ", + .product_id = " ", + .product_rev = " ", }; + size_t len; - strncpy((char*) inquiry_rsp.vendor_id , CFG_TUD_MSC_VENDOR , sizeof(inquiry_rsp.vendor_id)); - strncpy((char*) inquiry_rsp.product_id , CFG_TUD_MSC_PRODUCT , sizeof(inquiry_rsp.product_id)); - strncpy((char*) inquiry_rsp.product_rev, CFG_TUD_MSC_PRODUCT_REV, sizeof(inquiry_rsp.product_rev)); + #define _min(a,b) ((a) < (b) ? (a) : (b)) + len = strlen(CFG_TUD_MSC_VENDOR); + memcpy(inquiry_rsp.vendor_id , CFG_TUD_MSC_VENDOR , _min(len, sizeof(inquiry_rsp.vendor_id))); + len = strlen(CFG_TUD_MSC_PRODUCT); + memcpy(inquiry_rsp.product_id , CFG_TUD_MSC_PRODUCT , _min(len, sizeof(inquiry_rsp.product_id))); + len = strlen(CFG_TUD_MSC_PRODUCT_REV); + memcpy(inquiry_rsp.product_rev, CFG_TUD_MSC_PRODUCT_REV, _min(len, sizeof(inquiry_rsp.product_rev))); + #undef _min ret = sizeof(inquiry_rsp); memcpy(buffer, &inquiry_rsp, ret);