halapi: Fix strncpy() warnings 49/302749/3
authorYoungjae Cho <y0.cho@samsung.com>
Wed, 13 Dec 2023 04:55:41 +0000 (13:55 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Fri, 22 Dec 2023 01:27:08 +0000 (10:27 +0900)
Those strncpy() cause warnings, -Wstringop-truncate, -Wstringop-overflow
due to the length calculated by strlen(). Replace the length with the
explicit size specified by the parameter. It is logically the same as
before.

Change-Id: I5b06f8595aa89f32fc7a6becab31130eb1178116
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/hal-api-common.c

index 3520a5892fc728f48b057d61b0427a33a6202172..8d6381833764e95ed7661585d59f9c4897eec660 100644 (file)
@@ -80,8 +80,7 @@ int hal_common_get_backend_library_name(enum hal_module module, char *name, int
                name = NULL;
                goto out;
        }
-       strncpy(name, library_name, len_library_name);
-       name[len_library_name] = '\0';
+       strncpy(name, library_name, size);
 
        ret = 0;
 out:
@@ -136,8 +135,7 @@ static int __hal_common_get_backend_name(enum hal_module module, char *name, int
                str = NULL;
                goto out;
        }
-       strncpy(name, str, len_str);
-       name[len_str] = '\0';
+       strncpy(name, str, size);
 
        ret = 0;
 out:
@@ -458,8 +456,7 @@ static int __get_backend_data(enum hal_module module, unsigned int *abi_version,
                        goto err_conf_exit;
                }
 
-               strncpy(name, info->backend->name, len);
-               name[len] = '\0';
+               strncpy(name, info->backend->name, name_size);
 
        /* Return vendor of hal_backend structure */
        } else if (info->backend->vendor && !name_size && vendor_size) {
@@ -471,8 +468,7 @@ static int __get_backend_data(enum hal_module module, unsigned int *abi_version,
                        goto err_conf_exit;
                }
 
-               strncpy(vendor, info->backend->vendor, len);
-               vendor[len] = '\0';
+               strncpy(vendor, info->backend->vendor, vendor_size);
        } else {
                _E("%s: Failed to get backend data\n", info->module_name);
                ret = -EINVAL;
@@ -641,7 +637,7 @@ static int __get_backend_library_data(enum hal_module module,
        struct dirent *de;
        DIR *dir;
        char *backend_module_name = NULL;
-       int count, i, ret, len;
+       int count, i, ret;
 #if defined(__aarch64__) || defined(__x86_64__) || defined (__riscv)
        const char hal_backend_path[] = "/hal/lib64";
 #else
@@ -696,12 +692,8 @@ static int __get_backend_library_data(enum hal_module module,
                if (lib_count == 0)
                        count++;
                else if (lib_count > 0) {
-                       len = strlen(de->d_name) + 1;
-
-                       if (len > lib_name_size)
-                               len = lib_name_size;
-
-                       strncpy(lib_names[count++], de->d_name, len);
+                       strncpy(lib_names[count], de->d_name, lib_name_size);
+                       count++;
                }
        }