From ab6f05528fc5cc7ea1bf9dc6960c48b6a2b35d88 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 22 Dec 2023 10:18:16 +0900 Subject: [PATCH] halapi: Add NULL check for strncpy() dest buffer Change-Id: I6dbd911d246edc49b2eecabe4eb2400affb86b9e Signed-off-by: Youngjae Cho --- src/hal-api-common.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/hal-api-common.c b/src/hal-api-common.c index 8d63818..3d1cd7d 100644 --- a/src/hal-api-common.c +++ b/src/hal-api-common.c @@ -57,6 +57,9 @@ int hal_common_get_backend_library_name(enum hal_module module, char *name, int return -EINVAL; } + if (!name) + return -EINVAL; + if (_hal_api_conf_init()) return -EINVAL; @@ -75,9 +78,8 @@ int hal_common_get_backend_library_name(enum hal_module module, char *name, int } len_library_name = strlen(library_name); - if (!name || (len_library_name + 1 > size) || len_library_name == 0) { + if ((len_library_name + 1 > size) || len_library_name == 0) { ret = -EINVAL; - name = NULL; goto out; } strncpy(name, library_name, size); @@ -102,6 +104,9 @@ static int __hal_common_get_backend_name(enum hal_module module, char *name, int return -EINVAL; } + if (!name) + return -EINVAL; + if (_hal_api_conf_init()) return -EINVAL; @@ -447,7 +452,7 @@ static int __get_backend_data(enum hal_module module, unsigned int *abi_version, *abi_version = info->backend->abi_version; /* Return name of hal_backend structure */ - } else if (info->backend->name && name_size && !vendor_size) { + } else if (info->backend->name && name && name_size && !vendor_size) { len = strlen(info->backend->name); if (!info->backend->name || (len + 1 > name_size)) { @@ -459,7 +464,7 @@ static int __get_backend_data(enum hal_module module, unsigned int *abi_version, strncpy(name, info->backend->name, name_size); /* Return vendor of hal_backend structure */ - } else if (info->backend->vendor && !name_size && vendor_size) { + } else if (info->backend->vendor && vendor && !name_size && vendor_size) { len = strlen(info->backend->vendor); if (!info->backend->vendor || (len + 1 > vendor_size)) { @@ -692,6 +697,8 @@ static int __get_backend_library_data(enum hal_module module, if (lib_count == 0) count++; else if (lib_count > 0) { + if (!lib_names[count]) + goto err_uninitialized_buffer; strncpy(lib_names[count], de->d_name, lib_name_size); count++; } @@ -708,6 +715,7 @@ static int __get_backend_library_data(enum hal_module module, return count; +err_uninitialized_buffer: err_mismatch_count: for (i = count - 1; i >= 0; i--) memset(lib_names[i], 0, strlen(lib_names[i])); -- 2.7.4