diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e7dcb6..95b5f96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,6 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src) OPTION(ENABLE_LLT "llt switch" OFF) IF(ENABLE_LLT) enable_testing() - add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test) ENDIF(ENABLE_LLT) # install all files diff --git a/cmake/checker.cmake b/cmake/checker.cmake index 9596dca..016af3e 100644 --- a/cmake/checker.cmake +++ b/cmake/checker.cmake @@ -14,13 +14,6 @@ endmacro() find_program(CMD_PYTHON python) _CHECK(CMD_PYTHON "CMD_PYTHON-NOTFOUND" "python") -# check securec -find_path(LIBSECUREC_INCLUDE_DIR securec.h) -_CHECK(LIBSECUREC_INCLUDE_DIR "LIBSECUREC_INCLUDE_DIR-NOTFOUND" "securec.h") - -find_library(LIBSECUREC_LIBRARY securec) -_CHECK(LIBSECUREC_LIBRARY "LIBSECUREC_LIBRARY-NOTFOUND" "libsecurec.so") - # check liblxc pkg_check_modules(PC_LIBLXC REQUIRED "lxc>=3") find_path(LIBLXC_INCLUDE_DIR lxc/lxccontainer.h diff --git a/lcr.spec b/lcr.spec index 6e2c181..68f2a86 100644 --- a/lcr.spec +++ b/lcr.spec @@ -14,7 +14,6 @@ BuildRequires: cmake BuildRequires: lxc BuildRequires: lxc-devel BuildRequires: yajl yajl-devel -BuildRequires: libsecurec libsecurec-devel Requires: rsync bridge-utils lxc ExclusiveArch: x86_64 aarch64 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d8325a3..9abc297 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,12 +15,10 @@ message("-- Get top json srcs: " ${topjsonsrcs}) add_library(liblcr ${LIBTYPE} ${topsrcs} ${topjsonsrcs} ${commonjsonsrcs} ${generatesrcs}) set(check_incs - ${LIBSECUREC_INCLUDE_DIR} ${LIBLXC_INCLUDE_DIR} ${LIBYAJL_INCLUDE_DIR} ) set(check_libs - ${LIBSECUREC_LIBRARY} ${LIBLXC_LIBRARY} ${LIBYAJL_LIBRARY} ) diff --git a/src/buffer.c b/src/buffer.c index 35e00fa..acafb0f 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -16,7 +16,6 @@ #include #include -#include #include "buffer.h" #include "log.h" @@ -84,7 +83,6 @@ static bool buffer_has_space(const Buffer *buf, size_t desired_length) static int buffer_grow(Buffer *buf, size_t minimum_size) { size_t factor = 0; - errno_t ret = 0; size_t new_size = 0; char *tmp = NULL; @@ -112,12 +110,7 @@ static int buffer_grow(Buffer *buf, size_t minimum_size) return -1; } - ret = memcpy_s(tmp, new_size, buf->contents, buf->total_size); - if (ret != EOK) { - ERROR("Failed to copy memory"); - free(tmp); - return -1; - } + (void)memcpy(tmp, buf->contents, buf->total_size); free(buf->contents); buf->contents = tmp; @@ -192,7 +185,7 @@ int buffer_nappendf(Buffer *buf, size_t length, const char *format, ...) } va_start(argp, format); - status = vsprintf_s(tmp, printf_length, format, argp); + status = vsprintf(tmp, format, argp); va_end(argp); if (status < 0) { goto error; @@ -229,11 +222,7 @@ char *buffer_to_s(const Buffer *buf) if (result == NULL) { return NULL; } - if (strncpy_s(result, len + 1, buf->contents, len) != EOK) { - ERROR("Failed to copy string!"); - free(result); - return NULL; - } + (void)strncpy(result, buf->contents, len); return result; } diff --git a/src/conf.c b/src/conf.c index edac782..2fdffbe 100644 --- a/src/conf.c +++ b/src/conf.c @@ -29,7 +29,6 @@ #include "utils.h" #include "log.h" #include "buffer.h" -#include "securec.h" #define SUB_UID_PATH "/etc/subuid" #define SUB_GID_PATH "/etc/subgid" @@ -299,7 +298,6 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len) size_t sep_len; size_t result_len; size_t iter; - int nret = 0; sep_len = strlen(sep); if (valid_sep_len(sep_len, len) == false) { @@ -322,20 +320,10 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len) } for (iter = 0; iter < len - 1; iter++) { - nret = strcat_s(result, result_len + 1, &(parts[iter][4])); - if (nret != EOK) { - goto err_out; - } - - nret = strcat_s(result, result_len + 1, sep); - if (nret != EOK) { - goto err_out; - } - } - nret = strcat_s(result, result_len + 1, &(parts[len - 1][4])); - if (nret != EOK) { - goto err_out; + (void)strcat(result, &(parts[iter][4])); + (void)strcat(result, sep); } + (void)strcat(result, &(parts[len - 1][4])); // Lower case for (iter = 0; iter < result_len; iter++) { @@ -345,10 +333,6 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len) } return result; - -err_out: - free(result); - return NULL; } #define UID_MAX_SIZE 21 @@ -360,8 +344,8 @@ static int trans_oci_process_init_uid(const oci_runtime_spec_process *proc, stru int nret; int ret = -1; if (proc->user != NULL && proc->user->uid != INVALID_INT) { - nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid); - if (nret < 0) { + nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid); + if (nret < 0 || (size_t)nret >= sizeof(buf)) { goto out; } @@ -384,8 +368,8 @@ static int trans_oci_process_init_gid(const oci_runtime_spec_process *proc, stru int nret; int ret = -1; if (proc->user != NULL && proc->user->gid != INVALID_INT) { - nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid); - if (nret < 0) { + nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid); + if (nret < 0 || (size_t)nret >= sizeof(buf)) { goto out; } @@ -418,16 +402,16 @@ static int trans_oci_process_init_groups(const oci_runtime_spec_process *proc, s goto out; } - nret = sprintf_s(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0])); - if (nret < 0) { + nret = snprintf(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0])); + if (nret < 0 || (size_t)nret >= total_len) { free(gids); goto out; } for (i = 1; i < proc->user->additional_gids_len; i++) { size_t old_len = strlen(gids); - nret = sprintf_s(gids + old_len, total_len - old_len, " %u", - (unsigned int)(proc->user->additional_gids[i])); - if (nret < 0) { + nret = snprintf(gids + old_len, total_len - old_len, " %u", + (unsigned int)(proc->user->additional_gids[i])); + if (nret < 0 || (size_t)nret >= (total_len - old_len)) { free(gids); goto out; } @@ -577,16 +561,16 @@ static int trans_oci_process_prlimit(const oci_runtime_spec_process *proc, struc } // Skip `RLIMIT_` - nret = sprintf_s(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7])); + nret = snprintf(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7])); free(type); - if (nret < 0) { + if (nret < 0 || (size_t)nret >= sizeof(buf_key)) { goto out; } // We always use format `soft_limit:hard_limit` - nret = sprintf_s(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft, - (unsigned long long)lr->hard); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft, + (unsigned long long)lr->hard); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -766,8 +750,8 @@ static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, stru goto out; } value = tmpvalue; - nret = sprintf_s(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation); - if (nret < 0) { + nret = snprintf(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation); + if (nret < 0 || (size_t)nret >= (newsize - strlen(value))) { ERROR("Failed to print string"); goto out; } @@ -952,8 +936,8 @@ static struct lcr_list *trans_mount_auto_to_lxc(const defs_mount *mount) goto out_free; } - ret = sprintf_s(buf, buf_len, "%s:%s", type, options); - if (ret < 0) { + ret = snprintf(buf, buf_len, "%s:%s", type, options); + if (ret < 0 || (size_t)ret >= buf_len) { DEBUG("Failed to print string"); goto out_free; } @@ -1000,8 +984,8 @@ static struct lcr_list *trans_mount_entry_to_lxc(const defs_mount *mount) goto out_free; } - ret = sprintf_s(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options); - if (ret < 0) { + ret = snprintf(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options); + if (ret < 0 || (size_t)ret >= buf_len) { ERROR("Failed to print string"); goto out_free; } @@ -1121,8 +1105,8 @@ static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, cons char buf_value[300] = { 0 }; char subid[ID_MAP_LEN] = { 0 }; - nret = sprintf_s(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { return -1; } @@ -1132,8 +1116,8 @@ static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, cons } lcr_list_add_tail(conf, node); - nret = sprintf_s(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size); - if (nret < 0) { + nret = snprintf(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size); + if (nret < 0 || (size_t)nret >= sizeof(subid)) { return -1; } nret = util_atomic_write_file(path, subid); @@ -1208,8 +1192,8 @@ static int trans_conf_int(struct lcr_list *conf, const char *lxc_key, int val) char buf_value[300] = { 0 }; int nret; - nret = sprintf_s(buf_value, sizeof(buf_value), "%d", val); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%d", val); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { return -1; } node = create_lcr_list_node(lxc_key, buf_value); @@ -1226,8 +1210,8 @@ static int trans_conf_uint32(struct lcr_list *conf, const char *lxc_key, uint32_ char buf_value[300] = { 0 }; int nret; - nret = sprintf_s(buf_value, sizeof(buf_value), "%u", (unsigned int)val); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%u", (unsigned int)val); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { return -1; } node = create_lcr_list_node(lxc_key, buf_value); @@ -1244,8 +1228,8 @@ static int trans_conf_int64(struct lcr_list *conf, const char *lxc_key, int64_t char buf_value[300] = { 0 }; int nret; - nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)val); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)val); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { return -1; } node = create_lcr_list_node(lxc_key, buf_value); @@ -1262,8 +1246,8 @@ static int trans_conf_uint64(struct lcr_list *conf, const char *lxc_key, uint64_ char buf_value[300] = { 0 }; int nret; - nret = sprintf_s(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { return -1; } node = create_lcr_list_node(lxc_key, buf_value); @@ -1412,11 +1396,11 @@ static int trans_resources_devices_no_match(const oci_runtime_defs_linux_device_ { int ret = 0; if (lrd->minor != WILDCARD) { - ret = sprintf_s(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), - (long long)lrd->minor, lrd->access ? lrd->access : "rwm"); + ret = snprintf(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), + (long long)lrd->minor, lrd->access ? lrd->access : "rwm"); } else { - ret = sprintf_s(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), - lrd->access ? lrd->access : "rwm"); + ret = snprintf(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major), + lrd->access ? lrd->access : "rwm"); } return ret; @@ -1426,10 +1410,10 @@ static int trans_resources_devices_match(const oci_runtime_defs_linux_device_cgr { int ret = 0; if (lrd->minor != WILDCARD) { - ret = sprintf_s(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor), - lrd->access ? lrd->access : "rwm"); + ret = snprintf(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor), + lrd->access ? lrd->access : "rwm"); } else { - ret = sprintf_s(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm"); + ret = snprintf(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm"); } return ret; @@ -1610,9 +1594,9 @@ static int trans_blkio_wdevice(const oci_runtime_config_linux_resources_block_io int nret; oci_runtime_defs_linux_block_io_device_weight *wd = block_io->weight_device[i]; if ((wd != NULL) && wd->weight != INVALID_INT) { - nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor, - wd->weight); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor, + wd->weight); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -1623,9 +1607,9 @@ static int trans_blkio_wdevice(const oci_runtime_config_linux_resources_block_io lcr_list_add_tail(conf, node); } if ((wd != NULL) && wd->leaf_weight != INVALID_INT) { - nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), - (long long)(wd->minor), wd->leaf_weight); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), + (long long)(wd->minor), wd->leaf_weight); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -1657,9 +1641,9 @@ static int trans_blkio_throttle(oci_runtime_defs_linux_block_io_device_throttle if (throttle[i] && throttle[i]->rate != INVALID_INT) { int nret; char buf_value[300] = { 0x00 }; - nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major, - (long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate)); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major, + (long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate)); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -1727,7 +1711,8 @@ static int trans_resources_hugetlb(const oci_runtime_config_linux_resources *res for (i = 0; i < res->hugepage_limits_len; i++) { oci_runtime_config_linux_resources_hugepage_limits_element *lrhl = res->hugepage_limits[i]; if (lrhl->page_size != NULL) { - if (sprintf_s(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size) < 0) { + int nret = snprintf(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size); + if (nret < 0 || (size_t)nret >= sizeof(buf_key)) { goto out; } @@ -1762,7 +1747,8 @@ static int trans_resources_network(const oci_runtime_config_linux_resources *res for (i = 0; i < res->network->priorities_len; i++) { oci_runtime_defs_linux_network_interface_priority *lrnp = res->network->priorities[i]; if ((lrnp != NULL) && lrnp->name != NULL && lrnp->priority != INVALID_INT) { - if (sprintf_s(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority) < 0) { + int nret = snprintf(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -1792,11 +1778,11 @@ static int trans_resources_pids(const oci_runtime_config_linux_resources *res, s if (res->pids->limit != INVALID_INT) { int nret; if (res->pids->limit == -1) { - nret = sprintf_s(buf_value, sizeof(buf_value), "max"); + nret = snprintf(buf_value, sizeof(buf_value), "max"); } else { - nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit)); + nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit)); } - if (nret < 0) { + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { goto out; } @@ -2001,10 +1987,10 @@ static struct lcr_list *trans_oci_linux_devices(const oci_runtime_config_linux * /* lxc.populate_device = PATH_IN_CONTAINER:DEVICETYPE:MAJOR:MINOR:MODE:UID:GID * For e.g. lxc.populate_device = /dev/sda:b:8:0:0666:0:0 */ - nret = sprintf_s(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type, - (long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid, - device->gid); - if (nret < 0) { + nret = snprintf(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type, + (long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid, + device->gid); + if (nret < 0 || (size_t)nret >= sizeof(buf_value)) { ERROR("Failed to get populate device string"); goto out_free; } @@ -2293,8 +2279,8 @@ static struct lcr_list *trans_oci_linux_sysctl(const json_map_string_string *sys for (i = 0; i < sysctl->len; i++) { char sysk[BUFSIZ] = { 0 }; - int nret = sprintf_s(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]); - if (nret < 0) { + int nret = snprintf(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]); + if (nret < 0 || (size_t)nret >= sizeof(sysk)) { ERROR("Failed to print string"); goto out_free; } diff --git a/src/error.c b/src/error.c index a2619cf..5011146 100644 --- a/src/error.c +++ b/src/error.c @@ -13,8 +13,9 @@ * Description: provide container error definition ******************************************************************************/ #include "utils.h" -#include "securec.h" #include "error.h" +#include +#include // record the lcr error __thread engine_error_t g_lcr_error = { @@ -58,7 +59,7 @@ void lcr_set_error_message(lcr_errno_t errcode, const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsprintf(errbuf, format, argp); va_end(argp); clear_error_message(&g_lcr_error); if (ret < 0) { @@ -79,7 +80,7 @@ void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...) return; } va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsprintf(errbuf, format, argp); va_end(argp); clear_error_message(&g_lcr_error); if (ret < 0) { @@ -99,7 +100,7 @@ void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsprintf(errbuf, format, argp); va_end(argp); if (ret < 0) { g_lcr_error.errcode = LCR_ERR_FORMAT; diff --git a/src/json/oci_runtime_hooks.c b/src/json/oci_runtime_hooks.c index 154a6e4..1138c16 100644 --- a/src/json/oci_runtime_hooks.c +++ b/src/json/oci_runtime_hooks.c @@ -16,9 +16,9 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif -#include #include "read_file.h" #include "oci_runtime_hooks.h" +#include #define PARSE_ERR_BUFFER_SIZE 1024 @@ -30,7 +30,6 @@ char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, co const unsigned char *gen_buf = NULL; char *json_buf = NULL; size_t gen_len = 0; - errno_t eret; if (ptr == NULL || err == NULL) { return NULL; @@ -67,11 +66,7 @@ char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, co *err = strdup("Out of memory"); goto free_out; } - eret = memcpy_s((void *)json_buf, gen_len + 1, (void *)gen_buf, gen_len); - if (eret != EOK) { - *err = strdup("Memcpy failed"); - goto free_out; - } + (void)memcpy((void *)json_buf, (void *)gen_buf, gen_len); json_buf[gen_len] = '\0'; free_out: diff --git a/src/json/schema/src/common_c.py b/src/json/schema/src/common_c.py index 78a90f2..316d9c7 100644 --- a/src/json/schema/src/common_c.py +++ b/src/json/schema/src/common_c.py @@ -33,8 +33,8 @@ yajl_gen_status map_uint(void *ctx, long long unsigned int num) { char numstr[MAX_NUM_STR_LEN]; int ret; - ret = sprintf_s(numstr, sizeof(numstr), "%llu", num); - if (ret < 0) { + ret = snprintf(numstr, sizeof(numstr), "%llu", num); + if (ret < 0 || (size_t)ret >= sizeof(numstr)) { return yajl_gen_in_error_state; } return yajl_gen_number((yajl_gen)ctx, (const char *)numstr, strlen(numstr)); @@ -44,8 +44,8 @@ yajl_gen_status map_int(void *ctx, long long int num) { char numstr[MAX_NUM_STR_LEN]; int ret; - ret = sprintf_s(numstr, sizeof(numstr), "%lld", num); - if (ret < 0) { + ret = snprintf(numstr, sizeof(numstr), "%lld", num); + if (ret < 0 || (size_t)ret >= sizeof(numstr)) { return yajl_gen_in_error_state; } return yajl_gen_number((yajl_gen)ctx, (const char *)numstr, strlen(numstr)); @@ -389,8 +389,8 @@ yajl_gen_status gen_json_map_int_int(void *ctx, const json_map_int_int *map, con for (i = 0; i < len; i++) { char numstr[MAX_NUM_STR_LEN]; int nret; - nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); - if (nret < 0) { + nret = snprintf(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0 || (size_t)nret >= sizeof(numstr)) { if (!*err && asprintf(err, "Error to print string") < 0) { *(err) = safe_strdup("error allocating memory"); } @@ -490,16 +490,8 @@ int append_json_map_int_int(json_map_int_int *map, int key, int val) { vals = safe_malloc(len * sizeof(int)); if (map->len) { - if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(int), map->values, map->len * sizeof(int)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(int)); + (void)memcpy(vals, map->values, map->len * sizeof(int)); } free(map->keys); map->keys = keys; @@ -530,8 +522,8 @@ yajl_gen_status gen_json_map_int_bool(void *ctx, const json_map_int_bool *map, c for (i = 0; i < len; i++) { char numstr[MAX_NUM_STR_LEN]; int nret; - nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); - if (nret < 0) { + nret = snprintf(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0 || (size_t)nret >= sizeof(numstr)) { if (!*err && asprintf(err, "Error to print string") < 0) { *(err) = safe_strdup("error allocating memory"); } @@ -631,16 +623,8 @@ int append_json_map_int_bool(json_map_int_bool *map, int key, bool val) { vals = safe_malloc(len * sizeof(bool)); if (map->len) { - if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(bool), map->values, map->len * sizeof(bool)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(int)); + (void)memcpy(vals, map->values, map->len * sizeof(bool)); } free(map->keys); map->keys = keys; @@ -671,8 +655,8 @@ yajl_gen_status gen_json_map_int_string(void *ctx, const json_map_int_string *ma for (i = 0; i < len; i++) { char numstr[MAX_NUM_STR_LEN]; int nret; - nret = sprintf_s(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); - if (nret < 0) { + nret = snprintf(numstr, sizeof(numstr), "%lld", (long long int)map->keys[i]); + if (nret < 0 || (size_t)nret >= sizeof(numstr)) { if (!*err && asprintf(err, "Error to print string") < 0) { *(err) = safe_strdup("error allocating memory"); } @@ -771,16 +755,8 @@ int append_json_map_int_string(json_map_int_string *map, int key, const char *va vals = safe_malloc(len * sizeof(char *)); if (map->len) { - if (memcpy_s(keys, len * sizeof(int), map->keys, map->len * sizeof(int)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(char *), map->values, map->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(int)); + (void)memcpy(vals, map->values, map->len * sizeof(char *)); } free(map->keys); map->keys = keys; @@ -897,16 +873,8 @@ int append_json_map_string_int(json_map_string_int *map, const char *key, int va vals = safe_malloc(len * sizeof(int)); if (map->len) { - if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(int), map->values, map->len * sizeof(int)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(char *)); + (void)memcpy(vals, map->values, map->len * sizeof(int)); } free(map->keys); map->keys = keys; @@ -1019,16 +987,8 @@ int append_json_map_string_bool(json_map_string_bool *map, const char *key, bool vals = safe_malloc(len * sizeof(bool)); if (map->len) { - if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(bool), map->values, map->len * sizeof(bool)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(char *)); + (void)memcpy(vals, map->values, map->len * sizeof(bool)); } free(map->keys); map->keys = keys; @@ -1148,16 +1108,8 @@ int append_json_map_string_string(json_map_string_string *map, const char *key, vals = safe_malloc(len * sizeof(char *)); if (map->len) { - if (memcpy_s(keys, len * sizeof(char *), map->keys, map->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(char *), map->values, map->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, map->keys, map->len * sizeof(char *)); + (void)memcpy(vals, map->values, map->len * sizeof(char *)); } free(map->keys); map->keys = keys; @@ -1205,12 +1157,7 @@ char *json_marshal_string(const char *str, size_t strlen, const struct parser_co } json_buf = safe_malloc(gen_len + 1); - if (memcpy_s(json_buf, gen_len + 1, gen_buf, gen_len) != EOK) { - *err = safe_strdup("Error to memcpy json"); - free(json_buf); - json_buf = NULL; - goto free_out; - } + (void)memcpy(json_buf, gen_buf, gen_len); json_buf[gen_len] = '\\0'; free_out: diff --git a/src/json/schema/src/common_h.py b/src/json/schema/src/common_h.py index c8084d9..cae6205 100644 --- a/src/json/schema/src/common_h.py +++ b/src/json/schema/src/common_h.py @@ -30,13 +30,13 @@ CODE = '''// Auto generated file. Do not edit! # ifndef _JSON_COMMON_H # define _JSON_COMMON_H +# include # include # include # include # include # include # include -# include "securec.h" # ifdef __cplusplus extern "C" { diff --git a/src/json/schema/src/read_file.c b/src/json/schema/src/read_file.c index e35de0c..a5e5610 100644 --- a/src/json/schema/src/read_file.c +++ b/src/json/schema/src/read_file.c @@ -23,7 +23,6 @@ #include #include -#include "securec.h" #include "read_file.h" #ifndef O_CLOEXEC @@ -57,8 +56,6 @@ char *fread_file(FILE *stream, size_t *length) while (1) { size_t ret, newsize, sizejudge; - int pret; - errno_t rc = EOK; sizejudge = (JSON_MAX_SIZE - BUFSIZ) - 1; if (sizejudge < off) { goto out; @@ -71,15 +68,9 @@ char *fread_file(FILE *stream, size_t *length) } if (buf != NULL) { - pret = memcpy_s(tmpbuf, newsize, buf, off); - if (pret) { - goto out; - } + (void)memcpy(tmpbuf, buf, off); - rc = memset_s(buf, off, 0, off); - if (rc != EOK) { - goto out; - } + (void)memset(buf, 0, off); free(buf); } diff --git a/src/json/schema/src/sources.py b/src/json/schema/src/sources.py index 4dbe196..49ef137 100644 --- a/src/json/schema/src/sources.py +++ b/src/json/schema/src/sources.py @@ -797,7 +797,6 @@ def src_reflect(structs, schema_info, c_file, root_typ): c_file.write("#endif\n") c_file.write('#include \n') c_file.write('#include \n') - c_file.write('#include "securec.h"\n') c_file.write('#include "%s"\n\n' % schema_info.header.basename) for i in structs: append_c_code(i, c_file, schema_info.prefix) @@ -982,12 +981,7 @@ yajl_gen_status gen_%s(yajl_gen g, const %s_element **ptr, size_t len, const str } json_buf = safe_malloc(gen_len + 1); - if (memcpy_s(json_buf, gen_len + 1, gen_buf, gen_len) != EOK) { - *err = safe_strdup("Error to memcpy json"); - free(json_buf); - json_buf = NULL; - goto free_out; - } + (void)memcpy(json_buf, gen_buf, gen_len); json_buf[gen_len] = '\\0'; free_out: diff --git a/src/lcrcontainer.c b/src/lcrcontainer.c index ceddce4..9ba7311 100644 --- a/src/lcrcontainer.c +++ b/src/lcrcontainer.c @@ -190,8 +190,8 @@ static bool create_container_dir(const struct lxc_container *c) goto out; } - nret = sprintf_s(s, length, "%s/%s", c->config_path, c->name); - if (nret < 0) { + nret = snprintf(s, length, "%s/%s", c->config_path, c->name); + if (nret < 0 || (size_t)nret >= length) { goto out; } // create container dir @@ -224,8 +224,8 @@ static bool remove_container_dir(const struct lxc_container *c) return false; } - ret = sprintf_s(s, length, "%s/%s", c->config_path, c->name); - if (ret < 0) { + ret = snprintf(s, length, "%s/%s", c->config_path, c->name); + if (ret < 0 || (size_t)ret >= length) { free(s); return false; } @@ -318,8 +318,8 @@ static bool lcr_save_ocihooks(const char *name, const char *lcrpath, const oci_r return false; } - nret = sprintf_s(ocihook, sizeof(ocihook), "%s/%s", bundle, OCIHOOKSFILE); - if (nret < 0) { + nret = snprintf(ocihook, sizeof(ocihook), "%s/%s", bundle, OCIHOOKSFILE); + if (nret < 0 || (size_t)nret >= sizeof(ocihook)) { ERROR("Failed to print string"); goto out_free; } @@ -374,8 +374,8 @@ static bool lcr_save_container(const char *name, const char *lcrpath, const oci_ goto out_free; } - nret = sprintf_s(ociconfig, sizeof(ociconfig), "%s/%s", bundle, OCICONFIGFILE); - if (nret < 0) { + nret = snprintf(ociconfig, sizeof(ociconfig), "%s/%s", bundle, OCICONFIGFILE); + if (nret < 0 || (size_t)nret >= sizeof(ociconfig)) { ERROR("Failed to print string"); goto out_free; } @@ -428,8 +428,8 @@ static bool mount_get_bundle_file(char **bundle, const char *container_name, con if (*bundle == NULL) { return false; } - nret = sprintf_s(*bundle, len, "%s/%s/%s", path, container_name, filename); - if (nret < 0) { + nret = snprintf(*bundle, len, "%s/%s/%s", path, container_name, filename); + if (nret < 0 || (size_t)nret >= len) { return false; } return true; @@ -446,8 +446,8 @@ static bool mount_file(oci_runtime_spec *container, const char *bundle, const ch int nret = 0; defs_mount *tmp_mounts = NULL; - nret = sprintf_s(dest, sizeof(dest), "%s/%s", targetdir, filename); - if (nret < 0) { + nret = snprintf(dest, sizeof(dest), "%s/%s", targetdir, filename); + if (nret < 0 || (size_t)nret >= sizeof(dest)) { ERROR("Failed to print string"); goto out_free; } @@ -616,9 +616,9 @@ static bool mount_hosts(oci_runtime_spec *container, const struct lxc_container goto out_free; } - nret = sprintf_s(content, content_len, "%s%s%s\n", default_config, loop_ip, container->hostname); - if (nret < 0) { - ERROR("Sprintf_s failed"); + nret = snprintf(content, content_len, "%s%s%s\n", default_config, loop_ip, container->hostname); + if (nret < 0 || (size_t)nret >= content_len) { + ERROR("Snprintf failed"); goto out_free; } /* 4.get file path for hosts */ @@ -662,8 +662,8 @@ static bool copy_host_file_to_bundle(const struct lxc_container *c, const char * bool ret = true; int nret; - nret = sprintf_s(full_path, sizeof(full_path), "%s%s%s", rootfs, "/etc/", filename); - if (nret < 0) { + nret = snprintf(full_path, sizeof(full_path), "%s%s%s", rootfs, "/etc/", filename); + if (nret < 0 || (size_t)nret >= sizeof(full_path)) { goto out_free; } @@ -819,8 +819,8 @@ static int create_partial(const struct lxc_container *c) return -1; } - ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name); - if (ret < 0) { + ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); + if (ret < 0 || (size_t)ret >= len) { ERROR("Error writing partial pathname"); goto out_free; } @@ -866,8 +866,8 @@ static void remove_partial(const struct lxc_container *c) return; } - ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name); - if (ret < 0) { + ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name); + if (ret < 0 || (size_t)ret >= len) { ERROR("Error writing partial pathname"); goto out_free; } @@ -1007,8 +1007,8 @@ static bool lcr_start_check_config(const char *lcrpath, const char *name) return false; } - nret = sprintf_s(config, sizeof(config), "%s/%s/config", lcrpath, name); - if (nret < 0) { + nret = snprintf(config, sizeof(config), "%s/%s/config", lcrpath, name); + if (nret < 0 || (size_t)nret >= sizeof(config)) { SYSERROR("Failed to allocated memory"); return false; } @@ -1059,8 +1059,8 @@ static int save_container_config_file(const char *rootpath, const char *id, cons if (json_data == NULL || strlen(json_data) == 0) { goto out; } - nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, fname); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, id, fname); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); ret = -1; goto out; diff --git a/src/lcrcontainer_execute.c b/src/lcrcontainer_execute.c index ec8a5cc..08413d5 100644 --- a/src/lcrcontainer_execute.c +++ b/src/lcrcontainer_execute.c @@ -29,7 +29,6 @@ #include "utils.h" #include "log.h" #include "error.h" -#include "securec.h" #include "oci_runtime_spec.h" // Cgroup Item Definition @@ -188,7 +187,8 @@ static int update_resources_cpu_shares(struct lxc_container *c, const struct lcr char numstr[128] = { 0 }; /* max buffer */ if (cr->cpu_shares != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_shares)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_shares)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -210,7 +210,8 @@ static int update_resources_cpu_period(struct lxc_container *c, const struct lcr char numstr[128] = { 0 }; /* max buffer */ if (cr->cpu_period != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_period)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_period)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -232,7 +233,8 @@ static int update_resources_cpu_quota(struct lxc_container *c, const struct lcr_ char numstr[128] = { 0 }; /* max buffer */ if (cr->cpu_quota != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_quota)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_quota)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -283,7 +285,8 @@ static int update_resources_memory_limit(struct lxc_container *c, const struct l char numstr[128] = { 0 }; /* max buffer */ if (cr->memory_limit != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_limit)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_limit)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -305,7 +308,8 @@ static int update_resources_memory_swap(struct lxc_container *c, const struct lc char numstr[128] = { 0 }; /* max buffer */ if (cr->memory_swap != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_swap)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_swap)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -327,7 +331,8 @@ static int update_resources_memory_reservation(struct lxc_container *c, const st char numstr[128] = { 0 }; /* max buffer */ if (cr->memory_reservation != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_reservation)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_reservation)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -370,7 +375,8 @@ static int update_resources_blkio_weight(struct lxc_container *c, const struct l char numstr[128] = { 0 }; /* max buffer */ if (cr->blkio_weight != 0) { - if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->blkio_weight)) < 0) { + int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->blkio_weight)); + if (num < 0 || (size_t)num >= sizeof(numstr)) { ret = -1; goto out; } @@ -742,7 +748,6 @@ static void stat_get_blk_stats(struct lxc_container *c, const char *item, struct size_t len = 0; char **lines = NULL; char **cols = NULL; - errno_t nret = 0; len = (size_t)c->get_cgroup_item(c, item, buf, sizeof(buf)); if (len == 0 || len >= sizeof(buf)) { @@ -755,10 +760,7 @@ static void stat_get_blk_stats(struct lxc_container *c, const char *item, struct return; } - nret = memset_s(stats, sizeof(struct blkio_stats), 0, sizeof(struct blkio_stats)); - if (nret != EOK) { - goto err_out; - } + (void)memset(stats, 0, sizeof(struct blkio_stats)); for (i = 0; lines[i]; i++) { cols = lcr_string_split_and_trim(lines[i], ' '); @@ -849,9 +851,7 @@ void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs) const char *state = NULL; clear_error_message(&g_lcr_error); - if (memset_s(lcs, sizeof(struct lcr_container_state), 0x00, sizeof(struct lcr_container_state)) != EOK) { - ERROR("Can not set memory"); - } + (void)memset(lcs, 0x00, sizeof(struct lcr_container_state)); lcs->name = util_strdup_s(c->name); @@ -929,7 +929,8 @@ static void execute_lxc_attach(const char *name, const char *path, const struct if (request->timeout != 0) { char timeout_str[LCR_NUMSTRLEN64] = { 0 }; add_array_elem(params, args_len, &i, "--timeout"); - if (sprintf_s(timeout_str, LCR_NUMSTRLEN64, "%lld", (long long)request->timeout) < 0) { + int num = snprintf(timeout_str, LCR_NUMSTRLEN64, "%lld", (long long)request->timeout); + if (num < 0 || num >= LCR_NUMSTRLEN64) { COMMAND_ERROR("Invaild attach timeout value :%lld", (long long)request->timeout); free(params); exit(EXIT_FAILURE); @@ -1069,7 +1070,8 @@ void execute_lxc_start(const char *name, const char *path, const struct lcr_star if (request->start_timeout != 0) { char start_timeout_str[LCR_NUMSTRLEN64] = { 0 }; add_array_elem(params, PARAM_NUM, &i, "--start-timeout"); - if (sprintf_s(start_timeout_str, LCR_NUMSTRLEN64, "%u", request->start_timeout) < 0) { + int num = snprintf(start_timeout_str, LCR_NUMSTRLEN64, "%u", request->start_timeout); + if (num < 0 || num >= LCR_NUMSTRLEN64) { COMMAND_ERROR("Invaild start timeout value: %u", request->start_timeout); exit(EXIT_FAILURE); } diff --git a/src/lcrcontainer_extend.c b/src/lcrcontainer_extend.c index 6a52207..bccb31f 100644 --- a/src/lcrcontainer_extend.c +++ b/src/lcrcontainer_extend.c @@ -127,8 +127,8 @@ static int make_annotations(oci_runtime_spec *container, const struct lxc_contai } if (!anno->values[fpos]) { - nret = sprintf_s(default_path, PATH_MAX, "%s/%s/%s", c->config_path, c->name, "console.log"); - if (nret < 0) { + nret = snprintf(default_path, PATH_MAX, "%s/%s/%s", c->config_path, c->name, "console.log"); + if (nret < 0 || nret >= PATH_MAX) { ERROR("create default path: %s failed", default_path); goto out; } @@ -373,8 +373,8 @@ static int lcr_spec_write_seccomp_line(int fd, const char *seccomp) goto cleanup; } - nret = sprintf_s(line, len, "%s = %s", "lxc.seccomp.profile", seccomp); - if (nret < 0) { + nret = snprintf(line, len, "%s = %s", "lxc.seccomp.profile", seccomp); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf failed"); goto cleanup; } @@ -400,8 +400,8 @@ static char *lcr_save_seccomp_file(const char *bundle, const char *seccomp_conf) int nret; ssize_t written_cnt; - nret = sprintf_s(seccomp, sizeof(seccomp), "%s/seccomp", bundle); - if (nret < 0) { + nret = snprintf(seccomp, sizeof(seccomp), "%s/seccomp", bundle); + if (nret < 0 || (size_t)nret >= sizeof(seccomp)) { goto cleanup; } @@ -733,8 +733,8 @@ static int lcr_open_config_file(const char *bundle) int fd = -1; int nret; - nret = sprintf_s(config, sizeof(config), "%s/config", bundle); - if (nret < 0) { + nret = snprintf(config, sizeof(config), "%s/config", bundle); + if (nret < 0 || (size_t)nret >= sizeof(config)) { goto out; } @@ -776,8 +776,8 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf) goto cleanup; } - nret = sprintf_s(line, len, "%s = %s", item->name, item->value); - if (nret < 0) { + nret = snprintf(line, len, "%s = %s", item->name, item->value); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf failed"); goto cleanup; } @@ -818,8 +818,8 @@ char *lcr_get_bundle(const char *lcrpath, const char *name) goto cleanup; } - nret = sprintf_s(bundle, len, "%s/%s", lcrpath, name); - if (nret < 0) { + nret = snprintf(bundle, len, "%s/%s", lcrpath, name); + if (nret < 0 || (size_t)nret >= len) { ERROR("Print bundle string failed"); goto cleanup; } diff --git a/src/log.c b/src/log.c index 2ba254f..21b5bdc 100644 --- a/src/log.c +++ b/src/log.c @@ -25,7 +25,6 @@ #include #include -#include "securec.h" #include "log.h" #include "utils.h" @@ -234,11 +233,11 @@ int engine_log_append(const struct engine_log_event *event, const char *format, struct timespec timestamp; va_start(args, format); - rc = vsprintf_s(msg, MAX_MSG_LENGTH, format, args); + rc = vsprintf(msg, format, args); va_end(args); - if (rc < 0 || rc >= MAX_MSG_LENGTH) { - rc = sprintf_s(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!"); - if (rc < 0) { + if (rc < 0) { + rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!"); + if (rc < 0 || rc >= MAX_MSG_LENGTH) { return 0; } } @@ -296,17 +295,17 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) { tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH); } - nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "", - timestamp, g_engine_log_prio_name[event->priority], - g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func, - event->locinfo->line, msg); + nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "", + timestamp, g_engine_log_prio_name[event->priority], + g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func, + event->locinfo->line, msg); if (nret < 0) { - nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", - tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority], - g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, - event->locinfo->func, event->locinfo->line, "Large log message"); - if (nret < 0) { + nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", + tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority], + g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, + event->locinfo->func, event->locinfo->line, "Large log message"); + if (nret < 0 || (size_t)nret >= sizeof(log_buffer)) { return; } } @@ -400,14 +399,14 @@ int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *t /* Calculate the real seconds */ real_seconds = (((time->tv_sec - trans_to_sec) - hours_to_sec) - (real_minutes * 60)); - ret = sprintf_s(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec); + ret = snprintf(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec); if (ret < 0 || ret >= LCR_NUMSTRLEN64) { return -1; } /* Create the final timestamp */ - ret = sprintf_s(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s", - real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns); + ret = snprintf(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s", + real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns); if (ret < 0 || (size_t)ret >= bufsize) { return -1; } diff --git a/src/utils.c b/src/utils.c index b55b5ad..343c7ad 100644 --- a/src/utils.c +++ b/src/utils.c @@ -26,7 +26,6 @@ #include #include "constants.h" #include "utils.h" -#include "securec.h" #include "log.h" #define ISSLASH(C) ((C) == '/') @@ -55,7 +54,6 @@ static int do_clean_path(const char *respath, const char *limit_respath, const c { char *dest = *dst; const char *endpos = stpos; - errno_t ret; for (; *stpos; stpos = endpos) { while (ISSLASH(*stpos)) { @@ -84,11 +82,7 @@ static int do_clean_path(const char *respath, const char *limit_respath, const c return -1; } - ret = memcpy_s(dest, (size_t)(endpos - stpos), stpos, (size_t)(endpos - stpos)); - if (ret != EOK) { - ERROR("Failed at cleanpath memcpy"); - return -1; - } + (void)memcpy(dest, stpos, (size_t)(endpos - stpos)); dest += endpos - stpos; *dest = '\0'; } @@ -102,7 +96,6 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len) char *dest = NULL; const char *stpos = NULL; const char *limit_respath = NULL; - errno_t ret; if (path == NULL || path[0] == '\0' || realpath == NULL || (realpath_len < PATH_MAX)) { return NULL; @@ -110,11 +103,7 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len) respath = realpath; - ret = memset_s(respath, realpath_len, 0, realpath_len); - if (ret != EOK) { - ERROR("Failed at cleanpath memset"); - goto error; - } + (void)memset(respath, 0, realpath_len); limit_respath = respath + PATH_MAX; if (!IS_ABSOLUTE_FILE_NAME(path)) { @@ -128,11 +117,11 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len) ERROR("Failed to get the end of respath"); goto error; } - ret = strcat_s(respath, PATH_MAX, path); - if (ret != EOK) { - ERROR("Failed at cleanpath strcat"); + if (strlen(path) >= (PATH_MAX - 1) - strlen(respath)) { + ERROR("%s path too long", path); goto error; } + (void)strcat(respath, path); stpos = path; } else { dest = respath; @@ -235,21 +224,11 @@ static char *do_string_join(const char *sep, const char **parts, size_t parts_le if (res_string == NULL) { return NULL; } - for (iter = 0; iter < parts_len - 1; iter++) { - if (strcat_s(res_string, result_len + 1, parts[iter]) != EOK) { - free(res_string); - return NULL; - } - if (strcat_s(res_string, result_len + 1, sep) != EOK) { - free(res_string); - return NULL; - } - } - if (strcat_s(res_string, result_len + 1, parts[parts_len - 1]) != EOK) { - free(res_string); - return NULL; + (void)strcat(res_string, parts[iter]); + (void)strcat(res_string, sep); } + (void)strcat(res_string, parts[parts_len - 1]); return res_string; } @@ -436,12 +415,7 @@ int lcr_grow_array(void ***orig_array, size_t *orig_capacity, size_t size, size_ return -1; } if (*orig_array) { - if (memcpy_s(add_array, add_capacity * sizeof(void *), *orig_array, *orig_capacity * sizeof(void *)) != - EOK) { - ERROR("Failed to memcpy memory"); - free(add_array); - return -1; - } + (void)memcpy(add_array, *orig_array, *orig_capacity * sizeof(void *)); free((void *)*orig_array); } @@ -477,7 +451,6 @@ void *util_common_calloc_s(size_t size) int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize) { - int nret = 0; void *addr = NULL; if (newptr == NULL) { @@ -494,11 +467,7 @@ int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize) } if (oldptr != NULL) { - nret = memcpy_s(addr, newsize, oldptr, oldsize); - if (nret != EOK) { - free(addr); - goto err_out; - } + (void)memcpy(addr, oldptr, oldsize); free(oldptr); } @@ -791,8 +760,8 @@ static void util_rmdir_one(const char *dirpath, const struct dirent *pdirent, in return; } - nret = sprintf_s(fname, PATH_MAX, "%s/%s", dirpath, pdirent->d_name); - if (nret < 0) { + nret = snprintf(fname, PATH_MAX, "%s/%s", dirpath, pdirent->d_name); + if (nret < 0 || nret >= PATH_MAX) { ERROR("Pathname too long"); *failure = 1; return; @@ -878,27 +847,18 @@ static ssize_t util_string_replace_one(const char *needle, const char *replace, for (next_p = (char *)haystack, p = strstr(next_p, needle); p != NULL; next_p = p, p = strstr(next_p, needle)) { part_len = (size_t)(p - next_p); if ((res_string != NULL) && part_len > 0) { - if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) { - ERROR("Failed to copy memory!"); - return -1; - } + (void)memcpy(&res_string[length], next_p, part_len); } length += (ssize_t)part_len; if ((res_string != NULL) && replace_len > 0) { - if (memcpy_s(&res_string[length], replace_len, replace, replace_len) != EOK) { - ERROR("Failed to copy memory!"); - return -1; - } + (void)memcpy(&res_string[length], replace, replace_len); } length += (ssize_t)replace_len; p += nl_len; } part_len = strlen(next_p); if ((res_string != NULL) && part_len > 0) { - if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) { - ERROR("Failed to copy memory!"); - return -1; - } + (void)memcpy(&res_string[length], next_p, part_len); } length += (ssize_t)part_len; return length; @@ -1117,14 +1077,8 @@ char *util_string_append(const char *post, const char *pre) if (res_string == NULL) { return NULL; } - if (strcat_s(res_string, length, pre) != EOK) { - free(res_string); - return NULL; - } - if (strcat_s(res_string, length, post) != EOK) { - free(res_string); - return NULL; - } + (void)strcat(res_string, pre); + (void)strcat(res_string, post); return res_string; } @@ -1152,11 +1106,7 @@ char *util_string_split_prefix(size_t prefix_len, const char *file) if (path == NULL) { return NULL; } - if (strncpy_s(path, len + 1, file + prefix_len, len) != EOK) { - ERROR("Failed to copy string!"); - free(path); - return NULL; - } + (void)strncpy(path, file + prefix_len, len); path[len] = '\0'; return path; @@ -1374,11 +1324,7 @@ int util_array_append(char ***array, const char *element) return -1; } if (*array) { - if (memcpy_s(new_array, (len + 2) * sizeof(char *), *array, len * sizeof(char *)) != EOK) { - ERROR("Failed to memcpy memory"); - free(new_array); - return -1; - } + (void)memcpy(new_array, *array, len * sizeof(char *)); free((void *)*array); } *array = new_array; @@ -1603,7 +1549,8 @@ static int append_new_content_to_file(FILE *fp, const char *content) ret = -1; goto out; } - if (sprintf_s(tmp_str, content_len, "%s\n", content) < 0) { + int num = snprintf(tmp_str, content_len, "%s\n", content); + if (num < 0 || (size_t)num >= content_len) { ERROR("Failed to print string"); ret = -1; goto out; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index 4320971..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -project(lcr_LLT) - -# setup testing -find_package(Threads REQUIRED) -find_package(GTest REQUIRED) - -add_subdirectory(utils) diff --git a/test/include/mock.h b/test/include/mock.h deleted file mode 100644 index d77cd3c..0000000 --- a/test/include/mock.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved. - * lcr licensed under the Mulan PSL v1. - * You can use this software according to the terms and conditions of the Mulan PSL v1. - * You may obtain a copy of Mulan PSL v1 at: - * http://license.coscl.org.cn/MulanPSL - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR - * PURPOSE. - * See the Mulan PSL v1 for more details. - * Description: define mock method - * Author: wangyushui - * Create: 2019-6-10 - */ - -#ifndef MOCK_H -#define MOCK_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define MOCK_STRUCT_INIT(...) \ - { __VA_ARGS__ } - -#define DEFINE_RETURN_MOCK(fn, ret) \ - bool ut_ ## fn ## _mocked = false; \ - ret ut_ ## fn - -#define DEFINE_RETURN_MOCK_V(fn, ret, dargs) \ - bool ut_ ## fn ## _mocked = false; \ - ret(* ut_ ## fn) dargs -/* - * For controlling mocked function behavior, setting - * and getting values from the stub, the _P macros are - * for mocking functions that return pointer values. - */ -#define MOCK_SET(fn, val) \ - ut_ ## fn ## _mocked = true; \ - ut_ ## fn = val - -#define MOCK_SET_V(fn, fun) \ - ut_ ## fn ## _mocked = true; \ - ut_ ## fn = fun - -#define MOCK_GET(fn) \ - ut_ ## fn - -#define MOCK_GET_V(fn, args) \ - ut_ ## fn args - -#define MOCK_CLEAR(fn) \ - ut_ ## fn ## _mocked = false; - -#define MOCK_CLEAR_P(fn) \ - ut_ ## fn ## _mocked = false; \ - ut_ ## fn = NULL; - -/* for declaring function protoypes for wrappers */ -#define DECLARE_WRAPPER(fn, ret, args) \ - extern bool ut_ ## fn ## _mocked; \ - extern ret ut_ ## fn; \ - ret __wrap_ ## fn args; \ - ret __real_ ## fn args; - -#define DECLARE_WRAPPER_V(fn, ret, args) \ - extern bool ut_ ## fn ## _mocked; \ - extern ret(* ut_ ## fn) args; \ - ret __wrap_ ## fn args; \ - ret __real_ ## fn args; - -/* for defining the implmentation of wrappers for syscalls */ -#define DEFINE_WRAPPER(fn, ret, dargs, pargs) \ - DEFINE_RETURN_MOCK(fn, ret); \ - ret __wrap_ ## fn dargs \ - { \ - if (!ut_ ## fn ## _mocked) { \ - return __real_ ## fn pargs; \ - } else { \ - return MOCK_GET(fn); \ - } \ - } - -#define DEFINE_WRAPPER_V(fn, ret, dargs, pargs) \ - DEFINE_RETURN_MOCK_V(fn, ret, dargs); \ - __attribute__((used)) ret __wrap_ ## fn dargs \ - { \ - if (!ut_ ## fn ## _mocked) { \ - return __real_ ## fn pargs; \ - } else { \ - return MOCK_GET_V(fn, pargs); \ - } \ - } - -/* DEFINE_STUB is for defining the implmentation of stubs for funcs. */ -#define DEFINE_STUB(fn, ret, dargs, val) \ - bool ut_ ## fn ## _mocked = true; \ - ret ut_ ## fn = val; \ - ret fn dargs; \ - ret fn dargs \ - { \ - return MOCK_GET(fn); \ - } - -/* DEFINE_STUB_V macro is for stubs that don't have a return value */ -#define DEFINE_STUB_V(fn, dargs) \ - void fn dargs; \ - void fn dargs \ - { \ - } - -#define HANDLE_RETURN_MOCK(fn) \ - if (ut_ ## fn ## _mocked) { \ - return ut_ ## fn; \ - } - -#ifdef __cplusplus -} -#endif - -#endif /* MOCK_H */ - - - diff --git a/test/llt.sh b/test/llt.sh deleted file mode 100644 index 9a2f3c5..0000000 --- a/test/llt.sh +++ /dev/null @@ -1,281 +0,0 @@ -####################################################################### -##- @Copyright (C) Huawei Technologies., Ltd. 2019. All rights reserved. -# - lcr licensed under the Mulan PSL v1. -# - You can use this software according to the terms and conditions of the Mulan PSL v1. -# - You may obtain a copy of Mulan PSL v1 at: -# - http://license.coscl.org.cn/MulanPSL -# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR -# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR -# - PURPOSE. -# - See the Mulan PSL v1 for more details. -##- @Description: generate cetification -##- @Author: wujing -##- @Create: 2019-04-25 -####################################################################### -#! /bin/bash - -#set -xe - -usage() -{ - echo "Usage: sh llt.sh [OPTIONS]" - echo "Use llt.sh to control llt operation" - echo "" - echo "Misc:" - echo " -h, --help Print this help, then exit" - echo - echo "Compile Options:" - echo " -m, --cmake