Fix the build error using gcc 13 accepted/tizen/unified/dev/20230726.115933
authorSangYoun Kwak <sy.kwak@samsung.com>
Tue, 4 Jul 2023 07:59:41 +0000 (16:59 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Thu, 6 Jul 2023 02:13:18 +0000 (11:13 +0900)
In the function "cfs_set_gadget_config" from
src/usb-gadget/usb-gadget-cfs-ops.c, it concatenates two strings and
stores it into a buffer(named instance) using snprintf.

In this situation, the gcc-13 compiler complains about: the length of
concatenated string can be greater than the size of the buffer, so
it should be handled.

It is already handled by checking the length of the two strings but the
compiler don't know about it.

To fix this situation, codes for checking the return value of snprintf
have been added.

Change-Id: I6195e72dd2f88bfeb72fb53b19227d2e08d03afb
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/usb-gadget/usb-gadget-cfs-ops.c

index 7379f52..08ed2f4 100644 (file)
@@ -402,7 +402,11 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s
                /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
                if (usb_func->is_functionfs) {
                        function_type = USBG_F_FFS;
-                       snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
+                       ret = snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
+                       if (ret < 0)
+                               return -EINVAL;
+                       if (sizeof(instance) <= ret)
+                               return -ENAMETOOLONG;
                } else {
                        function_type = usbg_lookup_function_type(usb_func->name);
                        strncpy(instance, usb_func->instance, sizeof(instance) - 1);