From ee41558c0cd97c218984914c4a4844c247ca58fa Mon Sep 17 00:00:00 2001 From: openeuler-iSula Date: Sun, 29 Dec 2019 15:59:28 +0800 Subject: [PATCH] iSUlad: remove seucrec new Signed-off-by: openeuler-iSula --- cmake/checker.cmake | 25 ++-- cmake/options.cmake | 17 ++- cmake/protoc.cmake | 97 ++++++++----- cmake/set_build_flags.cmake | 2 +- iSulad.spec | 8 +- src/cmd/commander.c | 6 +- src/cmd/lcrc/arguments.c | 11 +- src/cmd/lcrc/base/create.c | 5 +- src/cmd/lcrc/base/kill.c | 1 - src/cmd/lcrc/base/rename.c | 1 - src/cmd/lcrc/base/restart.c | 1 - src/cmd/lcrc/base/rm.c | 1 - src/cmd/lcrc/base/start.c | 1 - src/cmd/lcrc/base/stop.c | 1 - src/cmd/lcrc/commands.c | 11 +- src/cmd/lcrc/extend/events.c | 1 - src/cmd/lcrc/extend/export.c | 13 +- src/cmd/lcrc/extend/pause.c | 1 - src/cmd/lcrc/extend/resume.c | 1 - src/cmd/lcrc/extend/stats.c | 25 ++-- src/cmd/lcrc/extend/update.c | 1 - src/cmd/lcrc/images/images.c | 11 +- src/cmd/lcrc/images/load.c | 5 +- src/cmd/lcrc/images/login.c | 9 +- src/cmd/lcrc/images/logout.c | 1 - src/cmd/lcrc/images/pull.c | 1 - src/cmd/lcrc/images/rmi.c | 1 - src/cmd/lcrc/information/health.c | 1 - src/cmd/lcrc/information/info.c | 1 - src/cmd/lcrc/information/inspect.c | 9 +- src/cmd/lcrc/information/logs.c | 1 - src/cmd/lcrc/information/ps.c | 60 ++++---- src/cmd/lcrc/information/top.c | 1 - src/cmd/lcrc/information/version.c | 1 - src/cmd/lcrc/information/wait.c | 1 - src/cmd/lcrc/stream/attach.c | 1 - src/cmd/lcrc/stream/cp.c | 5 +- src/cmd/lcrc/stream/exec.c | 1 - src/cmd/lcrd/commands.c | 24 ++-- src/cmd/lcrd/main.c | 27 ++-- src/config/lcrd_config.c | 43 +++--- .../client/grpc/grpc_containers_client.cc | 18 +-- src/connect/client/grpc/grpc_images_client.cc | 1 - .../client/grpc/grpc_isula_image_client.cc | 6 + src/connect/client/isula_image_connect.c | 8 +- src/connect/client/lcrc_connect.c | 7 +- .../client/rest/rest_containers_client.c | 6 +- src/connect/client/rest/rest_images_client.c | 1 - .../service/grpc/grpc_containers_service.cc | 7 +- .../service/grpc/grpc_images_service.cc | 1 - .../service/rest/rest_containers_service.c | 1 - .../service/rest/rest_images_service.c | 1 - src/connect/service/rest/rest_service.c | 2 +- .../service/rest/rest_service_common.c | 3 +- src/console/console.c | 11 +- src/contrib/docker | 0 src/contrib/env_checkconfig | 0 src/contrib/sysmonitor/isulad-check.sh | 0 src/cutils/utils.c | 77 ++++------ src/cutils/utils_array.c | 21 +-- src/cutils/utils_file.c | 42 +++--- src/cutils/utils_string.c | 38 ++--- src/cutils/utils_verify.c | 13 +- src/engines/engine.c | 10 +- src/engines/lcr/lcr_engine.c | 19 +-- src/error.h | 6 +- src/http/buffer.c | 26 +--- src/http/certificate.c | 2 +- src/http/http.c | 16 +-- src/http/parser.c | 15 +- src/http/rest_common.c | 16 +-- src/image/embedded/db/db_all.c | 2 +- src/image/embedded/db/sqlite_common.c | 5 +- src/image/embedded/embedded_config_merge.c | 15 +- src/image/embedded/embedded_image.c | 1 - src/image/embedded/lim.c | 5 +- src/image/embedded/load.c | 13 +- src/image/embedded/snapshot/embedded.c | 1 - src/image/external/ext_image.c | 1 - src/image/image.c | 5 +- src/image/oci/global_config.c | 4 +- src/image/oci/isula_image_pull.c | 14 +- src/image/oci/isula_images_list.c | 24 +++- src/image/oci/oci_auth.c | 1 - src/image/oci/oci_common_operators.c | 14 +- src/image/oci/oci_config_merge.c | 1 - src/image/oci/run_image_server.c | 4 +- .../schema/schema/container/config-v2.json | 3 + src/json/schema/src/common_c.py | 99 +++---------- src/json/schema/src/common_h.py | 2 +- src/json/schema/src/read_file.c | 14 +- src/json/schema/src/read_file.h | 1 - src/json/schema/src/sources.py | 8 +- src/liblcrc.c | 1 - src/liblcrd.c | 14 +- src/log.c | 27 ++-- src/log.h | 1 + src/pack_config.c | 4 +- src/path.c | 75 +++------- src/plugin/plugin.c | 43 +++--- src/runtime/lcr/lcr_rt_ops.c | 21 +-- src/runtime/runtime.c | 1 - src/services/callback.c | 1 - src/services/cri/checkpoint_handler.cc | 1 - src/services/cri/cni_network_plugin.cc | 1 - src/services/cri/cri_container.cc | 2 + src/services/cri/cri_helpers.cc | 17 +-- src/services/cri/cri_runtime_service.cc | 4 +- src/services/cri/errors.cc | 6 +- src/services/cri/request_cache.cc | 4 + src/services/execution/events/collector.c | 13 +- .../execution/events/events_handler.c | 1 - src/services/execution/execute/execution.c | 51 ++++--- .../execution/execute/execution_create.c | 113 +++++++++++++-- .../execution/execute/execution_extend.c | 1 - .../execution/execute/execution_information.c | 131 ++---------------- .../execution/execute/execution_network.c | 30 ++-- .../execution/execute/execution_stream.c | 34 ++--- src/services/execution/log_gather.c | 12 +- .../execution/manager/container_unix.c | 32 +++-- .../execution/manager/container_unix.h | 5 +- .../execution/manager/containers_gc.c | 8 +- src/services/execution/manager/health_check.c | 19 +-- src/services/execution/manager/monitord.c | 5 +- .../execution/manager/restartmanager.c | 1 - src/services/execution/manager/restore.c | 33 +++-- src/services/execution/manager/supervisor.c | 9 +- src/services/execution/spec/specs.c | 22 +-- src/services/execution/spec/specs_extend.c | 15 +- src/services/execution/spec/specs_mount.c | 23 +-- src/services/execution/spec/specs_security.c | 9 +- src/services/execution/spec/sysinfo.c | 34 ++--- src/services/graphdriver/driver.c | 4 +- src/services/image/image_cb.c | 1 - src/sha256/sha256.c | 1 - src/sysctl_tools.c | 13 +- src/tar/lcrdtar.c | 13 +- src/types_def.c | 48 +++---- src/websocket/service/ws_server.cc | 28 +--- test/llt.sh | 0 tools/static_check | 0 update-version.bash | 0 142 files changed, 829 insertions(+), 1148 deletions(-) mode change 100644 => 100755 src/contrib/docker mode change 100644 => 100755 src/contrib/env_checkconfig mode change 100644 => 100755 src/contrib/sysmonitor/isulad-check.sh mode change 100644 => 100755 test/llt.sh mode change 100644 => 100755 tools/static_check mode change 100644 => 100755 update-version.bash diff --git a/cmake/checker.cmake b/cmake/checker.cmake index fa17cb0..a570418 100644 --- a/cmake/checker.cmake +++ b/cmake/checker.cmake @@ -47,12 +47,6 @@ find_library(ZLIB_LIBRARY z HINTS ${PC_ZLIB_LIBDIR} ${PC_ZLIB_LIBRARY_DIRS}) _CHECK(ZLIB_LIBRARY "ZLIB_LIBRARY-NOTFOUND" "libz.so") -# 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 libyajl pkg_check_modules(PC_LIBYAJL REQUIRED "yajl>=2") find_path(LIBYAJL_INCLUDE_DIR yajl/yajl_tree.h @@ -80,13 +74,7 @@ if (OPENSSL_VERIFY) _CHECK(OPENSSL_INCLUDE_DIR "OPENSSL_INCLUDE_DIR-NOTFOUND" "openssl/x509.h") endif() -if (GRPC_CONNECTOR) - # check websocket - find_path(WEBSOCKET_INCLUDE_DIR libwebsockets.h) - _CHECK(WEBSOCKET_INCLUDE_DIR "WEBSOCKET_INCLUDE_DIR-NOTFOUND" libwebsockets.h) - find_library(WEBSOCKET_LIBRARY websockets) - _CHECK(WEBSOCKET_LIBRARY "WEBSOCKET_LIBRARY-NOTFOUND" "libwebsockets.so") - +if (GRPC_CONNECTOR OR ENABLE_OCI_IMAGE) # check protobuf pkg_check_modules(PC_PROTOBUF "protobuf>=3.1.0") find_library(PROTOBUF_LIBRARY protobuf @@ -110,6 +98,15 @@ if (GRPC_CONNECTOR) find_library(GPR_LIBRARY gpr) _CHECK(GPR_LIBRARY "GPR_LIBRARY-NOTFOUND" "libgpr.so") +endif() + +if (GRPC_CONNECTOR) + # check websocket + find_path(WEBSOCKET_INCLUDE_DIR libwebsockets.h) + _CHECK(WEBSOCKET_INCLUDE_DIR "WEBSOCKET_INCLUDE_DIR-NOTFOUND" libwebsockets.h) + find_library(WEBSOCKET_LIBRARY websockets) + _CHECK(WEBSOCKET_LIBRARY "WEBSOCKET_LIBRARY-NOTFOUND" "libwebsockets.so") + # check clibcni pkg_check_modules(PC_CLIBCNI REQUIRED "clibcni") find_path(CLIBCNI_INCLUDE_DIR clibcni/api.h @@ -136,7 +133,7 @@ else() _CHECK(EVHTP_LIBRARY "EVHTP_LIBRARY-NOTFOUND" "libevhtp.so") endif() -if (ENABLE_EMBEDDED) +if (ENABLE_EMBEDDED_IMAGE) pkg_check_modules(PC_SQLITE3 "sqlite3>=3.7.17") find_path(SQLIT3_INCLUDE_DIR sqlite3.h HINTS ${PC_SQLITE3_INCLUDEDIR} ${PC_SQLITE3_INCLUDE_DIRS}) diff --git a/cmake/options.cmake b/cmake/options.cmake index c9dfa3a..f03bc84 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -33,7 +33,7 @@ endif() option(VERSION "set lcrd version" ON) if (VERSION STREQUAL "ON") - set(LCRD_VERSION "1.1.2") + set(LCRD_VERSION "1.1.4") endif() option(DEBUG "set lcrd gcc option" ON) @@ -45,3 +45,18 @@ option(GCOV "set lcrd gcov option" OFF) if (GCOV STREQUAL "ON") set(ISULAD_GCOV "ON") endif() + +# set OCI image server type +option(DISABLE_OCI "disable oci image" OFF) +if (DISABLE_OCI STREQUAL "ON") + message("Disable OCI image") +else() + add_definitions(-DENABLE_OCI_IMAGE=1) + set(ENABLE_OCI_IMAGE 2) +endif() + +option(ENABLE_EMBEDDED "enable embedded image" OFF) +if (ENABLE_EMBEDDED STREQUAL "ON") + add_definitions(-DENABLE_EMBEDDED_IMAGE=1) + set(ENABLE_EMBEDDED_IMAGE 1) +endif() diff --git a/cmake/protoc.cmake b/cmake/protoc.cmake index a5675ca..8dcaa17 100644 --- a/cmake/protoc.cmake +++ b/cmake/protoc.cmake @@ -6,11 +6,9 @@ set(TYPES_PROTOS_OUT_PATH ${GRPC_OUT_PRE_PATH}/src/api/types) set(CONTAINER_PROTOS_OUT_PATH ${GRPC_OUT_PRE_PATH}/src/api/services/containers) set(IMAGE_PROTOS_OUT_PATH ${GRPC_OUT_PRE_PATH}/src/api/services/images) set(CRI_PROTOS_OUT_PATH ${GRPC_OUT_PRE_PATH}/src/api/services/cri) +set(IMAGE_SERVICE_PROTOS_OUT_PATH ${GRPC_OUT_PRE_PATH}/src/api/image_client) execute_process(COMMAND mkdir -p ${TYPES_PROTOS_OUT_PATH}) -execute_process(COMMAND mkdir -p ${CONTAINER_PROTOS_OUT_PATH}) -execute_process(COMMAND mkdir -p ${IMAGE_PROTOS_OUT_PATH}) -execute_process(COMMAND mkdir -p ${CRI_PROTOS_OUT_PATH}) execute_process(COMMAND ${CMD_PROTOC} -I ${TYPES_PROTOS_PATH} --cpp_out=${TYPES_PROTOS_OUT_PATH} ${TYPES_PROTOS_PATH}/descriptor.proto ERROR_VARIABLE types_err) @@ -19,44 +17,71 @@ if (types_err) message(FATAL_ERROR ${types_err}) endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/containers --cpp_out=${CONTAINER_PROTOS_OUT_PATH} - ${PROTOS_PATH}/containers/container.proto ERROR_VARIABLE containers_err) -if (containers_err) - message("Parse containers.proto failed: ") - message(FATAL_ERROR ${containers_err}) -endif() +if (GRPC_CONNECTOR) + message("---------------Generate GRPC proto-----------------------") + execute_process(COMMAND mkdir -p ${CONTAINER_PROTOS_OUT_PATH}) + execute_process(COMMAND mkdir -p ${IMAGE_PROTOS_OUT_PATH}) + execute_process(COMMAND mkdir -p ${CRI_PROTOS_OUT_PATH}) + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/containers --cpp_out=${CONTAINER_PROTOS_OUT_PATH} + ${PROTOS_PATH}/containers/container.proto ERROR_VARIABLE containers_err) + if (containers_err) + message("Parse containers.proto failed: ") + message(FATAL_ERROR ${containers_err}) + endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/containers --grpc_out=${CONTAINER_PROTOS_OUT_PATH} - --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/containers/container.proto ERROR_VARIABLE containers_err) -if (containers_err) - message("Parse containers.proto plugin failed: ") - message(FATAL_ERROR ${containers_err}) -endif() + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/containers --grpc_out=${CONTAINER_PROTOS_OUT_PATH} + --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/containers/container.proto ERROR_VARIABLE containers_err) + if (containers_err) + message("Parse containers.proto plugin failed: ") + message(FATAL_ERROR ${containers_err}) + endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/images -I ${TYPES_PROTOS_PATH} - --cpp_out=${IMAGE_PROTOS_OUT_PATH} ${PROTOS_PATH}/images/images.proto ERROR_VARIABLE images_err) -if (images_err) - message("Parse images.proto failed: ") - message(FATAL_ERROR ${images_err}) -endif() + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/images -I ${TYPES_PROTOS_PATH} + --cpp_out=${IMAGE_PROTOS_OUT_PATH} ${PROTOS_PATH}/images/images.proto ERROR_VARIABLE images_err) + if (images_err) + message("Parse images.proto failed: ") + message(FATAL_ERROR ${images_err}) + endif() + + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/images -I ${TYPES_PROTOS_PATH} --grpc_out=${IMAGE_PROTOS_OUT_PATH} + --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/images/images.proto ERROR_VARIABLE images_err) + if (images_err) + message("Parse images.proto plugin failed: ") + message(FATAL_ERROR ${images_err}) + endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/images -I ${TYPES_PROTOS_PATH} --grpc_out=${IMAGE_PROTOS_OUT_PATH} - --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/images/images.proto ERROR_VARIABLE images_err) -if (images_err) - message("Parse images.proto plugin failed: ") - message(FATAL_ERROR ${images_err}) + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/cri --cpp_out=${CRI_PROTOS_OUT_PATH} ${PROTOS_PATH}/cri/api.proto + ERROR_VARIABLE cri_err) + if (cri_err) + message("Parse cri.proto failed: ") + message(FATAL_ERROR ${cri_err}) + endif() + + execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/cri --grpc_out=${CRI_PROTOS_OUT_PATH} + --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/cri/api.proto ERROR_VARIABLE cri_err) + if (cri_err) + message("Parse cri.proto plugin failed: ") + message(FATAL_ERROR ${cri_err}) + endif() endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/cri --cpp_out=${CRI_PROTOS_OUT_PATH} ${PROTOS_PATH}/cri/api.proto - ERROR_VARIABLE cri_err) -if (cri_err) - message("Parse cri.proto failed: ") - message(FATAL_ERROR ${cri_err}) + +if (ENABLE_OCI_IMAGE) + message("---------------Generate OCI image proto-----------------------") + execute_process(COMMAND mkdir -p ${IMAGE_SERVICE_PROTOS_OUT_PATH}) + execute_process(COMMAND ${CMD_PROTOC} -I ${CMAKE_CURRENT_SOURCE_DIR}/src/api/image_client --cpp_out=${IMAGE_SERVICE_PROTOS_OUT_PATH} + ${CMAKE_CURRENT_SOURCE_DIR}/src/api/image_client/isula_image.proto ERROR_VARIABLE image_client_err) + if (image_client_err) + message("Parse image client api.proto failed: ") + message(FATAL_ERROR ${image_client_err}) + endif() + + execute_process(COMMAND ${CMD_PROTOC} -I ${CMAKE_CURRENT_SOURCE_DIR}/src/api/image_client --grpc_out=${IMAGE_SERVICE_PROTOS_OUT_PATH} + --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${CMAKE_CURRENT_SOURCE_DIR}/src/api/image_client/isula_image.proto + ERROR_VARIABLE image_client_err) endif() -execute_process(COMMAND ${CMD_PROTOC} -I ${PROTOS_PATH}/cri --grpc_out=${CRI_PROTOS_OUT_PATH} - --plugin=protoc-gen-grpc=${CMD_GRPC_CPP_PLUGIN} ${PROTOS_PATH}/cri/api.proto ERROR_VARIABLE cri_err) -if (cri_err) - message("Parse cri.proto plugin failed: ") - message(FATAL_ERROR ${cri_err}) +if (image_client_err) + message("Parse image client api.proto plugin failed: ") + message(FATAL_ERROR ${image_client_err}) endif() diff --git a/cmake/set_build_flags.cmake b/cmake/set_build_flags.cmake index d9b33e9..fbe587c 100644 --- a/cmake/set_build_flags.cmake +++ b/cmake/set_build_flags.cmake @@ -1,6 +1,6 @@ # set common FLAGS set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -rdynamic") -if (GRPC_CONNECTOR) +if (GRPC_CONNECTOR OR ENABLE_OCI_IMAGE) set(CMAKE_CXX_FLAGS "-fPIC -std=c++11 -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror") endif() set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread") diff --git a/iSulad.spec b/iSulad.spec index 638f6f4..9699dab 100644 --- a/iSulad.spec +++ b/iSulad.spec @@ -1,5 +1,5 @@ -%global _version 1.1.2 -%global _release 20191224.232827.git9001f2f6 +%global _version 1.1.4 +%global _release 20191226.061440.gitfa7769d5 %global is_systemd 1 %global debug_package %{nil} @@ -31,14 +31,14 @@ Requires(preun): initscripts BuildRequires: cmake gcc-c++ lxc lxc-devel lcr yajl yajl-devel clibcni-devel BuildRequires: grpc grpc-devel protobuf-devel -BuildRequires: libsecurec libsecurec-devel libcurl libcurl-devel sqlite-devel +BuildRequires: libcurl libcurl-devel sqlite-devel BuildRequires: http-parser-devel libevhtp-devel libevent-devel BuildRequires: libseccomp-devel libcap-devel libwebsockets libwebsockets-devel BuildRequires: systemd-devel git Requires: iSulad-kit lcr lxc clibcni Requires: grpc protobuf yajl -Requires: libcurl libsecurec +Requires: libcurl Requires: sqlite http-parser libseccomp Requires: libcap libwebsockets Requires: libevhtp libevent systemd diff --git a/src/cmd/commander.c b/src/cmd/commander.c index c3b8c08..84acbd6 100644 --- a/src/cmd/commander.c +++ b/src/cmd/commander.c @@ -21,7 +21,6 @@ #include #include #include -#include "securec.h" #include "liblcrd.h" #include "utils.h" @@ -115,10 +114,7 @@ int command_valid_socket(command_option_t *option, const char *arg) void command_init(command_t *self, command_option_t *opts, int opts_len, int argc, const char **argv, const char *description, const char *usage) { - if (memset_s(self, sizeof(command_t), 0, sizeof(command_t)) != EOK) { - COMMAND_ERROR("Failed to set memory"); - return; - } + (void)memset(self, 0, sizeof(command_t)); self->name = argv[0]; self->argc = argc - 2; self->argv = argv + 2; diff --git a/src/cmd/lcrc/arguments.c b/src/cmd/lcrc/arguments.c index 3c5ff3d..1721aba 100644 --- a/src/cmd/lcrc/arguments.c +++ b/src/cmd/lcrc/arguments.c @@ -23,7 +23,6 @@ #include "error.h" #include "commander.h" #include "log.h" -#include "securec.h" #include "utils.h" #include "constants.h" @@ -129,15 +128,9 @@ int client_arguments_init(struct client_arguments *args) args->socket = util_strdup_s(DEFAULT_UNIX_SOCKET); } - if (memset_s(&args->custom_conf, sizeof(args->custom_conf), 0x00, sizeof(struct custom_configs)) != EOK) { - COMMAND_ERROR("Failed to set memory"); - return -1; - } + (void)memset(&args->custom_conf, 0, sizeof(struct custom_configs)); + (void)memset(&args->cr, 0, sizeof(struct args_cgroup_resources)); - if (memset_s(&args->cr, sizeof(args->cr), 0x00, sizeof(struct args_cgroup_resources)) != EOK) { - COMMAND_ERROR("Failed to set memory"); - return -1; - } if (set_default_tls_options(args) != 0) { return -1; } diff --git a/src/cmd/lcrc/base/create.c b/src/cmd/lcrc/base/create.c index b7a133e..b96dbcc 100644 --- a/src/cmd/lcrc/base/create.c +++ b/src/cmd/lcrc/base/create.c @@ -22,7 +22,6 @@ #include "namespace.h" #include "error.h" -#include "securec.h" #include "arguments.h" #include "log.h" #include "utils.h" @@ -295,6 +294,7 @@ static int validate_env(const char *env, char **dst) *dst = util_strdup_s(env); goto out; } else { + int sret; size_t len = strlen(env) + 1 + strlen(value) + 1; *dst = (char *)util_common_calloc_s(len); if (*dst == NULL) { @@ -302,7 +302,8 @@ static int validate_env(const char *env, char **dst) ret = -1; goto out; } - if (sprintf_s(*dst, len, "%s=%s", env, value) < 0) { + sret = snprintf(*dst, len, "%s=%s", env, value); + if (sret < 0 || (size_t)sret >= len) { ERROR("Failed to compose env string"); ret = -1; goto out; diff --git a/src/cmd/lcrc/base/kill.c b/src/cmd/lcrc/base/kill.c index c95942a..043d9db 100644 --- a/src/cmd/lcrc/base/kill.c +++ b/src/cmd/lcrc/base/kill.c @@ -13,7 +13,6 @@ * Description: provide container kill functions ******************************************************************************/ #include "error.h" -#include "securec.h" #include "arguments.h" #include "kill.h" #include "log.h" diff --git a/src/cmd/lcrc/base/rename.c b/src/cmd/lcrc/base/rename.c index be1ab60..642a851 100644 --- a/src/cmd/lcrc/base/rename.c +++ b/src/cmd/lcrc/base/rename.c @@ -13,7 +13,6 @@ * Description: provide container stop functions ******************************************************************************/ #include "rename.h" -#include "securec.h" #include "arguments.h" #include "log.h" #include "utils.h" diff --git a/src/cmd/lcrc/base/restart.c b/src/cmd/lcrc/base/restart.c index d79ac66..745f794 100644 --- a/src/cmd/lcrc/base/restart.c +++ b/src/cmd/lcrc/base/restart.c @@ -13,7 +13,6 @@ * Description: provide container restart functions ******************************************************************************/ #include -#include #include "restart.h" #include "arguments.h" diff --git a/src/cmd/lcrc/base/rm.c b/src/cmd/lcrc/base/rm.c index 01e1ce3..73fd1d1 100644 --- a/src/cmd/lcrc/base/rm.c +++ b/src/cmd/lcrc/base/rm.c @@ -20,7 +20,6 @@ #include "commands.h" #include "console.h" #include "utils.h" -#include "securec.h" const char g_cmd_delete_desc[] = "Remove one or more containers"; const char g_cmd_delete_usage[] = "rm [OPTIONS] CONTAINER [CONTAINER...]"; diff --git a/src/cmd/lcrc/base/start.c b/src/cmd/lcrc/base/start.c index 8a293e7..2389769 100644 --- a/src/cmd/lcrc/base/start.c +++ b/src/cmd/lcrc/base/start.c @@ -19,7 +19,6 @@ #include #include "error.h" -#include "securec.h" #include "arguments.h" #include "commander.h" #include "start.h" diff --git a/src/cmd/lcrc/base/stop.c b/src/cmd/lcrc/base/stop.c index 74c01d2..56b5483 100644 --- a/src/cmd/lcrc/base/stop.c +++ b/src/cmd/lcrc/base/stop.c @@ -13,7 +13,6 @@ * Description: provide container stop functions ******************************************************************************/ #include "stop.h" -#include "securec.h" #include "arguments.h" #include "log.h" #include "utils.h" diff --git a/src/cmd/lcrc/commands.c b/src/cmd/lcrc/commands.c index 85cfde0..5e4eb10 100644 --- a/src/cmd/lcrc/commands.c +++ b/src/cmd/lcrc/commands.c @@ -19,7 +19,6 @@ #include #include #include -#include "securec.h" #include "arguments.h" #include "config.h" @@ -55,8 +54,8 @@ static void send_msg_to_syslog(int argc, const char **argv) } ppid = getppid(); // get parent cmdline, "/proc/ppid/cmdline" - nret = sprintf_s(cmdline_path, PATH_MAX, "/proc/%d/cmdline", ppid); - if (nret < 0) { + nret = snprintf(cmdline_path, PATH_MAX, "/proc/%d/cmdline", ppid); + if (nret < 0 || nret >= PATH_MAX) { COMMAND_ERROR("Get parent '%d' cmdline path failed", ppid); return; } @@ -304,9 +303,9 @@ int create_console_fifos(bool attach_stdin, bool attach_stdout, bool attach_stde return -1; } - ret = sprintf_s(subpath, sizeof(subpath), "%s/%s-%u-%u", name, type, (unsigned int)getpid(), - (unsigned int)pthread_self()); - if (ret < 0) { + ret = snprintf(subpath, sizeof(subpath), "%s/%s-%u-%u", name, type, (unsigned int)getpid(), + (unsigned int)pthread_self()); + if (ret < 0 || (size_t)ret >= sizeof(subpath)) { ERROR("Path is too long"); goto cleanup; } diff --git a/src/cmd/lcrc/extend/events.c b/src/cmd/lcrc/extend/events.c index 8525c56..e46ed9c 100644 --- a/src/cmd/lcrc/extend/events.c +++ b/src/cmd/lcrc/extend/events.c @@ -17,7 +17,6 @@ #include "arguments.h" #include "log.h" #include "lcrc_connect.h" -#include "securec.h" const char g_cmd_events_desc[] = "Get real time events from the server"; const char g_cmd_events_usage[] = "events [command options]"; diff --git a/src/cmd/lcrc/extend/export.c b/src/cmd/lcrc/extend/export.c index 214c597..4d8ecb4 100644 --- a/src/cmd/lcrc/extend/export.c +++ b/src/cmd/lcrc/extend/export.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "export.h" #include -#include "securec.h" #include "utils.h" #include "arguments.h" #include "log.h" @@ -31,17 +30,12 @@ struct client_arguments g_cmd_export_args = {}; static int client_export(const struct client_arguments *args) { int ret = 0; - errno_t mret; lcrc_connect_ops *ops = NULL; struct lcrc_export_request request; struct lcrc_export_response *response = NULL; client_connect_config_t config = { 0 }; - mret = memset_s(&request, sizeof(request), 0x00, sizeof(request)); - if (mret != EOK) { - ERROR("Failed to memset export request"); - return -1; - } + (void)memset(&request, 0, sizeof(request)); response = util_common_calloc_s(sizeof(struct lcrc_export_response)); if (response == NULL) { ERROR("Resume: Out of memory"); @@ -110,13 +104,14 @@ int cmd_export_main(int argc, const char **argv) /* If it's not a absolute path, add cwd to be absolute path */ if (g_cmd_export_args.file[0] != '/') { + int sret; char cwd[PATH_MAX] = { 0 }; if (!getcwd(cwd, sizeof(cwd))) { COMMAND_ERROR("get cwd failed:%s", strerror(errno)); exit(ECOMMON); } - - if (sprintf_s(file, sizeof(file), "%s/%s", cwd, g_cmd_export_args.file) < 0) { + sret = snprintf(file, sizeof(file), "%s/%s", cwd, g_cmd_export_args.file); + if (sret < 0 || (size_t)sret >= sizeof(file)) { COMMAND_ERROR("filename too long"); exit(EINVALIDARGS); } diff --git a/src/cmd/lcrc/extend/pause.c b/src/cmd/lcrc/extend/pause.c index 3a7cc8d..83182ee 100644 --- a/src/cmd/lcrc/extend/pause.c +++ b/src/cmd/lcrc/extend/pause.c @@ -13,7 +13,6 @@ * Description: provide container pause functions ******************************************************************************/ #include "pause.h" -#include "securec.h" #include "utils.h" #include "arguments.h" #include "log.h" diff --git a/src/cmd/lcrc/extend/resume.c b/src/cmd/lcrc/extend/resume.c index da4fb5c..e2625c3 100644 --- a/src/cmd/lcrc/extend/resume.c +++ b/src/cmd/lcrc/extend/resume.c @@ -13,7 +13,6 @@ * Description: provide container resume functions ******************************************************************************/ #include "resume.h" -#include "securec.h" #include "utils.h" #include "arguments.h" #include "log.h" diff --git a/src/cmd/lcrc/extend/stats.c b/src/cmd/lcrc/extend/stats.c index cab626d..aab1db4 100644 --- a/src/cmd/lcrc/extend/stats.c +++ b/src/cmd/lcrc/extend/stats.c @@ -26,7 +26,6 @@ #include #include "error.h" -#include "securec.h" #include "arguments.h" #include "stats.h" #include "utils.h" @@ -54,20 +53,20 @@ static void lcrc_size_humanize(unsigned long long val, char *buf, size_t bufsz) { int ret = 0; if (val > 1024 * 1024 * 1024) { - ret = sprintf_s(buf, bufsz, "%u.%.2u GiB", (unsigned int)(val >> 30), - (unsigned int)(val & ((1 << 30) - 1)) / 10737419); + ret = snprintf(buf, bufsz, "%u.%.2u GiB", (unsigned int)(val >> 30), + (unsigned int)(val & ((1 << 30) - 1)) / 10737419); } else if (val > 1024 * 1024) { unsigned long long x = val + 5243; /* for rounding */ - ret = sprintf_s(buf, bufsz, "%u.%.2u MiB", (unsigned int)(x >> 20), - (unsigned int)(((x & ((1 << 20) - 1)) * 100) >> 20)); + ret = snprintf(buf, bufsz, "%u.%.2u MiB", (unsigned int)(x >> 20), + (unsigned int)(((x & ((1 << 20) - 1)) * 100) >> 20)); } else if (val > 1024) { unsigned long long x = val + 5; /* for rounding */ - ret = sprintf_s(buf, bufsz, "%u.%.2u KiB", (unsigned int)(x >> 10), - (unsigned int)(((x & ((1 << 10) - 1)) * 100) >> 10)); + ret = snprintf(buf, bufsz, "%u.%.2u KiB", (unsigned int)(x >> 10), + (unsigned int)(((x & ((1 << 10) - 1)) * 100) >> 10)); } else { - ret = sprintf_s(buf, bufsz, "%u.00 B", (unsigned int)val); + ret = snprintf(buf, bufsz, "%u.00 B", (unsigned int)val); } - if (ret < 0) { + if (ret < 0 || (size_t)ret >= bufsz) { ERROR("Humanize sprintf failed!"); } } @@ -100,13 +99,13 @@ static void stats_print(const struct lcrc_container_info *stats) lcrc_size_humanize(stats->mem_used, mem_used_str, sizeof(mem_used_str)); lcrc_size_humanize(stats->mem_limit, mem_limit_str, sizeof(mem_limit_str)); - len = sprintf_s(iosb_str, sizeof(iosb_str), "%s / %s", iosb_read_str, iosb_write_str); - if (len < 0) { + len = snprintf(iosb_str, sizeof(iosb_str), "%s / %s", iosb_read_str, iosb_write_str); + if (len < 0 || (size_t)len >= sizeof(iosb_str)) { ERROR("Sprintf iosb_str failed"); return; } - len = sprintf_s(mem_str, sizeof(mem_str), "%s / %s", mem_used_str, mem_limit_str); - if (len < 0) { + len = snprintf(mem_str, sizeof(mem_str), "%s / %s", mem_used_str, mem_limit_str); + if (len < 0 || (size_t)len >= sizeof(mem_str)) { ERROR("Sprintf mem_str failed"); return; } diff --git a/src/cmd/lcrc/extend/update.c b/src/cmd/lcrc/extend/update.c index 46557f1..069126c 100644 --- a/src/cmd/lcrc/extend/update.c +++ b/src/cmd/lcrc/extend/update.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include #include -#include "securec.h" #include "arguments.h" #include "update.h" #include "utils.h" diff --git a/src/cmd/lcrc/images/images.c b/src/cmd/lcrc/images/images.c index 5ef0eed..bba14a8 100644 --- a/src/cmd/lcrc/images/images.c +++ b/src/cmd/lcrc/images/images.c @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" @@ -60,9 +59,9 @@ static char *trans_time(int64_t created) return NULL; } - nret = sprintf_s(formated_time, sizeof(formated_time), "%04d-%02d-%02d %02d:%02d:%02d", t.tm_year + 1900, - t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); - if (nret < 0) { + nret = snprintf(formated_time, sizeof(formated_time), "%04d-%02d-%02d %02d:%02d:%02d", t.tm_year + 1900, + t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); + if (nret < 0 || nret >= sizeof(formated_time)) { ERROR("format created time failed"); return NULL; } @@ -187,8 +186,8 @@ static void list_field_width(const struct lcrc_image_info *images_list, const si } } - slen = sprintf_s(tmpbuffer, sizeof(tmpbuffer), "%.2f", (float)(in->size) / (1024 * 1024)); - if (slen < 0) { + slen = snprintf(tmpbuffer, sizeof(tmpbuffer), "%.2f", (float)(in->size) / (1024 * 1024)); + if (slen < 0 || (size_t)slen >= sizeof(tmpbuffer)) { ERROR("sprintf tmpbuffer failed"); return; } diff --git a/src/cmd/lcrc/images/load.c b/src/cmd/lcrc/images/load.c index 1118a82..27218a0 100644 --- a/src/cmd/lcrc/images/load.c +++ b/src/cmd/lcrc/images/load.c @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" @@ -150,13 +149,15 @@ int cmd_load_main(int argc, const char **argv) /* If it's not a absolute path, add cwd to be absolute path */ if (g_cmd_load_args.file[0] != '/') { char cwd[PATH_MAX] = { 0 }; + int len; if (!getcwd(cwd, sizeof(cwd))) { COMMAND_ERROR("get cwd failed:%s", strerror(errno)); exit(exit_code); } - if (sprintf_s(file, sizeof(file), "%s/%s", cwd, g_cmd_load_args.file) < 0) { + len = snprintf(file, sizeof(file), "%s/%s", cwd, g_cmd_load_args.file); + if (len < 0 || (size_t)len >= sizeof(file)) { COMMAND_ERROR("filename too long"); exit(exit_code); } diff --git a/src/cmd/lcrc/images/login.c b/src/cmd/lcrc/images/login.c index 36f2997..68d851f 100644 --- a/src/cmd/lcrc/images/login.c +++ b/src/cmd/lcrc/images/login.c @@ -19,7 +19,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" @@ -104,9 +103,7 @@ static int get_password_from_notty(struct client_arguments *args) return -1; } args->password = util_strdup_s(password); - if (memset_s(password, sizeof(password), 0, sizeof(password)) != EOK) { - ERROR("Failed to memset sensitive string memory"); - } + (void)memset(password, 0, sizeof(password)); } return 0; @@ -152,9 +149,7 @@ static int get_auth_from_terminal(struct client_arguments *args) return -1; } args->password = util_strdup_s(password); - if (memset_s(password, sizeof(password), 0, sizeof(password)) != EOK) { - ERROR("Failed to memset sensitive string memory"); - } + (void)memset(password, 0, sizeof(password)); } return 0; diff --git a/src/cmd/lcrc/images/logout.c b/src/cmd/lcrc/images/logout.c index 9bf4711..5068b3a 100644 --- a/src/cmd/lcrc/images/logout.c +++ b/src/cmd/lcrc/images/logout.c @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" diff --git a/src/cmd/lcrc/images/pull.c b/src/cmd/lcrc/images/pull.c index 42e32d4..62cf64a 100644 --- a/src/cmd/lcrc/images/pull.c +++ b/src/cmd/lcrc/images/pull.c @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" diff --git a/src/cmd/lcrc/images/rmi.c b/src/cmd/lcrc/images/rmi.c index d95b17c..fc26430 100644 --- a/src/cmd/lcrc/images/rmi.c +++ b/src/cmd/lcrc/images/rmi.c @@ -17,7 +17,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "arguments.h" diff --git a/src/cmd/lcrc/information/health.c b/src/cmd/lcrc/information/health.c index 070d3c1..79f2cd5 100644 --- a/src/cmd/lcrc/information/health.c +++ b/src/cmd/lcrc/information/health.c @@ -13,7 +13,6 @@ * Description: provide container health functions ******************************************************************************/ #include "health.h" -#include "securec.h" #include "utils.h" #include "arguments.h" #include "log.h" diff --git a/src/cmd/lcrc/information/info.c b/src/cmd/lcrc/information/info.c index 7503442..1f1aa78 100644 --- a/src/cmd/lcrc/information/info.c +++ b/src/cmd/lcrc/information/info.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "info.h" #include -#include "securec.h" #include "utils.h" #include "arguments.h" diff --git a/src/cmd/lcrc/information/inspect.c b/src/cmd/lcrc/information/inspect.c index 5a5374c..2be1291 100644 --- a/src/cmd/lcrc/information/inspect.c +++ b/src/cmd/lcrc/information/inspect.c @@ -19,7 +19,6 @@ #include "lcrc_connect.h" #include "console.h" #include "utils.h" -#include "securec.h" #include "json_common.h" #include @@ -85,7 +84,7 @@ static yajl_val json_object(yajl_val element, char *key) char *top_key = key; char *next_context = NULL; - top_key = strtok_s(top_key, ".", &next_context); + top_key = strtok_r(top_key, ".", &next_context); if (top_key == NULL) { return NULL; } @@ -111,7 +110,7 @@ static yajl_val json_array(yajl_val element, char *key) if (YAJL_GET_ARRAY(element) != NULL) { size = YAJL_GET_ARRAY(element)->len; } - top_key = strtok_s(top_key, ".", &next_context); + top_key = strtok_r(top_key, ".", &next_context); if (top_key == NULL) { return NULL; } @@ -616,7 +615,7 @@ static char *inspect_pause_filter(const char *arg) input_str = util_strdup_s(arg); - p = strtok_s(input_str, ".", &next_context); + p = strtok_r(input_str, ".", &next_context); if (p == NULL) { goto out; } @@ -626,7 +625,7 @@ static char *inspect_pause_filter(const char *arg) goto out; } - p = strtok_s(p, " }", &next_context); + p = strtok_r(p, " }", &next_context); if (p == NULL) { goto out; } diff --git a/src/cmd/lcrc/information/logs.c b/src/cmd/lcrc/information/logs.c index f394796..533afab 100644 --- a/src/cmd/lcrc/information/logs.c +++ b/src/cmd/lcrc/information/logs.c @@ -24,7 +24,6 @@ #include #include "error.h" -#include "securec.h" #include "logs.h" #include "arguments.h" #include "log.h" diff --git a/src/cmd/lcrc/information/ps.c b/src/cmd/lcrc/information/ps.c index eae9639..d6f40c4 100644 --- a/src/cmd/lcrc/information/ps.c +++ b/src/cmd/lcrc/information/ps.c @@ -19,7 +19,6 @@ #include #include #include -#include "securec.h" #include "arguments.h" #include "ps.h" #include "utils.h" @@ -162,21 +161,11 @@ static int mix_container_state(const struct lcrc_container_summary_info *in, cha container_status = lcrc_lcrsta2str(in->status); if (container_status == NULL) { - ret = strcpy_s(state, len, "-"); - if (ret < 0) { - ERROR("Failed to copy string"); - ret = -1; - goto out; - } + (void)strcpy(state, "-"); } else { - if (strcpy_s(state, len, container_status) != EOK) { - ERROR("Failed to copy string"); - ret = -1; - goto out; - } + (void)strcpy(state, container_status); } -out: return ret; } @@ -184,15 +173,18 @@ static int handle_running_status(const char *start_at, const struct lcrc_contain char *status, size_t len) { int ret = 0; + int nret; if (in->health_state != NULL) { - if (sprintf_s(status, len, "Up %s %s", start_at, in->health_state) < 0) { + nret = snprintf(status, len, "Up %s %s", start_at, in->health_state); + if (nret < 0 || nret >= len) { ERROR("Failed to compose string"); ret = -1; goto out; } } else { - if (sprintf_s(status, len, "Up %s", start_at) < 0) { + nret = snprintf(status, len, "Up %s", start_at); + if (nret < 0 || nret >= len) { ERROR("Failed to compose string"); ret = -1; goto out; @@ -206,6 +198,7 @@ out: static int mix_container_status(const struct lcrc_container_summary_info *in, char *status, size_t len) { int ret = -1; + int sret = 0; char startat_duration[TIME_DURATION_MAX_LEN] = { 0 }; char finishat_duration[TIME_DURATION_MAX_LEN] = { 0 }; char *start_at = NULL; @@ -219,24 +212,19 @@ static int mix_container_status(const struct lcrc_container_summary_info *in, ch if (handle_running_status(start_at, in, status, len) != 0) { goto out; } - } else if (in->status == CONTAINER_STATUS_CREATED) { - if (sprintf_s(status, len, "Created") < 0) { - goto out; - } - } else if (in->status == CONTAINER_STATUS_RESTARTING) { - if (sprintf_s(status, len, "Restarting (%d) %s", (int)in->exit_code, finish_at) < 0) { - goto out; - } - } else if (in->status == CONTAINER_STATUS_PAUSED) { - if (sprintf_s(status, len, "Up %s (Paused)", start_at) < 0) { - goto out; - } - } else if (in->status == CONTAINER_STATUS_STARTING) { - if (sprintf_s(status, len, "Starting %s", start_at) < 0) { - goto out; - } } else { - if (sprintf_s(status, len, "Exited (%d) %s", (int)in->exit_code, finish_at) < 0) { + if (in->status == CONTAINER_STATUS_CREATED) { + sret = snprintf(status, len, "Created"); + } else if (in->status == CONTAINER_STATUS_RESTARTING) { + sret = snprintf(status, len, "Restarting (%d) %s", (int)in->exit_code, finish_at); + } else if (in->status == CONTAINER_STATUS_PAUSED) { + sret = snprintf(status, len, "Up %s (Paused)", start_at); + } else if (in->status == CONTAINER_STATUS_STARTING) { + sret = snprintf(status, len, "Starting %s", start_at); + } else { + sret = snprintf(status, len, "Exited (%d) %s", (int)in->exit_code, finish_at); + } + if (sret < 0 || (size_t)sret >= len) { goto out; } } @@ -523,8 +511,8 @@ static void calculate_uint_str_length(uint32_t data, unsigned int *length) int len = 0; char tmpbuffer[UINT_LEN + 1] = { 0 }; - len = sprintf_s(tmpbuffer, sizeof(tmpbuffer), "%u", data); - if (len < 0) { + len = snprintf(tmpbuffer, sizeof(tmpbuffer), "%u", data); + if (len < 0 || (size_t)len >= sizeof(tmpbuffer)) { ERROR("sprintf buffer failed"); return; } @@ -808,7 +796,7 @@ static char *get_filter_string(const char *arg) input_str = util_strdup_s(arg); - p = strtok_s(input_str, ".", &next_context); + p = strtok_r(input_str, ".", &next_context); if (p == NULL) { goto out; } @@ -818,7 +806,7 @@ static char *get_filter_string(const char *arg) goto out; } - p = strtok_s(p, " }", &next_context); + p = strtok_r(p, " }", &next_context); if (p == NULL) { goto out; } diff --git a/src/cmd/lcrc/information/top.c b/src/cmd/lcrc/information/top.c index e91d61e..e6456fa 100644 --- a/src/cmd/lcrc/information/top.c +++ b/src/cmd/lcrc/information/top.c @@ -20,7 +20,6 @@ #include "commands.h" #include "console.h" #include "utils.h" -#include "securec.h" #include "container_inspect.h" #include "attach.h" #include "commander.h" diff --git a/src/cmd/lcrc/information/version.c b/src/cmd/lcrc/information/version.c index 78f56cf..f15c3d6 100644 --- a/src/cmd/lcrc/information/version.c +++ b/src/cmd/lcrc/information/version.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "version.h" #include -#include "securec.h" #include "utils.h" #include "arguments.h" diff --git a/src/cmd/lcrc/information/wait.c b/src/cmd/lcrc/information/wait.c index de6b88f..1286eae 100644 --- a/src/cmd/lcrc/information/wait.c +++ b/src/cmd/lcrc/information/wait.c @@ -13,7 +13,6 @@ * Description: provide container wait functions ******************************************************************************/ #include "error.h" -#include "securec.h" #include "wait.h" #include "arguments.h" #include "log.h" diff --git a/src/cmd/lcrc/stream/attach.c b/src/cmd/lcrc/stream/attach.c index c28c241..0033f85 100644 --- a/src/cmd/lcrc/stream/attach.c +++ b/src/cmd/lcrc/stream/attach.c @@ -19,7 +19,6 @@ #include #include -#include "securec.h" #include "arguments.h" #include "exec.h" #include "log.h" diff --git a/src/cmd/lcrc/stream/cp.c b/src/cmd/lcrc/stream/cp.c index 2aa8f44..3f0a6e4 100644 --- a/src/cmd/lcrc/stream/cp.c +++ b/src/cmd/lcrc/stream/cp.c @@ -23,7 +23,6 @@ #include "log.h" #include "path.h" #include "lcrc_connect.h" -#include "securec.h" #include "lcrdtar.h" #define FromContainer 0x01u @@ -231,9 +230,7 @@ static void ignore_sigpipe() /* * Ignore SIGPIPE so the current process still exists after child process exited. */ - if (memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction)) != EOK) { - WARN("Failed to set memory"); - } + (void)memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); diff --git a/src/cmd/lcrc/stream/exec.c b/src/cmd/lcrc/stream/exec.c index 313022d..d95238d 100644 --- a/src/cmd/lcrc/stream/exec.c +++ b/src/cmd/lcrc/stream/exec.c @@ -19,7 +19,6 @@ #include #include -#include "securec.h" #include "arguments.h" #include "exec.h" #include "log.h" diff --git a/src/cmd/lcrd/commands.c b/src/cmd/lcrd/commands.c index a5d1e39..813a855 100644 --- a/src/cmd/lcrd/commands.c +++ b/src/cmd/lcrd/commands.c @@ -396,16 +396,18 @@ static int ulimit_flag_join(char* out_msg, const size_t msg_len, const size_t de size_t i; char *tmp = NULL; - if (sprintf_s(out_msg, msg_len, "[") < 0) { + int nret = snprintf(out_msg, msg_len, "["); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } for (i = 0; i < default_ulimit_len; i++) { tmp = util_strdup_s(out_msg); - if (sprintf_s(out_msg, msg_len, "%s %s=%lld:%lld", tmp, - default_ulimit[i]->name, (long long int)default_ulimit[i]->soft, - (long long int)default_ulimit[i]->hard) < 0) { + nret = snprintf(out_msg, msg_len, "%s %s=%lld:%lld", tmp, + default_ulimit[i]->name, (long long int)default_ulimit[i]->soft, + (long long int)default_ulimit[i]->hard); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } @@ -414,7 +416,8 @@ static int ulimit_flag_join(char* out_msg, const size_t msg_len, const size_t de } tmp = util_strdup_s(out_msg); - if (sprintf_s(out_msg, msg_len, "%s ]", tmp) < 0) { + nret = snprintf(out_msg, msg_len, "%s ]", tmp); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } @@ -435,15 +438,17 @@ static int ulimit_file_join(char* out_msg, const size_t msg_len, char *tmp = NULL; isulad_daemon_configs_default_ulimits_element *ptr = NULL; - if (sprintf_s(out_msg, msg_len, "[") < 0) { + int nret = snprintf(out_msg, msg_len, "["); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } for (i = 0; i < default_ulimits_len; i++) { ptr = default_ulimits[i]; tmp = util_strdup_s(out_msg); - if (sprintf_s(out_msg, msg_len, "%s %s=%lld:%lld", tmp, ptr->name, - (long long int)(ptr->soft), (long long int)(ptr->hard)) < 0) { + nret = snprintf(out_msg, msg_len, "%s %s=%lld:%lld", tmp, ptr->name, + (long long int)(ptr->soft), (long long int)(ptr->hard)); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } @@ -452,7 +457,8 @@ static int ulimit_file_join(char* out_msg, const size_t msg_len, } tmp = util_strdup_s(out_msg); - if (sprintf_s(out_msg, msg_len, "%s ]", tmp) < 0) { + nret = snprintf(out_msg, msg_len, "%s ]", tmp); + if (nret < 0 || nret >= msg_len) { ERROR("Failed to print string"); goto out; } diff --git a/src/cmd/lcrd/main.c b/src/cmd/lcrd/main.c index d46cbdb..25ad8a7 100644 --- a/src/cmd/lcrd/main.c +++ b/src/cmd/lcrd/main.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -37,7 +38,6 @@ #include "constants.h" #include "liblcrd.h" -#include "securec.h" #include "collector.h" #include "commands.h" #include "log.h" @@ -278,10 +278,7 @@ static int ignore_signals() * Ignore SIGHUP so lcrd process still exists after * terminal die. */ - if (memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction)) != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); @@ -307,10 +304,7 @@ static int add_shutdown_signal_handler() { struct sigaction sa; - if (memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction)) != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&sa, 0, sizeof(struct sigaction)); if (sem_init(&g_daemon_shutdown_sem, 0, 0) == -1) { ERROR("Failed to init daemon shutdown sem"); @@ -325,10 +319,7 @@ static int add_shutdown_signal_handler() return -1; } - if (memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction)) != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sigterm_handler; sigemptyset(&sa.sa_mask); @@ -441,8 +432,8 @@ int check_and_save_pid(const char *fn) goto out; } - len = sprintf_s(pidbuf, sizeof(pidbuf), "%lu\n", (unsigned long)getpid()); - if (len < 0) { + len = snprintf(pidbuf, sizeof(pidbuf), "%lu\n", (unsigned long)getpid()); + if (len < 0 || len >= sizeof(pidbuf)) { ERROR("failed sprint pidbuf"); ret = -1; goto out; @@ -504,8 +495,8 @@ static int set_parent_mount_dir(struct service_arguments *args) ERROR("Out of memory"); goto out; } - nret = sprintf_s(rootfsdir, len, "%s/mnt/rootfs", args->json_confs->graph); - if (nret < 0) { + nret = snprintf(rootfsdir, len, "%s/mnt/rootfs", args->json_confs->graph); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to print string"); goto out; } @@ -1134,7 +1125,7 @@ static int load_listener(const struct service_arguments *args) char *proto_addr = NULL; proto_addr = parse_host(args->json_confs->tls, args->hosts[i]); - proto = strtok_s(proto_addr, delim, &addr); + proto = strtok_r(proto_addr, delim, &addr); if (proto == NULL) { ERROR("Failed to get proto"); ret = -1; diff --git a/src/config/lcrd_config.c b/src/config/lcrd_config.c index 1dcd854..456fb19 100644 --- a/src/config/lcrd_config.c +++ b/src/config/lcrd_config.c @@ -25,7 +25,6 @@ #include #include "constants.h" -#include "securec.h" #include "log.h" #include "utils.h" #include "lcrd_config.h" @@ -95,8 +94,8 @@ int get_system_cpu_usage(uint64_t *val) ret = -1; goto out; } - nret = sscanf_s(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, - &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); + nret = sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, + &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); if (nret != BUFFER_ITEM_NUMS) { ERROR("sscanf buffer failed"); ret = -1; @@ -204,7 +203,8 @@ char *conf_get_engine_rootpath() goto free_out; } - if (sprintf_s(epath, len, "%s/%s", rootpath, ENGINE_ROOTPATH_NAME) < 0) { + int nret = snprintf(epath, len, "%s/%s", rootpath, ENGINE_ROOTPATH_NAME); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf engine path failed"); free(epath); epath = NULL; @@ -242,7 +242,8 @@ char *conf_get_graph_rootpath() goto free_out; } - if (sprintf_s(epath, len, "%s/%s", rootpath, GRAPH_ROOTPATH_NAME) < 0) { + int nret = snprintf(epath, len, "%s/%s", rootpath, GRAPH_ROOTPATH_NAME); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf graph path failed"); free(epath); epath = NULL; @@ -280,7 +281,8 @@ char *conf_get_graph_check_flag_file() goto free_out; } - if (sprintf_s(epath, len, "%s/%s/%s", rootpath, GRAPH_ROOTPATH_NAME, GRAPH_ROOTPATH_CHECKED_FLAG) < 0) { + int nret = snprintf(epath, len, "%s/%s/%s", rootpath, GRAPH_ROOTPATH_NAME, GRAPH_ROOTPATH_CHECKED_FLAG); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf graph checked flag failed"); free(epath); epath = NULL; @@ -318,7 +320,8 @@ char *conf_get_graph_run_path() goto free_out; } - if (sprintf_s(epath, len, "%s/%s", rootpath, GRAPH_ROOTPATH_NAME) < 0) { + int nret = snprintf(epath, len, "%s/%s", rootpath, GRAPH_ROOTPATH_NAME); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf graph run path failed"); free(epath); epath = NULL; @@ -367,7 +370,8 @@ char *conf_get_routine_rootdir(const char *runtime) goto out; } - if (sprintf_s(path, len, "%s/%s/%s", conf->json_confs->graph, ENGINE_ROOTPATH_NAME, runtime) < 0) { + int nret = snprintf(path, len, "%s/%s/%s", conf->json_confs->graph, ENGINE_ROOTPATH_NAME, runtime); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to sprintf path"); free(path); path = NULL; @@ -412,7 +416,8 @@ char *conf_get_routine_statedir(const char *runtime) goto out; } - if (sprintf_s(path, len, "%s/%s", conf->json_confs->state, runtime) < 0) { + int nret = snprintf(path, len, "%s/%s", conf->json_confs->state, runtime); + if (nret < 0 || (size_t)nret >= len) { ERROR("sprintf path failed"); free(path); path = NULL; @@ -762,8 +767,8 @@ char *get_log_file_helper(const struct service_arguments *conf, const char *suff goto out; } - nret = sprintf_s(logfile, len, "%s/%s", conf->logpath, suffix); - if (nret < 0) { + nret = snprintf(logfile, len, "%s/%s", conf->logpath, suffix); + if (nret < 0 || (size_t)nret >= len) { free(logfile); logfile = NULL; ERROR("Failed to sprintf log path"); @@ -800,8 +805,8 @@ char *conf_get_lcrd_log_gather_fifo_path() ERROR("Out of memory"); goto err_out; } - nret = sprintf_s(logfile, len, "%s%s", statedir, "/lcrd_log_gather_fifo"); - if (nret < 0) { + nret = snprintf(logfile, len, "%s%s", statedir, "/lcrd_log_gather_fifo"); + if (nret < 0 || (size_t)nret >= len) { ERROR("Sprintf log file failed"); goto err_out; } @@ -860,7 +865,8 @@ char *conf_get_engine_log_file() FATAL("Out of Memory"); goto out; } - if (sprintf_s(full_path, len, "%s%s", prefix, logfile) < 0) { + int nret = snprintf(full_path, len, "%s%s", prefix, logfile); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to sprintf engine log path"); free(full_path); full_path = NULL; @@ -1368,11 +1374,13 @@ static int maybe_create_cpu_realtime_file(bool present, int64_t value, const cha return -1; } - if (sprintf_s(fpath, sizeof(fpath), "%s/%s", path, file) < 0) { + int nret = snprintf(fpath, sizeof(fpath), "%s/%s", path, file); + if (nret < 0 || nret >= sizeof(fpath)) { ERROR("Failed to print string"); return -1; } - if (sprintf_s(buf, sizeof(buf), "%lld", (long long int)value) < 0) { + nret = snprintf(buf, sizeof(buf), "%lld", (long long int)value); + if (nret < 0 || (size_t)nret >= sizeof(buf)) { ERROR("Failed to print string"); return -1; } @@ -1452,7 +1460,8 @@ static int recursively_create_cgroup(const char *path, int recursive_depth, int6 root[1] = '\0'; } - if (sprintf_s(fpath, sizeof(fpath), "%s/%s/%s", mnt, root, path) < 0) { + int nret = snprintf(fpath, sizeof(fpath), "%s/%s/%s", mnt, root, path); + if (nret < 0 || (size_t)nret >= sizeof(fpath)) { ERROR("Failed to print string"); ret = -1; goto out; diff --git a/src/connect/client/grpc/grpc_containers_client.cc b/src/connect/client/grpc/grpc_containers_client.cc index 268f6cc..59b3293 100644 --- a/src/connect/client/grpc/grpc_containers_client.cc +++ b/src/connect/client/grpc/grpc_containers_client.cc @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "container_copy_to_request.h" #include "container_exec_request.h" #include "utils.h" @@ -1541,10 +1540,7 @@ public: } lcrc_host_config_t hostconfig; - if (memset_s(&hostconfig, sizeof(hostconfig), 0, sizeof(hostconfig)) != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&hostconfig, 0, sizeof(hostconfig)); if (request->updateconfig) { hostconfig.restart_policy = request->updateconfig->restart_policy; @@ -1793,13 +1789,7 @@ private: void event_from_grpc(container_events_format_t *event, Event *gevent) { - errno_t mret = EOK; - - mret = memset_s(event, sizeof(*event), 0, sizeof(*event)); - if (mret != EOK) { - ERROR("Failed to set memory"); - return; - } + (void)memset(event, 0, sizeof(*event)); if (!gevent->id().empty()) { event->id = (char *)gevent->id().c_str(); } @@ -1855,9 +1845,7 @@ static ssize_t CopyFromContainerRead(void *context, void *buf, size_t len) } size_t data_len = res.data().length(); if (data_len <= len) { - if (memcpy_s(buf, len, res.data().c_str(), data_len) != EOK) { - return -1; - } + (void)memcpy(buf, res.data().c_str(), data_len); return (ssize_t)data_len; } diff --git a/src/connect/client/grpc/grpc_images_client.cc b/src/connect/client/grpc/grpc_images_client.cc index ab92f15..6fab05c 100644 --- a/src/connect/client/grpc/grpc_images_client.cc +++ b/src/connect/client/grpc/grpc_images_client.cc @@ -14,7 +14,6 @@ ******************************************************************************/ #include "grpc_images_client.h" #include -#include "securec.h" #include "images.grpc.pb.h" #include "api.grpc.pb.h" #include "utils.h" diff --git a/src/connect/client/grpc/grpc_isula_image_client.cc b/src/connect/client/grpc/grpc_isula_image_client.cc index b9b42a6..f94dc8b 100644 --- a/src/connect/client/grpc/grpc_isula_image_client.cc +++ b/src/connect/client/grpc/grpc_isula_image_client.cc @@ -84,6 +84,12 @@ int copy_image_metadata(const isula::Image &gimage, struct image_metadata **meta goto err_out; } + tmp_data->size = gimage.size(); + + if (gimage.has_uid()) { + tmp_data->uid = gimage.uid().value(); + } + if (!gimage.username().empty()) { tmp_data->username = util_strdup_s(gimage.username().c_str()); } diff --git a/src/connect/client/isula_image_connect.c b/src/connect/client/isula_image_connect.c index c70429f..470147d 100644 --- a/src/connect/client/isula_image_connect.c +++ b/src/connect/client/isula_image_connect.c @@ -14,7 +14,6 @@ *******************************************************************************/ #include "isula_image_connect.h" -#include "securec.h" #include "utils.h" #include "grpc_isula_image_client.h" @@ -22,12 +21,7 @@ static isula_image_ops g_image_ops; int isula_image_ops_init(void) { - errno_t ret; - - ret = memset_s(&g_image_ops, sizeof(isula_image_ops), 0, sizeof(isula_image_ops)); - if (ret != EOK) { - return -1; - } + (void)memset(&g_image_ops, 0, sizeof(isula_image_ops)); return grpc_isula_image_client_ops_init(&g_image_ops); } diff --git a/src/connect/client/lcrc_connect.c b/src/connect/client/lcrc_connect.c index ad4d60c..b5111d0 100644 --- a/src/connect/client/lcrc_connect.c +++ b/src/connect/client/lcrc_connect.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "lcrc_connect.h" -#include "securec.h" #ifdef GRPC_CONNECTOR #include "grpc_client.h" @@ -27,11 +26,7 @@ static lcrc_connect_ops g_connect_ops; /* connect client ops init */ int connect_client_ops_init(void) { - errno_t ret; - ret = memset_s(&g_connect_ops, sizeof(g_connect_ops), 0, sizeof(g_connect_ops)); - if (ret != EOK) { - return -1; - } + (void)memset(&g_connect_ops, 0, sizeof(g_connect_ops)); #ifdef GRPC_CONNECTOR if (grpc_ops_init(&g_connect_ops)) { return -1; diff --git a/src/connect/client/rest/rest_containers_client.c b/src/connect/client/rest/rest_containers_client.c index 1e5b9bc..7c0c2a6 100644 --- a/src/connect/client/rest/rest_containers_client.c +++ b/src/connect/client/rest/rest_containers_client.c @@ -16,7 +16,6 @@ #include "error.h" #include "log.h" -#include "securec.h" #include "lcrc_connect.h" #include "container.rest.h" #include "pack_config.h" @@ -1087,10 +1086,7 @@ static int update_request_to_rest(const struct lcrc_update_request *lu_request, char *srcconfigjson = NULL; int ret = 0; - if (memset_s(&srcconfig, sizeof(srcconfig), 0, sizeof(srcconfig)) != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&srcconfig, 0, sizeof(srcconfig)); crequest = util_common_calloc_s(sizeof(container_update_request)); if (crequest == NULL) { diff --git a/src/connect/client/rest/rest_images_client.c b/src/connect/client/rest/rest_images_client.c index 3ae0862..0a01806 100644 --- a/src/connect/client/rest/rest_images_client.c +++ b/src/connect/client/rest/rest_images_client.c @@ -17,7 +17,6 @@ #include #include "log.h" -#include "securec.h" #include "lcrc_connect.h" #include "image.rest.h" #include "rest_common.h" diff --git a/src/connect/service/grpc/grpc_containers_service.cc b/src/connect/service/grpc/grpc_containers_service.cc index ae02546..5b9f28c 100644 --- a/src/connect/service/grpc/grpc_containers_service.cc +++ b/src/connect/service/grpc/grpc_containers_service.cc @@ -18,7 +18,6 @@ #include #include #include -#include "securec.h" #include "log.h" #include "utils.h" #include "error.h" @@ -118,11 +117,7 @@ static bool copy_to_container_data_from_grpc(struct lcrd_copy_to_container_data ERROR("Out of memory"); return false; } - if (memcpy_s(data, len, gcopy->data().c_str(), len) != EOK) { - ERROR("Can not copy memory"); - free(data); - return false; - } + (void)memcpy(data, gcopy->data().c_str(), len); copy->data = data; copy->data_len = len; return true; diff --git a/src/connect/service/grpc/grpc_images_service.cc b/src/connect/service/grpc/grpc_images_service.cc index f313140..a540b3b 100644 --- a/src/connect/service/grpc/grpc_images_service.cc +++ b/src/connect/service/grpc/grpc_images_service.cc @@ -20,7 +20,6 @@ #include #include #include -#include #include "log.h" #include "utils.h" diff --git a/src/connect/service/rest/rest_containers_service.c b/src/connect/service/rest/rest_containers_service.c index c3a259d..d392094 100644 --- a/src/connect/service/rest/rest_containers_service.c +++ b/src/connect/service/rest/rest_containers_service.c @@ -18,7 +18,6 @@ #include "log.h" #include "utils.h" #include "error.h" -#include "securec.h" #include "callback.h" #include "container.rest.h" #include "rest_service_common.h" diff --git a/src/connect/service/rest/rest_images_service.c b/src/connect/service/rest/rest_images_service.c index 55bb7e5..5a90740 100644 --- a/src/connect/service/rest/rest_images_service.c +++ b/src/connect/service/rest/rest_images_service.c @@ -15,7 +15,6 @@ #include #include "log.h" -#include "securec.h" #include "callback.h" #include "image.rest.h" #include "rest_service_common.h" diff --git a/src/connect/service/rest/rest_service.c b/src/connect/service/rest/rest_service.c index 3bc8ba6..511633f 100644 --- a/src/connect/service/rest/rest_service.c +++ b/src/connect/service/rest/rest_service.c @@ -16,10 +16,10 @@ #include #include #include +#include #include "log.h" #include "utils.h" -#include "securec.h" #include "rest_containers_service.h" #include "rest_images_service.h" diff --git a/src/connect/service/rest/rest_service_common.c b/src/connect/service/rest/rest_service_common.c index f8168b2..06d04c0 100644 --- a/src/connect/service/rest/rest_service_common.c +++ b/src/connect/service/rest/rest_service_common.c @@ -13,10 +13,11 @@ * Description: provide container restful service common functions ******************************************************************************/ #include +#include +#include #include "log.h" #include "utils.h" -#include "securec.h" #include "rest_service_common.h" #define UNIX_PATH_MAX 128 diff --git a/src/console/console.c b/src/console/console.c index 12c1f6b..31d975c 100644 --- a/src/console/console.c +++ b/src/console/console.c @@ -19,9 +19,10 @@ #include #include #include +#include +#include #include "console.h" -#include "securec.h" #include "mainloop.h" #include "log.h" #include "utils.h" @@ -185,8 +186,8 @@ int console_fifo_name(const char *rundir, const char *subpath, int ret = 0; int nret = 0; - nret = sprintf_s(fifo_path, fifo_path_sz, "%s/%s/", rundir, subpath); - if (nret < 0) { + nret = snprintf(fifo_path, fifo_path_sz, "%s/%s/", rundir, subpath); + if (nret < 0 || (size_t)nret >= fifo_path_sz) { ERROR("FIFO path:%s/%s/ is too long.", rundir, subpath); ret = -1; goto out; @@ -200,8 +201,8 @@ int console_fifo_name(const char *rundir, const char *subpath, } } - nret = sprintf_s(fifo_name, fifo_name_sz, "%s/%s/%s-fifo", rundir, subpath, stdflag); - if (nret < 0) { + nret = snprintf(fifo_name, fifo_name_sz, "%s/%s/%s-fifo", rundir, subpath, stdflag); + if (nret < 0 || (size_t)nret >= fifo_name_sz) { ERROR("FIFO name %s/%s/%s-fifo is too long.", rundir, subpath, stdflag); ret = -1; goto out; diff --git a/src/contrib/docker b/src/contrib/docker old mode 100644 new mode 100755 diff --git a/src/contrib/env_checkconfig b/src/contrib/env_checkconfig old mode 100644 new mode 100755 diff --git a/src/contrib/sysmonitor/isulad-check.sh b/src/contrib/sysmonitor/isulad-check.sh old mode 100644 new mode 100755 diff --git a/src/cutils/utils.c b/src/cutils/utils.c index b5bfc78..d62ca89 100644 --- a/src/cutils/utils.c +++ b/src/cutils/utils.c @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -37,7 +39,6 @@ #include #include #include -#include #include #include "utils.h" @@ -47,7 +48,6 @@ int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize) { void *tmp = NULL; - int nret = 0; if (newptr == NULL || newsize == 0) { goto err_out; @@ -60,18 +60,8 @@ int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize) } if (oldptr != NULL) { - nret = memcpy_s(tmp, newsize, oldptr, (newsize < oldsize) ? newsize : oldsize); - if (nret != EOK) { - ERROR("Failed to memcpy memory"); - free(tmp); - goto err_out; - } - - if (memset_s(oldptr, oldsize, 0, oldsize) != EOK) { - ERROR("Failed to memset memory"); - free(tmp); - goto err_out; - } + (void)memcpy(tmp, oldptr, (newsize < oldsize) ? newsize : oldsize); + (void)memset(oldptr, 0, oldsize); free(oldptr); } @@ -385,10 +375,7 @@ char *util_short_digest(const char *digest) start_pos = strlen(SHA256_PREFIX); } - if (memcpy_s(short_digest, sizeof(short_digest), digest + start_pos, SHORT_DIGEST_LEN) != EOK) { - ERROR("Failed to memcpy memory"); - return NULL; - } + (void)memcpy(short_digest, digest + start_pos, SHORT_DIGEST_LEN); short_digest[SHORT_DIGEST_LEN] = 0; @@ -405,8 +392,8 @@ char *util_full_digest(const char *digest) return NULL; } - nret = sprintf_s(full_digest, sizeof(full_digest), "%s%s", SHA256_PREFIX, digest); - if (nret < 0) { + nret = snprintf(full_digest, sizeof(full_digest), "%s%s", SHA256_PREFIX, digest); + if (nret < 0 || (size_t)nret >= sizeof(full_digest)) { ERROR("digest too long failed"); return NULL; } @@ -445,22 +432,22 @@ proc_t *util_stat2proc(const char *s, size_t len) /* parse these two strings separately, skipping the leading "(". */ /* comm[16] in kernel */ - num = sscanf_s(s, "%d (%15c", &p->pid, p->cmd, 16); + num = sscanf(s, "%d (%15c", &p->pid, p->cmd); if (num != 2) { ERROR("Call sscanf error: %s", errno ? strerror(errno) : ""); free(p); return NULL; } - num = sscanf_s(tmp + 2, /* skip space after ')' too */ - "%c " - "%d %d %d %d %d " - "%lu %lu %lu %lu %lu " - "%Lu %Lu %Lu %Lu " /* utime stime cutime cstime */ - "%ld %ld %ld %ld " - "%Lu ", /* start_time */ - &p->state, 1, &p->ppid, &p->pgrp, &p->session, &p->tty, &p->tpgid, &p->flags, &p->min_flt, - &p->cmin_flt, &p->maj_flt, &p->cmaj_flt, &p->utime, &p->stime, &p->cutime, &p->cstime, &p->priority, - &p->nice, &p->timeout, &p->it_real_value, &p->start_time); + num = sscanf(tmp + 2, /* skip space after ')' too */ + "%c " + "%d %d %d %d %d " + "%lu %lu %lu %lu %lu " + "%Lu %Lu %Lu %Lu " /* utime stime cutime cstime */ + "%ld %ld %ld %ld " + "%Lu ", /* start_time */ + &p->state, &p->ppid, &p->pgrp, &p->session, &p->tty, &p->tpgid, &p->flags, &p->min_flt, + &p->cmin_flt, &p->maj_flt, &p->cmaj_flt, &p->utime, &p->stime, &p->cutime, &p->cstime, &p->priority, + &p->nice, &p->timeout, &p->it_real_value, &p->start_time); if (num != 20) { // max arg to read ERROR("Call sscanf error: %s", errno ? strerror(errno) : ""); free(p); @@ -490,8 +477,8 @@ bool util_process_alive(pid_t pid, unsigned long long start_time) return false; } - sret = sprintf_s(filename, sizeof(filename), "/proc/%d/stat", pid); - if (sret < 0 || (unsigned int)sret >= sizeof(filename)) { + sret = snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); + if (sret < 0 || (size_t)sret >= sizeof(filename)) { ERROR("Failed to sprintf filename"); goto out; } @@ -519,7 +506,6 @@ out: static void set_stderr_buf(char **stderr_buf, const char *format, ...) { - int ret = 0; char errbuf[BUFSIZ + 1] = { 0 }; char *jerr = NULL; @@ -528,11 +514,8 @@ static void set_stderr_buf(char **stderr_buf, const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + (void)vsprintf(errbuf, format, argp); va_end(argp); - if (ret < 0) { - return; - } *stderr_buf = json_marshal_string(errbuf, strlen(errbuf), NULL, &jerr); if (*stderr_buf == NULL) { @@ -968,8 +951,8 @@ proc_t *util_get_process_proc_info(pid_t pid) char filename[PATH_MAX] = { 0 }; char sbuf[1024] = { 0 }; /* bufs for stat */ - sret = sprintf_s(filename, sizeof(filename), "/proc/%d/stat", pid); - if (sret < 0 || (unsigned int)sret >= sizeof(filename)) { + sret = snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); + if (sret < 0 || (size_t)sret >= sizeof(filename)) { ERROR("Failed to sprintf filename"); goto out; } @@ -1221,9 +1204,7 @@ void free_sensitive_string(char *str) goto out; } - if (memset_s(str, strlen(str), 0, strlen(str)) != EOK) { - ERROR("Failed to memset sensitive string memory"); - } + (void)memset(str, 0, strlen(str)); out: free(str); @@ -1235,9 +1216,7 @@ void memset_sensitive_string(char *str) return; } - if (memset_s(str, strlen(str), 0, strlen(str)) != EOK) { - ERROR("Failed to memset sensitive string memory"); - } + (void)memset(str, 0, strlen(str)); } static char *get_mtpoint(const char *line) @@ -1272,11 +1251,7 @@ static char *get_mtpoint(const char *line) if (sret == NULL) { goto err_out; } - if (memcpy_s(sret, len + 1, tmp, len) != EOK) { - free(sret); - sret = NULL; - goto err_out; - } + (void)memcpy(sret, tmp, len); sret[len] = '\0'; err_out: diff --git a/src/cutils/utils_array.c b/src/cutils/utils_array.c index 1c54336..5605956 100644 --- a/src/cutils/utils_array.c +++ b/src/cutils/utils_array.c @@ -15,9 +15,8 @@ #define _GNU_SOURCE #include "utils_array.h" - -#include "securec.h" - +#include +#include #include "log.h" #include "utils.h" @@ -80,12 +79,7 @@ int util_array_append(char ***array, const char *element) return -1; } if (*array != NULL) { - 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 *)); UTIL_FREE_AND_SET_NULL(*array); } *array = new_array; @@ -122,13 +116,8 @@ int util_grow_array(char ***orig_array, size_t *orig_capacity, size_t size, if (add_array == NULL) { 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; - } + if (*orig_array != NULL) { + (void)memcpy(add_array, *orig_array, *orig_capacity * sizeof(void *)); UTIL_FREE_AND_SET_NULL(*orig_array); } diff --git a/src/cutils/utils_file.c b/src/cutils/utils_file.c index ed774b0..ff3886d 100644 --- a/src/cutils/utils_file.c +++ b/src/cutils/utils_file.c @@ -18,13 +18,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include "securec.h" #include "constants.h" #include "log.h" @@ -218,24 +218,16 @@ static int recursive_rmdir_helper(const char *dirpath, int recursive_depth, int pdirent = readdir(directory); for (; pdirent != NULL; pdirent = readdir(directory)) { struct stat fstat; - int pathname; + int pathname_len; - bool ret = !strcmp(pdirent->d_name, ".") || !strcmp(pdirent->d_name, ".."); - - if (ret) { + if (!strcmp(pdirent->d_name, ".") || !strcmp(pdirent->d_name, "..")) { continue; } - nret = memset_s(fname, sizeof(fname), 0, sizeof(fname)); - if (nret != EOK) { - ERROR("Failed to memset memory"); - failure = 1; - continue; - } - pathname = sprintf_s(fname, MAXPATHLEN, "%s/%s", dirpath, pdirent->d_name); + (void)memset(fname, 0, sizeof(fname)); - ret = pathname < 0 || pathname >= MAXPATHLEN; - if (ret) { + pathname_len = snprintf(fname, MAXPATHLEN, "%s/%s", dirpath, pdirent->d_name); + if (pathname_len < 0 || pathname_len >= MAXPATHLEN) { ERROR("Pathname too long"); failure = 1; continue; @@ -299,8 +291,8 @@ char *util_path_join(const char *dir, const char *file) return NULL; } - nret = sprintf_s(path, sizeof(path), "%s/%s", dir, file); - if (nret < 0) { + nret = snprintf(path, PATH_MAX, "%s/%s", dir, file); + if (nret < 0 || nret >= PATH_MAX) { ERROR("dir or file too long failed"); return NULL; } @@ -420,8 +412,8 @@ char *util_human_size(uint64_t val) return NULL; } - ret = sprintf_s(out, len, "%llu%s", (unsigned long long)ui, uf[index]); - if (ret < 0) { + ret = snprintf(out, len, "%llu%s", (unsigned long long)ui, uf[index]); + if (ret < 0 || ret >= len) { ERROR("Failed to print string"); free(out); return NULL; @@ -439,15 +431,15 @@ char *util_human_size_decimal(int64_t val) char out[16] = { 0 }; /* 16 is enough, format like: 123.456 MB */ if (val >= gb) { - nret = sprintf_s(out, sizeof(out), "%.3lf GB", ((double)val / gb)); + nret = snprintf(out, sizeof(out), "%.3lf GB", ((double)val / gb)); } else if (val >= mb) { - nret = sprintf_s(out, sizeof(out), "%.3lf MB", ((double)val / mb)); + nret = snprintf(out, sizeof(out), "%.3lf MB", ((double)val / mb)); } else if (val >= kb) { - nret = sprintf_s(out, sizeof(out), "%.3lf KB", ((double)val / kb)); + nret = snprintf(out, sizeof(out), "%.3lf KB", ((double)val / kb)); } else { - nret = sprintf_s(out, sizeof(out), "%lld B", (long long int)val); + nret = snprintf(out, sizeof(out), "%lld B", (long long int)val); } - if (nret < 0) { + if (nret < 0 || nret >= sizeof(out)) { ERROR("Failed to print string"); return NULL; } @@ -705,8 +697,8 @@ int util_list_all_subdir(const char *directory, char ***out) continue; } - nret = sprintf_s(tmpdir, PATH_MAX, "%s/%s", directory, direntp->d_name); - if (nret < 0) { + nret = snprintf(tmpdir, PATH_MAX, "%s/%s", directory, direntp->d_name); + if (nret < 0 || nret >= PATH_MAX) { ERROR("Sprintf: %s failed", direntp->d_name); goto error_out; } diff --git a/src/cutils/utils_string.c b/src/cutils/utils_string.c index a33d4eb..953f707 100644 --- a/src/cutils/utils_string.c +++ b/src/cutils/utils_string.c @@ -15,10 +15,9 @@ #define _GNU_SOURCE #include "utils_string.h" - #include -#include "securec.h" - +#include +#include #include "utils.h" #include "log.h" @@ -576,19 +575,10 @@ static char *do_string_join(const char *sep, const char **parts, size_t parts_le } 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; } @@ -641,14 +631,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; } @@ -695,16 +679,12 @@ char *util_sub_string(const char *source, size_t offset, size_t length) total_len = strlen(source); substr_len = ((total_len - offset) >= length ? length : (total_len - offset)) + 1; - substring = (char *)malloc(substr_len * sizeof(char)); + substring = (char *)util_common_calloc_s(substr_len * sizeof(char)); if (substring == NULL) { ERROR("Out of memory\n"); return NULL; } - if (strncpy_s(substring, substr_len, source + offset, substr_len - 1) != EOK) { - ERROR("Out of memory\n"); - free(substring); - return NULL; - } + (void)strncpy(substring, source + offset, substr_len - 1); substring[substr_len - 1] = '\0'; return substring; diff --git a/src/cutils/utils_verify.c b/src/cutils/utils_verify.c index 4b9725f..ccd7eb1 100644 --- a/src/cutils/utils_verify.c +++ b/src/cutils/utils_verify.c @@ -15,14 +15,14 @@ #define _GNU_SOURCE #include "utils_verify.h" +#include +#include #include #include #ifdef HAVE_LIBCAP_H #include #endif -#include "securec.h" - #include "log.h" #include "utils.h" #include "utils_regex.h" @@ -111,10 +111,7 @@ int util_validate_absolute_path(const char *path) return -1; } - if (memset_s(®match, sizeof(regmatch_t), 0, sizeof(regmatch_t)) != EOK) { - WARN("Failed to set memory!"); - return -1; - } + (void)memset(®match, 0, sizeof(regmatch_t)); if (regcomp(&preg, "^(/[^/ ]*)+/?$", REG_NOSUB | REG_EXTENDED)) { ERROR("Failed to compile the regex"); @@ -217,8 +214,8 @@ bool util_valid_cap(const char *cap) return false; } - nret = sprintf_s(tmpcap, sizeof(tmpcap), "CAP_%s", cap); - if (nret < 0) { + nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", cap); + if (nret < 0 || nret >= sizeof(tmpcap)) { ERROR("Failed to print string"); cret = false; goto err_out; diff --git a/src/engines/engine.c b/src/engines/engine.c index 59eb07e..a7bc132 100644 --- a/src/engines/engine.c +++ b/src/engines/engine.c @@ -23,7 +23,6 @@ #include "constants.h" #include "linked_list.h" #include "lcrd_config.h" -#include "securec.h" #include "log.h" #include "utils.h" #include "lcr_engine.h" @@ -42,15 +41,8 @@ typedef int (*engine_init_func_t)(struct engine_operation *ops); int engines_global_init() { int ret = 0; - errno_t mret = EOK; - mret = memset_s(&g_lcrd_engines_lists, sizeof(struct lcrd_engine_operation_lists), 0x00, - sizeof(struct lcrd_engine_operation_lists)); - if (mret != EOK) { - ERROR("Failed to set memory"); - ret = -1; - goto out; - } + (void)memset(&g_lcrd_engines_lists, 0, sizeof(struct lcrd_engine_operation_lists)); /* init lcrd_engines_op_rwlock */ ret = pthread_rwlock_init(&g_lcrd_engines_lists.lcrd_engines_op_rwlock, NULL); diff --git a/src/engines/lcr/lcr_engine.c b/src/engines/lcr/lcr_engine.c index 6ec25a3..083ec31 100644 --- a/src/engines/lcr/lcr_engine.c +++ b/src/engines/lcr/lcr_engine.c @@ -23,7 +23,6 @@ #include #include "error.h" -#include "securec.h" #include "engine.h" #include "log.h" #include "lcrd_config.h" @@ -80,7 +79,6 @@ static Engine_Container_Status lcrsta2sta(const char *state) static bool lcr_update_container(const char *name, const char *lcrpath, const struct engine_cgroup_resources *cr) { struct lcr_cgroup_resources lcr_cr; - errno_t ret = EOK; if (g_lcr_update_op == NULL) { ERROR("Not supported update operation"); @@ -92,11 +90,7 @@ static bool lcr_update_container(const char *name, const char *lcrpath, const st return false; } - ret = memset_s(&lcr_cr, sizeof(struct lcr_cgroup_resources), 0x00, sizeof(struct lcr_cgroup_resources)); - if (ret != EOK) { - ERROR("Failed to set memory"); - return false; - } + (void)memset(&lcr_cr, 0, sizeof(struct lcr_cgroup_resources)); lcr_cr.blkio_weight = cr->blkio_weight; lcr_cr.cpu_shares = cr->cpu_shares; @@ -146,18 +140,13 @@ bool get_console_config(const char *name, const char *lcrpath, struct engine_con { struct lcr_console_config lcr_config; bool ret = false; - errno_t mret = EOK; if (name == NULL || config == NULL) { ERROR("Invalid arguments"); return ret; } - mret = memset_s(&lcr_config, sizeof(struct lcr_console_config), 0x00, sizeof(struct lcr_console_config)); - if (mret != EOK) { - ERROR("Failed to set memory"); - return ret; - } + (void)memset(&lcr_config, 0, sizeof(struct lcr_console_config)); if (g_lcr_get_console_config_op != NULL) { ret = g_lcr_get_console_config_op(name, lcrpath, &lcr_config); @@ -215,9 +204,7 @@ static void copy_container_status(const struct lcr_container_state *lcs, struct const char *defvalue = "-"; const char *name = NULL; - if (memset_s(status, sizeof(struct engine_container_info), 0, sizeof(struct engine_container_info)) != EOK) { - WARN("Can not set memory"); - } + (void)memset(status, 0, sizeof(struct engine_container_info)); name = lcs->name ? lcs->name : defvalue; status->id = util_strdup_s(name); diff --git a/src/error.h b/src/error.h index 8605d9c..0802a45 100644 --- a/src/error.h +++ b/src/error.h @@ -16,7 +16,7 @@ #define __LCRD_ERROR_H_ #include -#include +#include #include "utils.h" #ifdef __cplusplus @@ -60,9 +60,9 @@ static inline void format_errorf(char **err, const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsnprintf(errbuf, BUFSIZ, format, argp); va_end(argp); - if (ret < 0) { + if (ret < 0 || ret >= BUFSIZ) { *err = util_strdup_s("Error is too long!!!"); return; } diff --git a/src/http/buffer.c b/src/http/buffer.c index be54053..7489f8d 100644 --- a/src/http/buffer.c +++ b/src/http/buffer.c @@ -14,8 +14,8 @@ ******************************************************************************/ #define _GNU_SOURCE #include "buffer.h" -#include +#include #include "log.h" #include "utils.h" @@ -71,16 +71,10 @@ void buffer_free(Buffer *buf) /* buffer empty */ void buffer_empty(Buffer *buf) { - errno_t ret = EOK; - if (buf == NULL) { return; } - ret = memset_s(buf->contents, buf->total_size, 0x00, buf->total_size); - if (ret != EOK) { - ERROR("Failed to set memory"); - return; - } + (void)memset(buf->contents, 0, buf->total_size); buf->bytes_used = 0; } @@ -89,9 +83,7 @@ void buffer_empty(Buffer *buf) int buffer_grow(Buffer *buffer, size_t min_size) { size_t factor = 0; - int ret; size_t new_size = 0; - errno_t mret = EOK; char *tmp = NULL; if (buffer == NULL) { @@ -116,19 +108,9 @@ int buffer_grow(Buffer *buffer, size_t min_size) return -1; } - ret = memcpy_s(tmp, new_size, buffer->contents, buffer->total_size); - if (ret) { - ERROR("Failed to copy memory"); - free(tmp); - return -1; - } + (void)memcpy(tmp, buffer->contents, buffer->total_size); - mret = memset_s(buffer->contents, buffer->total_size, 0, buffer->total_size); - if (mret != EOK) { - ERROR("Failed to set memory"); - free(tmp); - return -1; - } + (void)memset(buffer->contents, 0, buffer->total_size); free(buffer->contents); buffer->contents = tmp; diff --git a/src/http/certificate.c b/src/http/certificate.c index bb18207..5d71ef8 100644 --- a/src/http/certificate.c +++ b/src/http/certificate.c @@ -15,10 +15,10 @@ #include "certificate.h" #include #include +#include #include "log.h" #include "utils.h" -#include "securec.h" int get_common_name_from_tls_cert(const char *cert_path, char *value, size_t len) { diff --git a/src/http/http.c b/src/http/http.c index 81b17bd..04a608a 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -20,7 +20,6 @@ #include "buffer.h" #include "log.h" #include "utils.h" -#include "securec.h" size_t fwrite_buffer(const char *ptr, size_t eltsize, size_t nmemb, void *buffer_) { @@ -96,8 +95,8 @@ struct curl_slist *http_get_chunk_header(const struct http_get_options *options) ret = -1; goto out; } - nret = sprintf_s(header, len, "Authorization: %s", options->authorization); - if (nret < 0) { + nret = snprintf(header, len, "Authorization: %s", options->authorization); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to print string"); ret = -1; goto out; @@ -126,8 +125,8 @@ struct curl_slist *http_get_chunk_header(const struct http_get_options *options) ret = -1; goto out; } - nret = sprintf_s(header, len, "Accept: %s", options->accepts); - if (nret < 0) { + nret = snprintf(header, len, "Accept: %s", options->accepts); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to print string"); ret = -1; goto out; @@ -365,7 +364,8 @@ int authz_http_request(const char *username, const char *action, char **resp) *resp = util_strdup_s("Inernal server error: Out of memory"); return -1; } - if (sprintf_s(request_body, length, "%s:%s", username, action) < 0) { + int nret = snprintf(request_body, length, "%s:%s", username, action); + if (nret < 0 || (size_t)nret >= length) { ERROR("Failed to print string"); free(request_body); return -1; @@ -392,8 +392,8 @@ int authz_http_request(const char *username, const char *action, char **resp) goto out; } if (response_code != StatusOK) { - ret = sprintf_s(err_msg, sizeof(err_msg), "action '%s' for user '%s': permission denied", action, username); - if (ret < 0) { + ret = snprintf(err_msg, sizeof(err_msg), "action '%s' for user '%s': permission denied", action, username); + if (ret < 0 || (size_t)ret >= sizeof(err_msg)) { ERROR("Out of memory"); *resp = util_strdup_s("Inernal server error: Out of memory"); goto out; diff --git a/src/http/parser.c b/src/http/parser.c index 247c4d2..c388c6a 100644 --- a/src/http/parser.c +++ b/src/http/parser.c @@ -13,7 +13,8 @@ * Description: provide container parser functions ******************************************************************************/ #include -#include +#include +#include #include "parser.h" #include "utils.h" @@ -29,11 +30,7 @@ size_t strlncat(char *dststr, size_t size, const char *srcstr, size_t nsize) if (dsize < size) { size_t rsize = size - dsize; size_t ncpy = ssize < rsize ? ssize : (rsize - 1); - errno_t nret = memcpy_s(dststr + dsize, size - dsize, srcstr, ncpy); - if (nret != EOK) { - ERROR("Fail at strlncat memcpy!"); - return 0; - } + (void)memcpy(dststr + dsize, srcstr, ncpy); dststr[dsize + ncpy] = '\0'; } @@ -109,11 +106,7 @@ static int parser_body_cb(http_parser *parser, const char *buf, size_t len) return -1; } if (m->body != NULL && m->body_size > 0) { - if (memcpy_s(body, newsize, m->body, m->body_size) != EOK) { - ERROR("Failed to copy memory"); - free(body); - return -1; - } + (void)memcpy(body, m->body, m->body_size); free(m->body); } diff --git a/src/http/rest_common.c b/src/http/rest_common.c index 8d53255..f195ecc 100644 --- a/src/http/rest_common.c +++ b/src/http/rest_common.c @@ -14,7 +14,7 @@ ******************************************************************************/ #include "rest_common.h" #include -#include "securec.h" +#include #include "log.h" #include "utils.h" @@ -72,8 +72,6 @@ int check_status_code(int status_code) /* free httpclient ops */ static void free_httpclient_ops(struct httpclient_ops *ops) { - errno_t rc = EOK; - if (ops == NULL || ops->handle == NULL) { return; } @@ -81,10 +79,7 @@ static void free_httpclient_ops(struct httpclient_ops *ops) return; } dlclose(ops->handle); - rc = memset_s(ops, sizeof(struct httpclient_ops), 0, sizeof(struct httpclient_ops)); - if (rc != EOK) { - ERROR("Failed to set memory!"); - } + (void)memset(ops, 0, sizeof(struct httpclient_ops)); } /* ops init */ @@ -92,16 +87,11 @@ static int ops_init(struct httpclient_ops *ops) { void *handle = NULL; int ret = -1; - errno_t rc = EOK; if (ops == NULL) { return ret; } - rc = memset_s(ops, sizeof(struct httpclient_ops), 0, sizeof(struct httpclient_ops)); - if (rc != EOK) { - ERROR("Failed to set memory!"); - goto out; - } + (void)memset(ops, 0, sizeof(struct httpclient_ops)); handle = dlopen("libhttpclient.so", RTLD_LAZY); if (handle == NULL) { COMMAND_ERROR("Dlopen libhttpclient: %s", dlerror()); diff --git a/src/image/embedded/db/db_all.c b/src/image/embedded/db/db_all.c index e2349ae..23f106f 100644 --- a/src/image/embedded/db/db_all.c +++ b/src/image/embedded/db/db_all.c @@ -14,7 +14,7 @@ ******************************************************************************/ #include #include -#include "securec.h" +#include #include "log.h" #include "utils.h" #include "db_common.h" diff --git a/src/image/embedded/db/sqlite_common.c b/src/image/embedded/db/sqlite_common.c index f37dca9..482f36b 100644 --- a/src/image/embedded/db/sqlite_common.c +++ b/src/image/embedded/db/sqlite_common.c @@ -16,7 +16,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "constants.h" @@ -158,8 +157,8 @@ int db_common_init(const char *rootpath) char dbpath[PATH_MAX] = { 0 }; bool retry = true; - nret = sprintf_s(dbpath, sizeof(dbpath), "%s/%s", rootpath, DBNAME); - if (nret < 0) { + nret = snprintf(dbpath, sizeof(dbpath), "%s/%s", rootpath, DBNAME); + if (nret < 0 || (size_t)nret >= sizeof(dbpath)) { ERROR("Failed to print string"); return -1; } diff --git a/src/image/embedded/embedded_config_merge.c b/src/image/embedded/embedded_config_merge.c index c118dbf..dcf7037 100644 --- a/src/image/embedded/embedded_config_merge.c +++ b/src/image/embedded/embedded_config_merge.c @@ -19,7 +19,6 @@ #include #include -#include "securec.h" #include "utils.h" #include "log.h" #include "liblcrd.h" @@ -141,15 +140,15 @@ int gen_one_mount(embedded_manifest *manifest, char *mount, char *real_path, int return -1; } if (strcmp(manifest->layers[i]->media_type, MediaTypeEmbeddedLayerSquashfs) == 0) { - nret = sprintf_s(mount, PATH_MAX * 3, - "type=squashfs,ro=true,src=%s,dst=%s", - real_path, manifest->layers[i]->path_in_container); + nret = snprintf(mount, PATH_MAX * 3, + "type=squashfs,ro=true,src=%s,dst=%s", + real_path, manifest->layers[i]->path_in_container); } else { - nret = sprintf_s(mount, PATH_MAX * 3, - "type=bind,ro=true,bind-propagation=rprivate,src=%s,dst=%s", - real_path, manifest->layers[i]->path_in_container); + nret = snprintf(mount, PATH_MAX * 3, + "type=bind,ro=true,bind-propagation=rprivate,src=%s,dst=%s", + real_path, manifest->layers[i]->path_in_container); } - if (nret < 0) { + if (nret < 0 || (size_t)nret >= (PATH_MAX * 3)) { ERROR("print string for mounts failed"); return -1; } diff --git a/src/image/embedded/embedded_image.c b/src/image/embedded/embedded_image.c index 092820a..28c9f08 100644 --- a/src/image/embedded/embedded_image.c +++ b/src/image/embedded/embedded_image.c @@ -24,7 +24,6 @@ #include "containers_store.h" #include "specs_extend.h" #include "log.h" -#include "securec.h" #include "embedded_image.h" #include "lim.h" #include "embedded_config_merge.h" diff --git a/src/image/embedded/lim.c b/src/image/embedded/lim.c index 2edb05a..47dd41c 100644 --- a/src/image/embedded/lim.c +++ b/src/image/embedded/lim.c @@ -20,7 +20,6 @@ #include "log.h" #include "lim.h" #include "liblcrd.h" -#include "securec.h" #include "mediatype.h" #include "snapshot.h" #include "snapshot_def.h" @@ -241,8 +240,8 @@ static bool validate_layer_path_in_host(size_t layer_index, const char *location return false; } abs_path = util_add_path(location, path_in_host); - sret = sprintf_s(parent_location, sizeof(parent_location), "%s/..", location); - if (sret < 0 || sret >= (int)sizeof(parent_location)) { + sret = snprintf(parent_location, sizeof(parent_location), "%s/..", location); + if (sret < 0 || (size_t)sret >= sizeof(parent_location)) { ERROR("Failed to sprintf parent_location"); lcrd_try_set_error_message("Failed to sprintf parent_location"); UTIL_FREE_AND_SET_NULL(abs_path); diff --git a/src/image/embedded/load.c b/src/image/embedded/load.c index 33e4071..2f65c3f 100644 --- a/src/image/embedded/load.c +++ b/src/image/embedded/load.c @@ -21,7 +21,6 @@ #include "lim.h" #include "limits.h" #include "log.h" -#include "securec.h" #include "image.h" #define RAW_DIGEST_LEN 64 @@ -48,11 +47,7 @@ static char *replace_suffix_to_sgn(const char *file) } /* dump chars to sgn_file */ - if (strcat_s(sgn_file, len, file) != EOK) { - ERROR("strcat string failed"); - free(sgn_file); - return NULL; - } + (void)strcat(sgn_file, file); /* strip file's suffix */ for (i = strlen(sgn_file); i > 0; i--) { @@ -67,11 +62,7 @@ static char *replace_suffix_to_sgn(const char *file) } /* add .sgn to tail as suffix */ - if (strcat_s(sgn_file, len, ".sgn") != EOK) { - ERROR("strcat string failed"); - free(sgn_file); - return NULL; - } + (void)strcat(sgn_file, ".sgn"); return sgn_file; } diff --git a/src/image/embedded/snapshot/embedded.c b/src/image/embedded/snapshot/embedded.c index e1bca33..b29c6ad 100644 --- a/src/image/embedded/snapshot/embedded.c +++ b/src/image/embedded/snapshot/embedded.c @@ -17,7 +17,6 @@ #include "utils.h" #include "linked_list.h" #include "log.h" -#include "securec.h" #include "snapshot_def.h" #include "embedded.h" diff --git a/src/image/external/ext_image.c b/src/image/external/ext_image.c index 049f74a..7eb07ab 100644 --- a/src/image/external/ext_image.c +++ b/src/image/external/ext_image.c @@ -24,7 +24,6 @@ #include "log.h" #include "utils.h" #include "specs_extend.h" -#include "securec.h" #include "ext_image.h" #ifdef ENABLE_OCI_IMAGE diff --git a/src/image/image.c b/src/image/image.c index e7e270f..180dec1 100644 --- a/src/image/image.c +++ b/src/image/image.c @@ -21,7 +21,6 @@ #include #include -#include "securec.h" #include "image.h" #include "liblcrd.h" #include "log.h" @@ -1684,8 +1683,8 @@ int map_to_key_value_string(const json_map_string_string *map, char ***array, si ERROR("Out of memory"); goto cleanup; } - ret = sprintf_s(str, len, "%s=%s", map->keys[i], map->values[i]); - if (ret < 0) { + ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]); + if (ret < 0 || (size_t)ret >= len) { ERROR("Failed to print string"); free(str); goto cleanup; diff --git a/src/image/oci/global_config.c b/src/image/oci/global_config.c index ad18bff..65e817e 100644 --- a/src/image/oci/global_config.c +++ b/src/image/oci/global_config.c @@ -17,7 +17,6 @@ #include #include "lcrd_config.h" -#include "securec.h" #include "log.h" #ifdef ENABLE_OCI_IMAGE #include "driver.h" @@ -198,7 +197,8 @@ static int pack_global_opt_time(const char * const *options, char *params[], siz opt_timeout = conf_get_im_opt_timeout(); if (opt_timeout != 0) { add_array_elem(params, PARAM_NUM, &i, options[GB_OPTION_OPT_TIMEOUT]); - if (sprintf_s(timeout_str, UINT_LEN, "%us", opt_timeout) < 0) { + int nret = snprintf(timeout_str, UINT_LEN + 2, "%us", opt_timeout); + if (nret < 0 || (size_t)nret >= (UINT_LEN + 2)) { COMMAND_ERROR("Failed to print string"); goto out; } diff --git a/src/image/oci/isula_image_pull.c b/src/image/oci/isula_image_pull.c index bc6e0ef..bdf50e0 100644 --- a/src/image/oci/isula_image_pull.c +++ b/src/image/oci/isula_image_pull.c @@ -20,6 +20,7 @@ #include "isula_helper.h" #include "connect.h" #include "oci_images_store.h" +#include "oci_common_operators.h" static bool need_new_isula_auth(const im_pull_request *request) { @@ -107,6 +108,7 @@ int isula_pull_image(const im_pull_request *request, im_pull_response **response struct isula_pull_response *iresp = NULL; int ret = -1; client_connect_config_t conf = { 0 }; + char *normalized = NULL; im_ops = get_isula_image_ops(); if (im_ops == NULL) { @@ -149,9 +151,16 @@ int isula_pull_image(const im_pull_request *request, im_pull_response **response goto err_out; } - ret = register_new_oci_image_into_memory(request->image); + normalized = oci_normalize_image_name(request->image); + if (normalized == NULL) { + ret = -1; + ERROR("Normalize image name %s failed", request->image); + goto err_out; + } + + ret = register_new_oci_image_into_memory(normalized); if (ret != 0) { - ERROR("Register image %s into store failed", request->image); + ERROR("Register image %s into store failed", normalized); goto err_out; } @@ -161,6 +170,7 @@ err_out: *response = NULL; ret = -1; out: + free(normalized); free_client_connect_config_value(&conf); free_isula_pull_request(ireq); free_isula_pull_response(iresp); diff --git a/src/image/oci/isula_images_list.c b/src/image/oci/isula_images_list.c index 0a3d86b..b9cf8b0 100644 --- a/src/image/oci/isula_images_list.c +++ b/src/image/oci/isula_images_list.c @@ -99,18 +99,21 @@ err_out: return -1; } -static void pack_imagetool_images_list(const struct isula_list_response *resp, imagetool_images_list **images) +static int pack_imagetool_images_list(const struct isula_list_response *resp, imagetool_images_list **images) { + int ret = 0; size_t i = 0; imagetool_images_list *tmp_list = NULL; if (resp == NULL) { - return; + ret = -1; + goto err_out; } tmp_list = (imagetool_images_list *)util_common_calloc_s(sizeof(imagetool_images_list)); if (tmp_list == NULL) { ERROR("Out of memory"); - return; + ret = -1; + goto err_out; } if (resp->images_len == 0) { DEBUG("Get number of images is 0"); @@ -119,7 +122,8 @@ static void pack_imagetool_images_list(const struct isula_list_response *resp, i tmp_list->images = (imagetool_image **)util_common_calloc_s(sizeof(imagetool_image *) * resp->images_len); if (tmp_list->images == NULL) { ERROR("Out of memory"); - goto out; + ret = -1; + goto err_out; } for (; i < resp->images_len; i++) { if (pack_imagetool_image(resp->images[i], &(tmp_list->images[i])) != 0) { @@ -128,8 +132,14 @@ static void pack_imagetool_images_list(const struct isula_list_response *resp, i tmp_list->images_len++; } + goto out; + +err_out: + free(tmp_list); + tmp_list = NULL; out: *images = tmp_list; + return ret; } int isula_list_images(const im_list_request *request, imagetool_images_list **images) @@ -175,7 +185,11 @@ int isula_list_images(const im_list_request *request, imagetool_images_list **im goto out; } - pack_imagetool_images_list(iresp, images); + if (pack_imagetool_images_list(iresp, images) != 0) { + ERROR("Failed to pack images list"); + ret = -1; + goto out; + } out: free_isula_list_request(ireq); free_isula_list_response(iresp); diff --git a/src/image/oci/oci_auth.c b/src/image/oci/oci_auth.c index a9c9bfb..16b63ec 100644 --- a/src/image/oci/oci_auth.c +++ b/src/image/oci/oci_auth.c @@ -19,7 +19,6 @@ #include #include -#include "securec.h" #include "utils.h" #include "log.h" #include "imagetool_auth_input.h" diff --git a/src/image/oci/oci_common_operators.c b/src/image/oci/oci_common_operators.c index 73f0bee..730682d 100644 --- a/src/image/oci/oci_common_operators.c +++ b/src/image/oci/oci_common_operators.c @@ -23,7 +23,6 @@ #include "log.h" #include "utils.h" -#include "securec.h" #include "oci_images_store.h" #include "specs_extend.h" #include "oci_config_merge.h" @@ -169,7 +168,8 @@ char *oci_normalize_image_name(const char *name) util_free_array(parts); // Normalize image name - if (sprintf_s(temp, sizeof(temp), "%s%s%s%s", add_dockerio, add_library, name, add_default_tag) < 0) { + int nret = snprintf(temp, sizeof(temp), "%s%s%s%s", add_dockerio, add_library, name, add_default_tag); + if (nret < 0 || (size_t)nret >= sizeof(temp)) { ERROR("sprint temp image name failed"); return NULL; } @@ -196,7 +196,8 @@ static char *oci_strip_dockerio_prefix(const char *name) return NULL; } - if (sprintf_s(prefix, sizeof(prefix), "%s%s", DEFAULT_HOSTNAME, DEFAULT_REPO_PREFIX) < 0) { + int nret = snprintf(prefix, sizeof(prefix), "%s%s", DEFAULT_HOSTNAME, DEFAULT_REPO_PREFIX); + if (nret < 0 || (size_t)nret >= sizeof(prefix)) { ERROR("sprint prefix prefix failed"); return NULL; } @@ -756,11 +757,8 @@ int oci_inspect_image(const im_inspect_request *im_request, char **inspected_jso return -1; } - ret = memset_s(&request, sizeof(im_status_request), 0x00, sizeof(im_status_request)); - if (ret != EOK) { - ERROR("Failed to set memory"); - return -1; - } + (void)memset(&request, 0, sizeof(im_status_request)); + request.image.image = im_request->image.image; response = (im_status_response *)util_common_calloc_s(sizeof(im_status_response)); diff --git a/src/image/oci/oci_config_merge.c b/src/image/oci/oci_config_merge.c index 957a2da..53392f5 100644 --- a/src/image/oci/oci_config_merge.c +++ b/src/image/oci/oci_config_merge.c @@ -19,7 +19,6 @@ #include #include #include -#include "securec.h" #include "utils.h" #include "log.h" #include "liblcrd.h" diff --git a/src/image/oci/run_image_server.c b/src/image/oci/run_image_server.c index 744fb17..41afa1f 100644 --- a/src/image/oci/run_image_server.c +++ b/src/image/oci/run_image_server.c @@ -169,8 +169,8 @@ static unsigned long long get_image_server_start_time(pid_t server_pid) return 0; } - sret = sprintf_s(filename, sizeof(filename), "/proc/%d/stat", server_pid); - if (sret < 0 || (unsigned int)sret >= sizeof(filename)) { + sret = snprintf(filename, sizeof(filename), "/proc/%d/stat", server_pid); + if (sret < 0 || (size_t)sret >= sizeof(filename)) { ERROR("Failed to sprintf filename"); goto out; } diff --git a/src/json/schema/schema/container/config-v2.json b/src/json/schema/schema/container/config-v2.json index 66a79a3..173886b 100644 --- a/src/json/schema/schema/container/config-v2.json +++ b/src/json/schema/schema/container/config-v2.json @@ -99,6 +99,9 @@ ], "type": "object" }, + "Image": { + "type": "string" + }, "State": { "properties": { "Dead": { diff --git a/src/json/schema/src/common_c.py b/src/json/schema/src/common_c.py index 0279293..ca259ee 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 5f4ed68..d828e19 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 beecd91..ee3604a 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); } @@ -149,4 +140,3 @@ char *read_file(const char *path, size_t *length) (void)fclose(fp); return buf; } - diff --git a/src/json/schema/src/read_file.h b/src/json/schema/src/read_file.h index af57f5b..cc14940 100644 --- a/src/json/schema/src/read_file.h +++ b/src/json/schema/src/read_file.h @@ -23,4 +23,3 @@ char *fread_file(FILE *stream, size_t *length); char *read_file(const char *path, size_t *length); #endif - diff --git a/src/json/schema/src/sources.py b/src/json/schema/src/sources.py index 2cb5982..7d7b7d5 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/liblcrc.c b/src/liblcrc.c index 9987058..12ec385 100644 --- a/src/liblcrc.c +++ b/src/liblcrc.c @@ -21,7 +21,6 @@ #include "log.h" #include "pack_config.h" #include "utils.h" -#include "securec.h" /* lcrc filters free */ void lcrc_filters_free(struct lcrc_filters *filters) diff --git a/src/liblcrd.c b/src/liblcrd.c index 4b9f018..83aa7f6 100644 --- a/src/liblcrd.c +++ b/src/liblcrd.c @@ -16,12 +16,12 @@ #include #include #include +#include #include "liblcrd.h" #include "log.h" #include "pack_config.h" #include "utils.h" -#include "securec.h" // record the errno __thread char *g_lcrd_errmsg = NULL; @@ -107,9 +107,9 @@ void lcrd_set_error_message(const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsnprintf(errbuf, BUFSIZ, format, argp); va_end(argp); - if (ret < 0) { + if (ret < 0 || ret >= BUFSIZ) { return; } @@ -129,9 +129,9 @@ void lcrd_try_set_error_message(const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsnprintf(errbuf, BUFSIZ, format, argp); va_end(argp); - if (ret < 0) { + if (ret < 0 || ret >= BUFSIZ) { return; } @@ -148,9 +148,9 @@ void lcrd_append_error_message(const char *format, ...) va_list argp; va_start(argp, format); - ret = vsprintf_s(errbuf, BUFSIZ, format, argp); + ret = vsnprintf(errbuf, BUFSIZ, format, argp); va_end(argp); - if (ret < 0) { + if (ret < 0 || ret >= BUFSIZ) { return; } result = util_string_append(g_lcrd_errmsg, errbuf); diff --git a/src/log.c b/src/log.c index a71bd63..4b4a369 100644 --- a/src/log.c +++ b/src/log.c @@ -25,7 +25,6 @@ #include #include #include -#include "securec.h" #include "utils.h" @@ -244,11 +243,11 @@ int log_append(const struct log_event *event, const char *format, ...) struct timespec timestamp; va_start(args, format); - rc = vsnprintf_truncated_s(msg, MAX_MSG_LENGTH, format, args); + rc = vsnprintf(msg, MAX_MSG_LENGTH, format, args); va_end(args); - if (rc < 0 || rc >= MAX_MSG_LENGTH) { - rc = sprintf_s(msg, MAX_MSG_LENGTH, "%s", "Failed to truncate print error log"); - if (rc < 0) { + if (rc < 0) { + rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "Failed to truncate print error log"); + if (rc < 0 || (size_t)rc >= MAX_MSG_LENGTH) { return 0; } } @@ -307,12 +306,12 @@ void log_append_logfile(const struct log_event *event, const char *timestamp, co tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - 15); } if (event->locinfo != NULL) { - nret = snprintf_truncated_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", - tmp_prefix ? tmp_prefix : "", timestamp, g_log_prio_name[event->priority], - g_log_vmname ? g_log_vmname : "lcrd", 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_log_prio_name[event->priority], + g_log_vmname ? g_log_vmname : "lcrd", event->locinfo->file, event->locinfo->func, + event->locinfo->line, msg); } else { - nret = snprintf_truncated_s(log_buffer, sizeof(log_buffer), "%s %s", timestamp, msg); + nret = snprintf(log_buffer, sizeof(log_buffer), "%s %s", timestamp, msg); } if (nret < 0) { @@ -429,14 +428,14 @@ int lcrd_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *tim /* Calculate the real seconds */ real_seconds = (((time->tv_sec - trans_to_sec) - hours_to_sec) - (real_minutes * 60)); - ret = sprintf_s(ns, LCRD_NUMSTRLEN64, "%ld", time->tv_nsec); - if (ret < 0 || ret >= LCRD_NUMSTRLEN64) { + ret = snprintf(ns, LCRD_NUMSTRLEN64, "%ld", time->tv_nsec); + if (ret < 0 || (size_t)ret >= LCRD_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/log.h b/src/log.h index afbd7ec..29cae65 100644 --- a/src/log.h +++ b/src/log.h @@ -17,6 +17,7 @@ #include #include +#include #include #include diff --git a/src/pack_config.c b/src/pack_config.c index 57434de..102c44f 100644 --- a/src/pack_config.c +++ b/src/pack_config.c @@ -1216,8 +1216,8 @@ static int parse_seccomp(const lcrc_host_config_t *srcconfig, host_config *dstco ret = -1; goto out; } - nret = sprintf_s(tmp_str, size, "seccomp=%s", seccomp_json); - if (nret < 0) { + nret = snprintf(tmp_str, size, "seccomp=%s", seccomp_json); + if (nret < 0 || (size_t)nret >= size) { COMMAND_ERROR("failed to sprintf buffer!"); ret = -1; goto out; diff --git a/src/path.c b/src/path.c index b22d6a5..9cd7d9f 100644 --- a/src/path.c +++ b/src/path.c @@ -18,11 +18,12 @@ #include #include #include +#include +#include #include "log.h" #include "path.h" #include "utils.h" -#include "securec.h" #define ISSLASH(C) ((C) == '/') #define IS_ABSOLUTE_FILE_NAME(F) (ISSLASH ((F)[0])) @@ -51,7 +52,6 @@ static int do_clean_path(const char *respath, const char *limit_respath, { char *dest = *dst; const char *endpos = NULL; - errno_t ret; for (endpos = stpos; *stpos; stpos = endpos) { while (ISSLASH(*stpos)) { @@ -80,11 +80,7 @@ static int do_clean_path(const char *respath, const char *limit_respath, 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'; } @@ -98,7 +94,6 @@ 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)) { @@ -107,11 +102,7 @@ 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)) { @@ -125,11 +116,11 @@ 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; @@ -195,10 +186,7 @@ static int do_get_symlinks_copy_buf(const char *buf, const char *prefix, size_t { if (IS_ABSOLUTE_FILE_NAME(buf)) { if (prefix_len) { - if (memcpy_s(*rpath, PATH_MAX, prefix, prefix_len) != EOK) { - ERROR("Memory copy failed!"); - return -1; - } + (void)memcpy(*rpath, prefix, prefix_len); } *dest = *rpath + prefix_len; *(*dest)++ = '/'; @@ -219,7 +207,6 @@ static int do_get_symlinks(const char **fullpath, const char *prefix, size_t pre int ret = -1; size_t len; ssize_t n; - errno_t rc = EOK; char *buf = NULL; if (++(*num_links) > MAXSYMLINKS) { @@ -253,16 +240,8 @@ static int do_get_symlinks(const char **fullpath, const char *prefix, size_t pre goto out; } - rc = memmove_s(&(*extra_buf)[n], (size_t)(PATH_MAX - n), *end, len + 1); - if (rc != EOK) { - ERROR("Memory move failed!"); - goto out; - } - rc = memcpy_s(*extra_buf, PATH_MAX, buf, (size_t)n); - if (rc != EOK) { - ERROR("Memory copy failed!"); - goto out; - } + (void)memmove(&(*extra_buf)[n], *end, len + 1); + (void)memcpy(*extra_buf, buf, (size_t)n); *fullpath = *end = *extra_buf; if (do_get_symlinks_copy_buf(buf, prefix, prefix_len, rpath, dest) != 0) { @@ -332,7 +311,6 @@ static int do_eval_symlinks_in_scope(const char *fullpath, const char *prefix, const char *start = NULL; const char *end = NULL; char *extra_buf = NULL; - errno_t rc = EOK; start = fullpath + prefix_len; for (end = start; *start; start = end) { @@ -354,12 +332,7 @@ static int do_eval_symlinks_in_scope(const char *fullpath, const char *prefix, goto out; } - rc = memcpy_s(*dest, (size_t)(end - start), start, (size_t)(end - start)); - if (rc != EOK) { - ERROR("Out of memory"); - nret = -1; - goto out; - } + (void)memcpy(*dest, start, (size_t)(end - start)); *dest += end - start; **dest = '\0'; @@ -387,7 +360,6 @@ static char *eval_symlinks_in_scope(const char *fullpath, const char *rootpath) char *prefix = NULL; const char *rpath_limit = NULL; size_t prefix_len; - errno_t rc = EOK; char resroot[PATH_MAX] = { 0 }; if (fullpath == NULL || rootpath == NULL) { @@ -424,11 +396,7 @@ static char *eval_symlinks_in_scope(const char *fullpath, const char *rootpath) dest = rpath; if (prefix_len) { - rc = memcpy_s(rpath, PATH_MAX, prefix, prefix_len); - if (rc != EOK) { - ERROR("Out of memory"); - goto out; - } + (void)memcpy(rpath, prefix, prefix_len); dest += prefix_len; } *dest++ = '/'; @@ -515,8 +483,8 @@ char *preserve_trailing_dot_or_separator(const char *cleanedpath, const char *or return NULL; } - nret = sprintf_s(respath, PATH_MAX, "%s", cleanedpath); - if (nret < 0) { + nret = snprintf(respath, PATH_MAX, "%s", cleanedpath); + if (nret < 0 || (size_t)nret >= PATH_MAX) { ERROR("Failed to print string"); return NULL; } @@ -598,8 +566,8 @@ char *get_resource_path(const char *rootpath, const char *path) char tmppath[PATH_MAX] = { 0 }; char fullpath[PATH_MAX] = { 0 }; - nret = sprintf_s(tmppath, sizeof(tmppath), "/%s/%s", rootpath, path); - if (nret < 0) { + nret = snprintf(tmppath, sizeof(tmppath), "/%s/%s", rootpath, path); + if (nret < 0 || (size_t)nret >= sizeof(tmppath)) { return NULL; } @@ -624,8 +592,8 @@ int resolve_path(const char *rootpath, const char *path, char **resolvedpath, ch *resolvedpath = NULL; *abspath = NULL; - nret = sprintf_s(tmppath, sizeof(tmppath), "/%s", path); - if (nret < 0) { + nret = snprintf(tmppath, sizeof(tmppath), "/%s", path); + if (nret < 0 || (size_t)nret >= sizeof(tmppath)) { ERROR("Failed to print string"); return -1; } @@ -658,8 +626,8 @@ int resolve_path(const char *rootpath, const char *path, char **resolvedpath, ch ERROR("Out of memory"); goto cleanup; } - nret = sprintf_s(*resolvedpath, len, "%s/%s", resolved_dir_path, basepath); - if (nret < 0) { + nret = snprintf(*resolvedpath, len, "%s/%s", resolved_dir_path, basepath); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to print string"); goto cleanup; } @@ -759,7 +727,8 @@ int realpath_in_scope(const char *rootfs, const char *path, char **real_path) char cleaned[PATH_MAX] = { 0 }; char *tmp = NULL; - if (sprintf_s(full_path, sizeof(full_path), "%s%s", rootfs, path) < 0) { + int nret = snprintf(full_path, sizeof(full_path), "%s%s", rootfs, path); + if (nret < 0 || (size_t)nret >= sizeof(full_path)) { ERROR("sprintf error: %s", strerror(errno)); ret = -1; goto out; diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index 66e7049..c0de76a 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -303,8 +303,8 @@ static int get_plugin_dir(char *plugin_dir) return -1; } - ret = sprintf_s(plugin_dir, PATH_MAX, "%s/plugins", statedir); - if (ret < 0) { + ret = snprintf(plugin_dir, PATH_MAX, "%s/plugins", statedir); + if (ret < 0 || (size_t)ret >= PATH_MAX) { goto failed; } @@ -365,14 +365,11 @@ static int get_plugin_addr_and_name(char *plugin_addr, char *plugin_name, const ERROR("not plugin filename %s", filename); goto out; } - nret = strcpy_s(plugin_name, PATH_MAX, filename); - if (nret < 0) { - ERROR("get plugin name failed %s", filename); - goto out; - } + (void)strcpy(plugin_name, filename); + plugin_name[str_length - PLUGIN_SOCKET_FILE_SUFFIX_LEN] = 0; - nret = sprintf_s(plugin_addr, PATH_MAX, "%s/%s", plugin_dir, filename); - if (nret < 0) { + nret = snprintf(plugin_addr, PATH_MAX, "%s/%s", plugin_dir, filename); + if (nret < 0 || nret >= PATH_MAX) { ERROR("get plugin addr failed %s", filename); goto out; } @@ -574,8 +571,8 @@ static int reload_plugin(const char *name) return -1; } - ret = sprintf_s(filename, PATH_MAX, "%s.sock", name); - if (ret < 0) { + ret = snprintf(filename, PATH_MAX, "%s.sock", name); + if (ret < 0 || ret >= PATH_MAX) { ERROR("get plugin addr failed %s", filename); return -1; } @@ -904,8 +901,8 @@ int pm_activate_plugin(plugin_t *plugin) } body_len = strlen(body) + 1; - ret = sprintf_s(socket, PATH_MAX, "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, PATH_MAX, "unix://%s", plugin->addr); + if (ret < 0 || ret >= PATH_MAX) { ERROR("get plugin socket failed"); ret = -1; goto out; @@ -1130,8 +1127,8 @@ static int pm_init_plugin(const plugin_t *plugin) } body_len = strlen(body) + 1; - ret = sprintf_s(socket, PATH_MAX, "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, PATH_MAX, "unix://%s", plugin->addr); + if (ret < 0 || ret >= PATH_MAX) { ERROR("get plugin socket failed %s", plugin->addr); ret = -1; goto out; @@ -1470,8 +1467,8 @@ static int plugin_event_pre_create_handle(const plugin_t *plugin, const char *ci } body_len = strlen(body) + 1; - ret = sprintf_s(socket, sizeof(socket), "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, sizeof(socket), "unix://%s", plugin->addr); + if (ret < 0 || (size_t)ret >= sizeof(socket)) { ERROR("get plugin socket failed %s", plugin->addr); ret = -1; goto out; @@ -1642,8 +1639,8 @@ static int plugin_event_pre_start_handle(const plugin_t *plugin, const char *cid } body_len = strlen(body) + 1; - ret = sprintf_s(socket, sizeof(socket), "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, sizeof(socket), "unix://%s", plugin->addr); + if (ret < 0 || (size_t)ret >= sizeof(socket)) { ERROR("get plugin socket failed %s", plugin->addr); ret = -1; goto out; @@ -1741,8 +1738,8 @@ static int plugin_event_post_stop_handle(const plugin_t *plugin, const char *cid } body_len = strlen(body) + 1; - ret = sprintf_s(socket, sizeof(socket), "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, sizeof(socket), "unix://%s", plugin->addr); + if (ret < 0 || (size_t)ret >= sizeof(socket)) { ERROR("get plugin socket failed %s", plugin->addr); ret = -1; goto out; @@ -1839,8 +1836,8 @@ static int plugin_event_post_remove_handle(const plugin_t *plugin, const char *c } body_len = strlen(body) + 1; - ret = sprintf_s(socket, sizeof(socket), "unix://%s", plugin->addr); - if (ret < 0) { + ret = snprintf(socket, sizeof(socket), "unix://%s", plugin->addr); + if (ret < 0 || (size_t)ret >= sizeof(socket)) { ERROR("get plugin socket failed %s", plugin->addr); ret = -1; goto out; diff --git a/src/runtime/lcr/lcr_rt_ops.c b/src/runtime/lcr/lcr_rt_ops.c index eb8a873..dee1d41 100644 --- a/src/runtime/lcr/lcr_rt_ops.c +++ b/src/runtime/lcr/lcr_rt_ops.c @@ -72,11 +72,11 @@ out: return ret; } -static int parse_container_pid(const char *S, const container_pid_t *pid_info) +static int parse_container_pid(const char *S, container_pid_t *pid_info) { int num; - num = sscanf_s(S, "%d %Lu %d %Lu", &pid_info->pid, &pid_info->start_time, &pid_info->ppid, &pid_info->pstart_time); + num = sscanf(S, "%d %Lu %d %Lu", &pid_info->pid, &pid_info->start_time, &pid_info->ppid, &pid_info->pstart_time); if (num != 4) { // args num to read is 4 ERROR("Call sscanf error: %s", errno ? strerror(errno) : ""); return -1; @@ -85,7 +85,7 @@ static int parse_container_pid(const char *S, const container_pid_t *pid_info) return 0; } -static int lcr_rt_read_pidfile(const char *pidfile, const container_pid_t *pid_info) +static int lcr_rt_read_pidfile(const char *pidfile, container_pid_t *pid_info) { if (pidfile == NULL || pid_info == NULL) { ERROR("Invalid input arguments"); @@ -191,8 +191,8 @@ static int remove_container_rootpath(const char *id, const char *root_path) int ret = 0; char cont_root_path[PATH_MAX] = { 0 }; - ret = sprintf_s(cont_root_path, sizeof(cont_root_path), "%s/%s", root_path, id); - if (ret < 0) { + ret = snprintf(cont_root_path, sizeof(cont_root_path), "%s/%s", root_path, id); + if (ret < 0 || (size_t)ret >= sizeof(cont_root_path)) { ERROR("Failed to sprintf container_state"); ret = -1; goto out; @@ -223,19 +223,20 @@ int rt_lcr_rm(const char *name, const char *runtime, const rt_rm_params_t *param } if (!engine_ops->engine_delete_op(name, params->rootpath)) { + ret = -1; const char *tmpmsg = engine_ops->engine_get_errmsg_op(); + lcrd_set_error_message("Runtime delete container error: %s", + (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR); + ERROR("Runtime delete container error: %s", + (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR); if (tmpmsg != NULL && strstr(tmpmsg, "No such container") != NULL) { // container root path may been corrupted, try to remove by daemon WARN("container %s root path may been corrupted, try to remove by daemon", name); if (remove_container_rootpath(name, params->rootpath) == 0) { + ret = 0; goto out; } } - lcrd_set_error_message("Runtime delete container error: %s", - (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR); - ERROR("Runtime delete container error: %s", - (tmpmsg != NULL && strcmp(tmpmsg, DEF_SUCCESS_STR)) ? tmpmsg : DEF_ERR_RUNTIME_STR); - ret = -1; goto out; } diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 579e3f8..acea344 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -21,7 +21,6 @@ #include #include -#include "securec.h" #include "runtime.h" #include "log.h" #include "utils.h" diff --git a/src/services/callback.c b/src/services/callback.c index ae178dc..9d3cbcf 100644 --- a/src/services/callback.c +++ b/src/services/callback.c @@ -16,7 +16,6 @@ #include "image_cb.h" #include "execution.h" -#include "securec.h" service_callback_t g_isulad_servicecallback; diff --git a/src/services/cri/checkpoint_handler.cc b/src/services/cri/checkpoint_handler.cc index e0ba26f..58229a6 100644 --- a/src/services/cri/checkpoint_handler.cc +++ b/src/services/cri/checkpoint_handler.cc @@ -20,7 +20,6 @@ #include #include #include -#include #include "constants.h" #include "utils.h" diff --git a/src/services/cri/cni_network_plugin.cc b/src/services/cri/cni_network_plugin.cc index dc2d3f9..c34ce04 100644 --- a/src/services/cri/cni_network_plugin.cc +++ b/src/services/cri/cni_network_plugin.cc @@ -19,7 +19,6 @@ #include #include #include -#include #include "log.h" #include "utils.h" diff --git a/src/services/cri/cri_container.cc b/src/services/cri/cri_container.cc index a098c88..73b2fea 100644 --- a/src/services/cri/cri_container.cc +++ b/src/services/cri/cri_container.cc @@ -1357,6 +1357,7 @@ void CRIRuntimeServiceImpl::Exec(const runtime::v1alpha2::ExecRequest &req, runt std::string token = cache->Insert(const_cast(execReq)); if (token.empty()) { error.SetError("failed to get a unique token!"); + delete execReq; return; } std::string url = BuildURL("exec", token); @@ -1406,6 +1407,7 @@ void CRIRuntimeServiceImpl::Attach(const runtime::v1alpha2::AttachRequest &req, std::string token = cache->Insert(const_cast(attachReq)); if (token.empty()) { error.SetError("failed to get a unique token!"); + delete attachReq; return; } std::string url = BuildURL("attach", token); diff --git a/src/services/cri/cri_helpers.cc b/src/services/cri/cri_helpers.cc index a760a96..2ecef0d 100644 --- a/src/services/cri/cri_helpers.cc +++ b/src/services/cri/cri_helpers.cc @@ -225,17 +225,9 @@ int FiltersAdd(defs_filters *filters, const std::string &key, const std::string } if (filters->len) { - if (memcpy_s(keys, len * sizeof(char *), filters->keys, filters->len * sizeof(char *)) != EOK) { - free(keys); - free(vals); - return -1; - } - if (memcpy_s(vals, len * sizeof(json_map_string_bool *), filters->values, - filters->len * sizeof(json_map_string_bool *)) != EOK) { - free(keys); - free(vals); - return -1; - } + (void)memcpy(keys, filters->keys, filters->len * sizeof(char *)); + + (void)memcpy(vals, filters->values, filters->len * sizeof(json_map_string_bool *)); } free(filters->keys); filters->keys = keys; @@ -377,7 +369,8 @@ std::string sha256(const char *val) char outputBuffer[(SHA256_DIGEST_LENGTH * 2) + 1] { 0 }; for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - if (sprintf_s(outputBuffer + (i * 2), 3, "%02x", (unsigned int)hash[i]) < 0) { + int ret = snprintf(outputBuffer + (i * 2), 3, "%02x", (unsigned int)hash[i]); + if (ret >= 3 || ret < 0) { return ""; } } diff --git a/src/services/cri/cri_runtime_service.cc b/src/services/cri/cri_runtime_service.cc index bcaa96c..6784af2 100644 --- a/src/services/cri/cri_runtime_service.cc +++ b/src/services/cri/cri_runtime_service.cc @@ -164,6 +164,7 @@ std::unique_ptr CRIRuntimeServiceImpl::Status( std::string CRIRuntimeServiceImpl::GetNetNS(const std::string &podSandboxID, Errors &err) { + int ret; char fullpath[PATH_MAX] { 0 }; std::string result { "" }; const std::string NetNSFmt { "/proc/%d/ns/net" }; @@ -176,7 +177,8 @@ std::string CRIRuntimeServiceImpl::GetNetNS(const std::string &podSandboxID, Err err.Errorf("cannot find network namespace for the terminated container %s", podSandboxID.c_str()); goto cleanup; } - if (sprintf_s(fullpath, sizeof(fullpath), NetNSFmt.c_str(), inspect_data->state->pid) < 0) { + ret = snprintf(fullpath, sizeof(fullpath), NetNSFmt.c_str(), inspect_data->state->pid); + if ((size_t)ret >= sizeof(fullpath) || ret < 0) { err.SetError("Sprint nspath failed"); goto cleanup; } diff --git a/src/services/cri/errors.cc b/src/services/cri/errors.cc index 8373565..a2d41c1 100644 --- a/src/services/cri/errors.cc +++ b/src/services/cri/errors.cc @@ -17,8 +17,6 @@ #include -#include "securec.h" - Errors::Errors() { m_message.clear(); @@ -117,9 +115,9 @@ void Errors::Errorf(const char *fmt, ...) va_start(argp, fmt); - ret = vsprintf_s(errbuf, BUFSIZ, fmt, argp); + ret = vsnprintf(errbuf, BUFSIZ, fmt, argp); va_end(argp); - if (ret < 0) { + if (ret < 0 || ret >= BUFSIZ) { m_message = "Error message is too long"; return; } diff --git a/src/services/cri/request_cache.cc b/src/services/cri/request_cache.cc index e817530..e46e425 100644 --- a/src/services/cri/request_cache.cc +++ b/src/services/cri/request_cache.cc @@ -70,6 +70,10 @@ void RequestCache::GarbageCollection() if (now < oldest.expireTime) { return; } + if (oldest.req != nullptr) { + delete oldest.req; + oldest.req = nullptr; + } m_ll.pop_back(); m_tokens.erase(oldest.token); } diff --git a/src/services/execution/events/collector.c b/src/services/execution/events/collector.c index 5e2287f..1e197a8 100644 --- a/src/services/execution/events/collector.c +++ b/src/services/execution/events/collector.c @@ -30,7 +30,6 @@ #include #include "collector.h" #include "lcrd_config.h" -#include "securec.h" #include "liblcrd.h" #include "containers_store.h" @@ -73,8 +72,8 @@ static bool get_idreg(regex_t *preg, const char *id) return false; } - nret = sprintf_s(regexp, len, "^%s$", id); - if (nret < 0) { + nret = snprintf(regexp, len, "^%s$", id); + if ((size_t)nret >= len || nret < 0) { ERROR("Failed to print string"); goto error; } @@ -237,7 +236,6 @@ int lcrd_monitor_send_event(const char *name, runtime_state_t state, int pid, in { int ret = 0; char *statedir = NULL; - errno_t nret; struct monitord_msg msg = { .type = monitord_msg_state, .value = state, @@ -258,12 +256,7 @@ int lcrd_monitor_send_event(const char *name, runtime_state_t state, int pid, in goto out; } - nret = strncpy_s(msg.name, sizeof(msg.name), name, sizeof(msg.name) - 1); - if (nret != EOK) { - ERROR("Fail at lcrd_monitor_send_event string copy!"); - ret = -1; - goto out; - } + (void)strncpy(msg.name, name, sizeof(msg.name) - 1); msg.name[sizeof(msg.name) - 1] = 0; if (pid > 0) { msg.pid = pid; diff --git a/src/services/execution/events/events_handler.c b/src/services/execution/events/events_handler.c index 4581d07..9c544bb 100644 --- a/src/services/execution/events/events_handler.c +++ b/src/services/execution/events/events_handler.c @@ -27,7 +27,6 @@ #include "collector.h" #include "events_handler.h" #include "utils.h" -#include "securec.h" #include "containers_store.h" #include "execution.h" #include "plugin.h" diff --git a/src/services/execution/execute/execution.c b/src/services/execution/execute/execution.c index a260aff..bbe292e 100644 --- a/src/services/execution/execute/execution.c +++ b/src/services/execution/execute/execution.c @@ -35,7 +35,6 @@ #include "lcrd_config.h" #include "config.h" #include "image.h" -#include "securec.h" #include "execution.h" #include "verify.h" #include "container_inspect.h" @@ -190,7 +189,8 @@ static int umount_dev_tmpfs_for_system_container(const container_t *cont) { if (cont->hostconfig != NULL && cont->hostconfig->system_container) { char rootfs_dev_path[PATH_MAX] = { 0 }; - if (sprintf_s(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs) < 0) { + int nret = snprintf(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs); + if ((size_t)nret >= sizeof(rootfs_dev_path) || nret < 0) { ERROR("Out of memory"); return -1; } @@ -306,8 +306,8 @@ static int prepare_start_state_files(const container_t *cont, char **exit_fifo, char pidfile[PATH_MAX] = { 0 }; const char *id = cont->common_config->id; - nret = sprintf_s(container_state, sizeof(container_state), "%s/%s", cont->state_path, id); - if (nret < 0 || nret >= (int)sizeof(container_state)) { + nret = snprintf(container_state, sizeof(container_state), "%s/%s", cont->state_path, id); + if (nret < 0 || (size_t)nret >= sizeof(container_state)) { ERROR("Failed to sprintf container_state"); ret = -1; goto out; @@ -320,8 +320,8 @@ static int prepare_start_state_files(const container_t *cont, char **exit_fifo, goto out; } - nret = sprintf_s(pidfile, sizeof(pidfile), "%s/pid.file", container_state); - if (nret < 0 || nret >= (int)sizeof(pidfile)) { + nret = snprintf(pidfile, sizeof(pidfile), "%s/pid.file", container_state); + if (nret < 0 || (size_t)nret >= sizeof(pidfile)) { ERROR("Failed to sprintf pidfile"); ret = -1; goto out; @@ -418,8 +418,9 @@ static int mount_host_channel(const host_config_host_channel *host_channel, cons if (detect_mount(host_channel->path_on_host)) { return 0; } - if (sprintf_s(properties, sizeof(properties), "mode=1777,size=%llu", - (long long unsigned int)host_channel->size) < 0) { + int nret = snprintf(properties, sizeof(properties), "mode=1777,size=%llu", + (long long unsigned int)host_channel->size); + if (nret < 0 || (size_t)nret >= sizeof(properties)) { ERROR("Failed to generate mount properties"); return -1; } @@ -455,7 +456,8 @@ static int mount_dev_tmpfs_for_system_container(const container_t *cont) if (!cont->hostconfig->system_container) { return 0; } - if (sprintf_s(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs) < 0) { + int nret = snprintf(rootfs_dev_path, sizeof(rootfs_dev_path), "%s/dev", cont->common_config->base_fs); + if (nret < 0 || (size_t)nret >= sizeof(rootfs_dev_path)) { ERROR("Out of memory"); return -1; } @@ -597,7 +599,8 @@ static int write_env_content(const char *env_path, const char **env, size_t env_ ret = -1; goto out; } - if (sprintf_s(env_content, len, "%s\n", env[i]) < 0) { + nret = snprintf(env_content, len, "%s\n", env[i]); + if (nret < 0 || (size_t)nret >= len) { ERROR("Out of memory"); free(env_content); ret = -1; @@ -1123,6 +1126,12 @@ int stop_container(container_t *cont, int timeout, bool force, bool restart) container_lock(cont); + if (is_paused(cont->state)) { + ERROR("Container %s is paused. Unpause the container before stopping or killing", id); + lcrd_set_error_message("Container %s is paused. Unpause the container before stopping or killing", id); + ret = -1; + goto out; + } // set AutoRemove flag to false before stop so the container won't be // removed during restart process if (restart) { @@ -1612,8 +1621,8 @@ static int do_cleanup_container_resources(container_t *cont) goto out; } - ret = sprintf_s(container_state, sizeof(container_state), "%s/%s", statepath, id); - if (ret < 0) { + ret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id); + if (ret < 0 || (size_t)ret >= sizeof(container_state)) { ERROR("Failed to sprintf container_state"); ret = -1; goto out; @@ -1696,16 +1705,24 @@ int cleanup_container(container_t *cont, bool force) if (is_running(cont->state)) { if (!force) { - lcrd_set_error_message("You cannot remove a running container %s. " - "Stop the container before attempting removal or use -f", id); - ERROR("You cannot remove a running container %s. Stop the container before attempting removal or use -f", - id); + if (is_paused(cont->state)) { + lcrd_set_error_message("You cannot remove a paused container %s. " + "Unpause and then stop the container before " + "attempting removal or force remove", id); + ERROR("You cannot remove a paused container %s. Unpause and then stop the container before " + "attempting removal or force remove", id); + } else { + lcrd_set_error_message("You cannot remove a running container %s. " + "Stop the container before attempting removal or use -f", id); + ERROR("You cannot remove a running container %s." + " Stop the container before attempting removal or use -f", id); + } ret = -1; goto reset_removal_progress; } ret = stop_container(cont, 3, force, false); if (ret != 0) { - lcrd_try_set_error_message("Could not stop running container %s, cannot remove", id); + lcrd_append_error_message("Could not stop running container %s, cannot remove. ", id); ERROR("Could not stop running container %s, cannot remove", id); ret = -1; goto reset_removal_progress; diff --git a/src/services/execution/execute/execution_create.c b/src/services/execution/execute/execution_create.c index 0d8d942..312a41c 100644 --- a/src/services/execution/execute/execution_create.c +++ b/src/services/execution/execute/execution_create.c @@ -33,7 +33,6 @@ #include "console.h" #include "lcrd_config.h" #include "config.h" -#include "securec.h" #include "specs.h" #include "verify.h" #include "containers_store.h" @@ -179,8 +178,8 @@ static int add_default_log_config_to_custom_spec(const char *id, const char *run if (custom_config->log_config->log_file == NULL) { char default_path[PATH_MAX] = { 0 }; - int nret = sprintf_s(default_path, PATH_MAX, "%s/%s/console.log", runtime_root, id); - if (nret < 0) { + int nret = snprintf(default_path, PATH_MAX, "%s/%s/console.log", runtime_root, id); + if (nret >= PATH_MAX || nret < 0) { ERROR("Create default log path for container %s failed", id); ret = -1; goto out; @@ -235,8 +234,8 @@ static int generateID(char *id, size_t len) return -1; } unsigned char rs = (unsigned char)(num % m); - nret = sprintf_s((id + i * 2), ((len - i) * 2 + 1), "%02x", (unsigned int)rs); - if (nret < 0) { + nret = snprintf((id + i * 2), ((len - i) * 2 + 1), "%02x", (unsigned int)rs); + if (nret >= ((len - i) * 2 + 1) || nret < 0) { close(fd); return -1; } @@ -332,6 +331,93 @@ out: return id; } +static int inspect_image(const char *image, imagetool_image **result) +{ + int ret = 0; + im_status_request *request = NULL; + im_status_response *response = NULL; + + if (image == NULL) { + ERROR("Empty image name or id"); + return -1; + } + + request = (im_status_request *)util_common_calloc_s(sizeof(im_status_request)); + if (request == NULL) { + ERROR("Out of memory"); + return -1; + } + request->image.image = util_strdup_s(image); + + if (im_image_status(request, &response) != 0) { + if (response != NULL && response->errmsg != NULL) { + ERROR("failed to inspect inspect image info: %s", response->errmsg); + } else { + ERROR("Failed to call status image"); + } + ret = -1; + goto cleanup; + } + + if (response->image_info != NULL) { + *result = response->image_info->image; + response->image_info->image = NULL; + } + +cleanup: + free_im_status_request(request); + free_im_status_response(response); + return ret; +} + +static int conf_get_image_id(const char *image, char **id) +{ + int ret = 0; + imagetool_image *ir = NULL; + size_t len = 0; + char *image_id = NULL; + + if (image == NULL || strcmp(image, "none") == 0) { + *id = util_strdup_s("none"); + return 0; + } + + if (inspect_image(image, &ir) != 0) { + ERROR("Failed to inspect image status"); + ret = -1; + goto out; + } + + if (strlen(ir->id) > SIZE_MAX / sizeof(char) - strlen("sha256:")) { + ERROR("Invalid image id"); + ret = -1; + goto out; + } + + len = strlen("sha256:") + strlen(ir->id) + 1; + image_id = (char *)util_common_calloc_s(len * sizeof(char)); + if (image_id == NULL) { + ERROR("Out of memory"); + ret = -1; + goto out; + } + + int nret = snprintf(image_id, len, "sha256:%s", ir->id); + if (nret < 0 || (size_t)nret >= len) { + ERROR("Failed to sprintf string"); + ret = -1; + goto out; + } + + *id = image_id; + image_id = NULL; + +out: + free_imagetool_image(ir); + free(image_id); + return ret; +} + static int register_new_container(const char *id, const char *runtime, host_config **host_spec, container_config_v2_common_config **v2_spec) { @@ -339,6 +425,7 @@ static int register_new_container(const char *id, const char *runtime, host_conf bool registed = false; char *runtime_root = NULL; char *runtime_stat = NULL; + char *image_id = NULL; container_t *cont = NULL; runtime_root = conf_get_routine_rootdir(runtime); @@ -351,7 +438,12 @@ static int register_new_container(const char *id, const char *runtime, host_conf goto out; } - cont = container_new(runtime, runtime_root, runtime_stat, host_spec, v2_spec); + if (strcmp((*v2_spec)->image_type, IMAGE_TYPE_OCI) == 0) { + if (conf_get_image_id((*v2_spec)->image, &image_id) != 0) { + goto out; + } + } + cont = container_new(runtime, runtime_root, runtime_stat, image_id, host_spec, v2_spec); if (cont == NULL) { ERROR("Failed to create container '%s'", id); goto out; @@ -372,6 +464,7 @@ static int register_new_container(const char *id, const char *runtime, host_conf out: free(runtime_root); free(runtime_stat); + free(image_id); if (ret != 0) { container_unref(cont); } @@ -522,8 +615,8 @@ static int create_container_root_dir(const char *id, const char *runtime_root) char container_root[PATH_MAX] = { 0x00 }; mode_t mask = umask(S_IWOTH); - nret = sprintf_s(container_root, sizeof(container_root), "%s/%s", runtime_root, id); - if (nret < 0) { + nret = snprintf(container_root, sizeof(container_root), "%s/%s", runtime_root, id); + if ((size_t)nret >= sizeof(container_root) || nret < 0) { ret = -1; goto out; } @@ -545,8 +638,8 @@ static int delete_container_root_dir(const char *id, const char *runtime_root) int ret = 0; char container_root[PATH_MAX] = { 0x00 }; - ret = sprintf_s(container_root, sizeof(container_root), "%s/%s", runtime_root, id); - if (ret < 0) { + ret = snprintf(container_root, sizeof(container_root), "%s/%s", runtime_root, id); + if ((size_t)ret >= sizeof(container_root) || ret < 0) { ERROR("Failed to sprintf invalid root directory %s/%s", runtime_root, id); ret = -1; goto out; diff --git a/src/services/execution/execute/execution_extend.c b/src/services/execution/execute/execution_extend.c index 1f92236..cb589f1 100644 --- a/src/services/execution/execute/execution_extend.c +++ b/src/services/execution/execute/execution_extend.c @@ -34,7 +34,6 @@ #include "config.h" #include "restartmanager.h" #include "image.h" -#include "securec.h" #include "verify.h" #include "container_inspect.h" #include "containers_store.h" diff --git a/src/services/execution/execute/execution_information.c b/src/services/execution/execute/execution_information.c index ba79509..47d68e9 100644 --- a/src/services/execution/execute/execution_information.c +++ b/src/services/execution/execute/execution_information.c @@ -32,7 +32,6 @@ #include "lcrd_config.h" #include "config.h" #include "image.h" -#include "securec.h" #include "execution.h" #include "container_inspect.h" #include "containers_store.h" @@ -497,13 +496,13 @@ static char *ps_pids_arg(const pid_t *pids, size_t pids_len) int ret = -1; size_t tmp_len = 0; char *pid_arg = NULL; - char pid_str[UINT_LEN + 1] = { 0 }; + char pid_str[UINT_LEN + 2] = { 0 }; - if (pids_len > SIZE_MAX / (UINT_LEN + 1)) { + if (pids_len == 0 || pids_len > (SIZE_MAX - 1) / (UINT_LEN + 2)) { ERROR("Invalid pid size"); return NULL; } - tmp_len = pids_len * (UINT_LEN + 1); + tmp_len = pids_len * (UINT_LEN + 2) + 1; pid_arg = util_common_calloc_s(tmp_len); if (pid_arg == NULL) { ERROR("Out of memory"); @@ -512,22 +511,17 @@ static char *ps_pids_arg(const pid_t *pids, size_t pids_len) for (i = 0; i < pids_len; i++) { if (i != (pids_len - 1)) { - nret = sprintf_s(pid_str, sizeof(pid_str), "%d,", pids[i]); + nret = snprintf(pid_str, sizeof(pid_str), "%d,", pids[i]); } else { - nret = sprintf_s(pid_str, sizeof(pid_str), "%d", pids[i]); + nret = snprintf(pid_str, sizeof(pid_str), "%d", pids[i]); } - if (nret < 0) { + if ((size_t)nret >= sizeof(pid_str) || nret < 0) { ERROR("Failed to sprintf pids!"); ret = -1; goto out; } - nret = strcat_s(pid_arg, tmp_len, pid_str); - if (nret != EOK) { - ERROR("Failed to cat pids!"); - ret = -1; - goto out; - } + (void)strcat(pid_arg, pid_str); } ret = 0; @@ -651,16 +645,15 @@ static int do_top(const container_top_request *request, container_t *cont, size_ char **stdout_buffer, char **stderr_buffer) { int ret = 0; - int nret = 0; bool command_ret = false; char *ps_args_with_q = NULL; size_t ps_args_with_q_len = 0; - if (pids_len > (SIZE_MAX / (UINT_LEN + 1)) - 1) { + if (pids_len == 0 || pids_len > ((SIZE_MAX - 3) / (UINT_LEN + 2)) - 1) { ERROR("Invalid pid size"); return -1; } - ps_args_with_q_len = (pids_len + 1) * (UINT_LEN + 1); + ps_args_with_q_len = (pids_len + 1) * (UINT_LEN + 2) + 3; ps_args_with_q = util_common_calloc_s(ps_args_with_q_len); if (ps_args_with_q == NULL) { ERROR("Out of memory"); @@ -668,19 +661,9 @@ static int do_top(const container_top_request *request, container_t *cont, size_ goto out; } - nret = strcat_s(ps_args_with_q, ps_args_with_q_len, "-q"); - if (nret != EOK) { - ERROR("Failed to cat pids!"); - ret = -1; - goto out; - } + (void)strcat(ps_args_with_q, "-q"); - nret = strcat_s(ps_args_with_q, ps_args_with_q_len, pid_args); - if (nret != EOK) { - ERROR("Failed to cat pids!"); - ret = -1; - goto out; - } + (void)strcat(ps_args_with_q, pid_args); command_ret = util_exec_top_cmd(execute_ps_command, request->args, ps_args_with_q, request->args_len, stdout_buffer, stderr_buffer); @@ -1156,92 +1139,6 @@ out: return ret; } -static int inspect_image(const char *image, imagetool_image **result) -{ - int ret = 0; - im_status_request *request = NULL; - im_status_response *response = NULL; - - if (image == NULL) { - ERROR("Empty image name or id"); - return -1; - } - - request = (im_status_request *)util_common_calloc_s(sizeof(im_status_request)); - if (request == NULL) { - ERROR("Out of memory"); - return -1; - } - request->image.image = util_strdup_s(image); - - if (im_image_status(request, &response) != 0) { - if (response != NULL && response->errmsg != NULL) { - ERROR("failed to inspect inspect image info: %s", response->errmsg); - } else { - ERROR("Failed to call status image"); - } - ret = -1; - goto cleanup; - } - - if (response->image_info != NULL) { - *result = response->image_info->image; - response->image_info->image = NULL; - } - -cleanup: - free_im_status_request(request); - free_im_status_response(response); - return ret; -} - -static int pack_inspect_general_image_data(const char *image, container_inspect *inspect) -{ - int ret = 0; - imagetool_image *ir = NULL; - size_t len = 0; - char *image_data = NULL; - - if (image == NULL || strcmp(image, "none") == 0) { - inspect->image = util_strdup_s("none"); - return 0; - } - - if (inspect_image(image, &ir) != 0) { - ERROR("Failed to inspect image status"); - ret = -1; - goto out; - } - - if (strlen(ir->id) > SIZE_MAX / sizeof(char) - strlen("sha256:")) { - ERROR("Invalid image id"); - ret = -1; - goto out; - } - - len = strlen("sha256:") + strlen(ir->id) + 1; - image_data = (char *)util_common_calloc_s(len * sizeof(char)); - if (image_data == NULL) { - ERROR("Out of memory"); - ret = -1; - goto out; - } - - if (sprintf_s(image_data, len, "sha256:%s", ir->id) < 0) { - ERROR("Failed to sprintf string"); - ret = -1; - goto out; - } - - inspect->image = image_data; - image_data = NULL; - -out: - free_imagetool_image(ir); - free(image_data); - return ret; -} - static int pack_inspect_general_data(const container_t *cont, container_inspect *inspect) { int ret = 0; @@ -1258,11 +1155,7 @@ static int pack_inspect_general_data(const container_t *cont, container_inspect goto out; } - if (pack_inspect_general_image_data(cont->common_config->image, inspect) != 0) { - ERROR("Failed to pack image info"); - ret = -1; - goto out; - } + inspect->image = cont->image_id != NULL ? util_strdup_s(cont->image_id) : util_strdup_s(""); if (cont->common_config->log_path != NULL) { inspect->log_path = util_strdup_s(cont->common_config->log_path); diff --git a/src/services/execution/execute/execution_network.c b/src/services/execution/execute/execution_network.c index 43be0fb..fff0e97 100644 --- a/src/services/execution/execute/execution_network.c +++ b/src/services/execution/execute/execution_network.c @@ -109,7 +109,8 @@ static int get_content_and_hosts_map(FILE *fp, char **content, json_map_string_b host_ip = strtok_r(tmp_str, " ", &saveptr); host_name = strtok_r(NULL, " ", &saveptr); if (host_ip != NULL && host_name != NULL) { - if (sprintf_s(host_key, sizeof(host_key), "%s:%s", host_ip, host_name) < 0) { + int nret = snprintf(host_key, sizeof(host_key), "%s:%s", host_ip, host_name); + if ((size_t)nret >= sizeof(host_key) || nret < 0) { free(tmp_str); ERROR("Out of memory"); ret = -1; @@ -172,7 +173,8 @@ static int merge_hosts_content(const host_config *host_spec, char **content, jso ERROR("extra host '%s' format error.", host_spec->extra_hosts[i]); return -1; } - if (sprintf_s(host_key, sizeof(host_key), "%s:%s", host_ip, host_name) < 0) { + int nret = snprintf(host_key, sizeof(host_key), "%s:%s", host_ip, host_name); + if ((size_t)nret >= sizeof(host_key) || nret < 0) { free(hosts); ERROR("Out of memory"); return -1; @@ -648,7 +650,6 @@ error_out: static int chown_network(const char *user_remap, const char *rootfs, const char *filename) { int ret = 0; - size_t path_len = 0; char *file_path = NULL; unsigned int host_uid = 0; unsigned int host_gid = 0; @@ -663,20 +664,9 @@ static int chown_network(const char *user_remap, const char *rootfs, const char ret = -1; goto out; } - path_len = strlen(rootfs) + strlen(filename) + 1; - if (path_len > PATH_MAX) { - ERROR("Invalid path length"); - ret = -1; - goto out; - } - file_path = util_common_calloc_s(path_len); - if (file_path == NULL) { - ERROR("Out of memory"); - ret = -1; - goto out; - } - if (sprintf_s(file_path, path_len, "%s%s", rootfs, filename) < 0) { - ERROR("Failed to print string"); + if (realpath_in_scope(rootfs, filename, &file_path) < 0) { + SYSERROR("Failed to get real path '%s' under rootfs '%s'", filename, rootfs); + lcrd_set_error_message("Failed to get real path '%s' under rootfs '%s'", filename, rootfs); ret = -1; goto out; } @@ -848,14 +838,16 @@ int init_container_network_confs(const char *id, const char *rootpath, const hos } // create hosts, resolv.conf and so - if (sprintf_s(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "hosts") < 0) { + int nret = snprintf(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "hosts"); + if (nret >= PATH_MAX || nret < 0) { ERROR("Failed to print string"); ret = -1; goto cleanup; } free(common_config->hosts_path); common_config->hosts_path = util_strdup_s(file_path); - if (sprintf_s(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "resolv.conf") < 0) { + nret = snprintf(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "resolv.conf"); + if (nret >= PATH_MAX || nret < 0) { ERROR("Failed to print string"); ret = -1; goto cleanup; diff --git a/src/services/execution/execute/execution_stream.c b/src/services/execution/execute/execution_stream.c index 6cb4c0d..3ad79fc 100644 --- a/src/services/execution/execute/execution_stream.c +++ b/src/services/execution/execute/execution_stream.c @@ -28,7 +28,6 @@ #include #include #include -#include #include "log.h" #include "engine.h" @@ -144,15 +143,15 @@ int create_daemon_fifos(const char *id, const char *runtime, bool attach_stdin, goto cleanup; } - nret = sprintf_s(subpath, PATH_MAX, "%s/%s/%u_%u_%u", id, operation, - (unsigned int)tid, (unsigned int)now.tv_sec, (unsigned int)(now.tv_nsec)); - if (nret < 0) { + nret = snprintf(subpath, PATH_MAX, "%s/%s/%u_%u_%u", id, operation, + (unsigned int)tid, (unsigned int)now.tv_sec, (unsigned int)(now.tv_nsec)); + if (nret >= PATH_MAX || nret < 0) { ERROR("Failed to print string"); goto cleanup; } - nret = sprintf_s(fifodir, PATH_MAX, "%s/%s", statepath, subpath); - if (nret < 0) { + nret = snprintf(fifodir, PATH_MAX, "%s/%s", statepath, subpath); + if (nret >= PATH_MAX || nret < 0) { ERROR("Failed to print string"); goto cleanup; } @@ -438,12 +437,14 @@ static int generate_user_string_by_uid_gid(const oci_runtime_spec_process_user * char gid_str[LCRD_NUMSTRLEN32] = { 0 }; size_t len; - if (sprintf_s(uid_str, LCRD_NUMSTRLEN32, "%u", (unsigned int)puser->uid) < 0) { + int nret = snprintf(uid_str, LCRD_NUMSTRLEN32, "%u", (unsigned int)puser->uid); + if (nret >= LCRD_NUMSTRLEN32 || nret < 0) { ERROR("Invalid UID:%u", (unsigned int)puser->uid); return -1; } - if (sprintf_s(gid_str, LCRD_NUMSTRLEN32, "%u", (unsigned int)puser->gid) < 0) { + nret = snprintf(gid_str, LCRD_NUMSTRLEN32, "%u", (unsigned int)puser->gid); + if (nret >= LCRD_NUMSTRLEN32 || nret < 0) { ERROR("Invalid attach uid value :%u", (unsigned int)puser->gid); return -1; } @@ -455,7 +456,8 @@ static int generate_user_string_by_uid_gid(const oci_runtime_spec_process_user * return -1; } - if (sprintf_s(*user, len, "%u:%u", (unsigned int)puser->uid, (unsigned int)puser->gid) < 0) { + nret = snprintf(*user, len, "%u:%u", (unsigned int)puser->uid, (unsigned int)puser->gid); + if ((size_t)nret >= len || nret < 0) { ERROR("Invalid UID:GID (%u:%u)", (unsigned int)puser->uid, (unsigned int)puser->gid); free(*user); *user = NULL; @@ -1188,10 +1190,7 @@ static ssize_t extract_stream_to_io_read(void *content, void *buf, size_t buf_le DEBUG("Client may exited"); return -1; } - if (memcpy_s(buf, buf_len, copy.data, copy.data_len) != EOK) { - free(copy.data); - return -1; - } + (void)memcpy(buf, copy.data, copy.data_len); free(copy.data); return (ssize_t)(copy.data_len); } @@ -1562,7 +1561,8 @@ static int do_read_all_container_logs(int64_t require_line, const char *path, co char log_path[PATH_MAX] = { 0 }; for (; i > 0; i--) { - if (sprintf_s(log_path, PATH_MAX, "%s.%d", path, i) < 0) { + int nret = snprintf(log_path, PATH_MAX, "%s.%d", path, i); + if (nret >= PATH_MAX || nret < 0) { ERROR("Sprintf failed"); goto out; } @@ -1600,7 +1600,8 @@ static int do_show_all_logs(const struct container_log_config *conf, const strea char log_path[PATH_MAX] = { 0 }; while (index > 0) { - if (sprintf_s(log_path, PATH_MAX, "%s.%d", conf->path, index) < 0) { + int nret = snprintf(log_path, PATH_MAX, "%s.%d", conf->path, index); + if (nret >= PATH_MAX || nret < 0) { ERROR("Sprintf failed"); ret = -1; goto out; @@ -1734,7 +1735,8 @@ static int do_tail_container_logs(int64_t require_line, const struct container_l } left -= get_line; get_line = 0; - if (sprintf_s(log_path, PATH_MAX, "%s.%d", conf->path, i) < 0) { + int nret = snprintf(log_path, PATH_MAX, "%s.%d", conf->path, i); + if (nret >= PATH_MAX || nret < 0) { ERROR("Sprintf failed"); goto out; } diff --git a/src/services/execution/log_gather.c b/src/services/execution/log_gather.c index 13faf39..d376f80 100644 --- a/src/services/execution/log_gather.c +++ b/src/services/execution/log_gather.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include "log.h" @@ -44,15 +43,18 @@ static int log_file_open(); static int file_rotate_gz(const char *file_name, int i) { + int ret = 0; char from_path[PATH_MAX] = { 0 }; char to_path[PATH_MAX] = { 0 }; - if (sprintf_s(from_path, PATH_MAX, "%s.%d.gz", file_name, (i - 1)) == -1) { + ret = snprintf(from_path, PATH_MAX, "%s.%d.gz", file_name, (i - 1)); + if (ret >= PATH_MAX || ret < 0) { ERROR("sprint zip file name failed"); return -1; } - if (sprintf_s(to_path, PATH_MAX, "%s.%d.gz", file_name, i) == -1) { + ret = snprintf(to_path, PATH_MAX, "%s.%d.gz", file_name, i); + if (ret >= PATH_MAX || ret < 0) { ERROR("sprint zip file name failed"); return -1; } @@ -67,9 +69,11 @@ static int file_rotate_gz(const char *file_name, int i) static int file_rotate_me(const char *file_name) { + int ret = 0; char tmp_path[PATH_MAX] = { 0 }; - if (sprintf_s(tmp_path, PATH_MAX, "%s.1", file_name) == -1) { + ret = snprintf(tmp_path, PATH_MAX, "%s.1", file_name); + if (ret >= PATH_MAX || ret < 0) { ERROR("Out of memory"); return -1; } diff --git a/src/services/execution/manager/container_unix.c b/src/services/execution/manager/container_unix.c index eb8cc28..eb34534 100644 --- a/src/services/execution/manager/container_unix.c +++ b/src/services/execution/manager/container_unix.c @@ -59,7 +59,7 @@ out: } /* notes: hostconfig and common_config will be free in this function on error */ -container_t *container_new(const char *runtime, const char *rootpath, const char *statepath, +container_t *container_new(const char *runtime, const char *rootpath, const char *statepath, const char *image_id, host_config **hostconfig, container_config_v2_common_config **common_config) { int ret = 0; @@ -103,6 +103,7 @@ container_t *container_new(const char *runtime, const char *rootpath, const char cont->runtime = util_strdup_s(runtime); cont->root_path = util_strdup_s(rootpath); cont->state_path = util_strdup_s(statepath); + cont->image_id = image_id != NULL ? util_strdup_s(image_id) : NULL; cont->state = container_state_new(); if (cont->state == NULL) { ERROR("Out of memory"); @@ -148,6 +149,8 @@ void container_free(container_t *container) container->root_path = NULL; free(container->state_path); container->state_path = NULL; + free(container->image_id); + container->image_id = NULL; free(container->log_path); container->log_path = NULL; @@ -700,8 +703,8 @@ static int save_json_config_file(const char *id, const char *rootpath, if (json_data == NULL || strlen(json_data) == 0) { return 0; } - 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; @@ -747,8 +750,8 @@ container_config_v2 *read_config_v2(const char *rootpath, const char *id) parser_error err = NULL; container_config_v2 *v2config = NULL; - nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, CONFIG_V2_JSON); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, id, CONFIG_V2_JSON); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); goto out; } @@ -781,8 +784,8 @@ static host_config *read_host_config(const char *rootpath, const char *id) parser_error err = NULL; host_config *hostconfig = NULL; - nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, HOSTCONFIGJSON); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, id, HOSTCONFIGJSON); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); goto out; } @@ -806,8 +809,8 @@ static bool check_start_generate_config(const char *rootpath, const char *id) parser_error err = NULL; container_start_generate_config *config = NULL; - nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, START_GENERATE_CONFIG); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, id, START_GENERATE_CONFIG); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); goto out; } @@ -878,6 +881,8 @@ static int container_save_config_v2(const container_t *cont) config_v2.state = cont->state->state; + config_v2.image = cont->image_id; + json_v2 = container_config_v2_generate_json(&config_v2, NULL, &err); if (json_v2 == NULL) { ERROR("Failed to generate container config V2 json string:%s", err ? err : " "); @@ -985,6 +990,7 @@ container_t *container_load(const char *runtime, const char *rootpath, const cha container_config_v2 *v2config = NULL; container_config_v2_common_config *common_config = NULL; host_config *hostconfig = NULL; + const char *image_id = NULL; container_t *cont = NULL; if (rootpath == NULL || statepath == NULL || id == NULL || runtime == NULL) { @@ -1008,8 +1014,9 @@ container_t *container_load(const char *runtime, const char *rootpath, const cha common_config = v2config->common_config; v2config->common_config = NULL; + image_id = v2config->image; - cont = container_new(runtime, rootpath, statepath, &hostconfig, &common_config); + cont = container_new(runtime, rootpath, statepath, image_id, &hostconfig, &common_config); if (cont == NULL) { ERROR("Failed to create container '%s'", id); goto error_out; @@ -1055,7 +1062,8 @@ static char *append_quote_to_arg(const char *arg) ERROR("Out of memory"); return NULL; } - if (sprintf_s(new_arg, total, "%s%s%s", part, arg, part) < 0) { + int nret = snprintf(new_arg, total, "%s%s%s", part, arg, part); + if (nret < 0 || (size_t)nret >= total) { free(new_arg); ERROR("Sprintf failed"); return NULL; @@ -1261,7 +1269,7 @@ static container_pid_t *parse_container_pid(const char *S) return NULL; } - num = sscanf_s(S, "%d %Lu %d %Lu", &P->pid, &P->start_time, &P->ppid, &P->pstart_time); + num = sscanf(S, "%d %Lu %d %Lu", &P->pid, &P->start_time, &P->ppid, &P->pstart_time); if (num != 4) { // args num to read is 4 ERROR("Call sscanf error: %s", errno ? strerror(errno) : ""); free(P); diff --git a/src/services/execution/manager/container_unix.h b/src/services/execution/manager/container_unix.h index 6536f6e..55d8311 100644 --- a/src/services/execution/manager/container_unix.h +++ b/src/services/execution/manager/container_unix.h @@ -43,6 +43,7 @@ typedef struct _container_t_ { char *runtime; char *root_path; char *state_path; + char *image_id; container_config_v2_common_config *common_config; container_state_t *state; host_config *hostconfig; @@ -60,8 +61,8 @@ void container_refinc(container_t *cont); void container_unref(container_t *cont); -container_t *container_new(const char *runtime, const char *rootpath, const char *statepath, host_config **hostconfig, - container_config_v2_common_config **common_config); +container_t *container_new(const char *runtime, const char *rootpath, const char *statepath, const char *image_id, + host_config **hostconfig, container_config_v2_common_config **common_config); container_t *container_load(const char *runtime, const char *rootpath, const char *statepath, const char *id); diff --git a/src/services/execution/manager/containers_gc.c b/src/services/execution/manager/containers_gc.c index f2d64c6..0566976 100644 --- a/src/services/execution/manager/containers_gc.c +++ b/src/services/execution/manager/containers_gc.c @@ -61,8 +61,8 @@ static int save_gc_config(const char *json_gc_config) goto out; } - nret = sprintf_s(filename, sizeof(filename), "%s/%s", rootpath, GCCONFIGJSON); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s", rootpath, GCCONFIGJSON); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); ret = -1; goto out; @@ -237,8 +237,8 @@ container_garbage_config *read_gc_config() goto out; } - nret = sprintf_s(filename, sizeof(filename), "%s/%s", rootpath, GCCONFIGJSON); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s", rootpath, GCCONFIGJSON); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); goto out; } diff --git a/src/services/execution/manager/health_check.c b/src/services/execution/manager/health_check.c index d7827f3..0e707e4 100644 --- a/src/services/execution/manager/health_check.c +++ b/src/services/execution/manager/health_check.c @@ -204,23 +204,12 @@ static ssize_t write_to_string(void *context, const void *data, size_t len) } if (len >= REV_BUF_SIZE) { - if (strncpy_s(dst, REV_BUF_SIZE, data, REV_BUF_SIZE - 4) != EOK) { - ERROR("Failed to set output"); - len = 0; - goto out; - } - if (strcpy_s(dst + REV_BUF_SIZE - 4, strlen("...") + 1, "...") != EOK) { - ERROR("Failed to append string to output"); - len = 0; - goto out; - } + (void)strncpy(dst, data, REV_BUF_SIZE - 4); + (void)strcpy(dst + REV_BUF_SIZE - 4, "..."); } else { - if (strncpy_s(dst, REV_BUF_SIZE, data, len) != EOK) { - len = 0; - goto out; - } + (void)strncpy(dst, data, len); } -out: + return (ssize_t)len; } diff --git a/src/services/execution/manager/monitord.c b/src/services/execution/manager/monitord.c index 5dc35ec..0567105 100644 --- a/src/services/execution/manager/monitord.c +++ b/src/services/execution/manager/monitord.c @@ -25,7 +25,6 @@ #include "log.h" #include "monitord.h" #include "mainloop.h" -#include "securec.h" #include "lcrd_config.h" #include "collector.h" #include "utils.h" @@ -46,8 +45,8 @@ char *lcrd_monitor_fifo_name(const char *rootpath) ERROR("Invalid parameter"); goto err; } - ret = sprintf_s(fifo_file_path, PATH_MAX, "%s/monitord_fifo", rootpath); - if (ret < 0 || ret >= PATH_MAX) { + ret = snprintf(fifo_file_path, PATH_MAX, "%s/monitord_fifo", rootpath); + if (ret < 0 || (size_t)ret >= PATH_MAX) { ERROR("Create monitord fifo path failed"); goto err; } diff --git a/src/services/execution/manager/restartmanager.c b/src/services/execution/manager/restartmanager.c index df7a781..a376eb6 100644 --- a/src/services/execution/manager/restartmanager.c +++ b/src/services/execution/manager/restartmanager.c @@ -26,7 +26,6 @@ #include "lcrd_config.h" #include "restartmanager.h" #include "utils.h" -#include "securec.h" #include "containers_store.h" #include "execution.h" #include "containers_gc.h" diff --git a/src/services/execution/manager/restore.c b/src/services/execution/manager/restore.c index 0d15370..60d9d15 100644 --- a/src/services/execution/manager/restore.c +++ b/src/services/execution/manager/restore.c @@ -20,7 +20,6 @@ #include #include "lcrd_config.h" -#include "securec.h" #include "log.h" #include "restore.h" #include "containers_store.h" @@ -47,8 +46,8 @@ static int restore_supervisor(const char *id, const char *runtime, const char *s char *exit_fifo = NULL; container_pid_t *pid_info = NULL; - nret = sprintf_s(container_state, sizeof(container_state), "%s/%s", statepath, id); - if (nret < 0 || (unsigned int)nret >= sizeof(container_state)) { + nret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id); + if (nret < 0 || (size_t)nret >= sizeof(container_state)) { ERROR("Failed to sprintf container state %s/%s", statepath, id); ret = -1; goto out; @@ -68,8 +67,8 @@ static int restore_supervisor(const char *id, const char *runtime, const char *s goto out; } - nret = sprintf_s(pidfile, sizeof(pidfile), "%s/pid.file", container_state); - if (nret < 0 || (unsigned int)nret >= sizeof(pidfile)) { + nret = snprintf(pidfile, sizeof(pidfile), "%s/pid.file", container_state); + if (nret < 0 || (size_t)nret >= sizeof(pidfile)) { close(exit_fifo_fd); ERROR("Failed to sprintf pidfile"); ret = -1; @@ -133,15 +132,15 @@ static int post_stopped_container_to_gc(const char *id, const char *runtime, con char pidfile[PATH_MAX] = { 0 }; container_pid_t *pid_info = NULL; - nret = sprintf_s(container_state, sizeof(container_state), "%s/%s", statepath, id); - if (nret < 0 || (unsigned int)nret >= sizeof(container_state)) { + nret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id); + if (nret < 0 || (size_t)nret >= sizeof(container_state)) { ERROR("Failed to sprintf container state %s/%s", statepath, id); ret = -1; goto out; } - nret = sprintf_s(pidfile, sizeof(pidfile), "%s/pid.file", container_state); - if (nret < 0 || (unsigned int)nret >= sizeof(pidfile)) { + nret = snprintf(pidfile, sizeof(pidfile), "%s/pid.file", container_state); + if (nret < 0 || (size_t)nret >= sizeof(pidfile)) { ERROR("Failed to sprintf pidfile"); ret = -1; goto out; @@ -177,14 +176,14 @@ static container_pid_t *load_running_container_pid_info(const container_t *cont) char container_state[PATH_MAX] = { 0 }; container_pid_t *pid_info = NULL; - nret = sprintf_s(container_state, sizeof(container_state), "%s/%s", cont->state_path, id); - if (nret < 0 || (unsigned int)nret >= sizeof(container_state)) { + nret = snprintf(container_state, sizeof(container_state), "%s/%s", cont->state_path, id); + if (nret < 0 || (size_t)nret >= sizeof(container_state)) { ERROR("Failed to sprintf container_state for container %s", id); goto out; } - nret = sprintf_s(pidfile, sizeof(pidfile), "%s/pid.file", container_state); - if (nret < 0 || (unsigned int)nret >= sizeof(pidfile)) { + nret = snprintf(pidfile, sizeof(pidfile), "%s/pid.file", container_state); + if (nret < 0 || (size_t)nret >= sizeof(pidfile)) { ERROR("Failed to sprintf pidfile"); goto out; } @@ -458,8 +457,8 @@ static int remove_invalid_container(const container_t *cont, const char *runtime char container_root[PATH_MAX] = { 0x00 }; char container_state[PATH_MAX] = { 0x00 }; - ret = sprintf_s(container_state, sizeof(container_state), "%s/%s", state, id); - if (ret < 0 || (unsigned int)ret >= sizeof(container_state)) { + ret = snprintf(container_state, sizeof(container_state), "%s/%s", state, id); + if (ret < 0 || (size_t)ret >= sizeof(container_state)) { ERROR("Failed to sprintf container state %s/%s", state, id); ret = -1; goto out; @@ -471,8 +470,8 @@ static int remove_invalid_container(const container_t *cont, const char *runtime goto out; } - ret = sprintf_s(container_root, sizeof(container_root), "%s/%s", root, id); - if (ret < 0 || (unsigned int)ret >= sizeof(container_root)) { + ret = snprintf(container_root, sizeof(container_root), "%s/%s", root, id); + if (ret < 0 || (size_t)ret >= sizeof(container_root)) { ERROR("Failed to sprintf invalid root directory %s/%s", root, id); ret = -1; goto out; diff --git a/src/services/execution/manager/supervisor.c b/src/services/execution/manager/supervisor.c index fd962c6..e1ae95e 100644 --- a/src/services/execution/manager/supervisor.c +++ b/src/services/execution/manager/supervisor.c @@ -21,7 +21,6 @@ #include "utils.h" #include "supervisor.h" #include "mainloop.h" -#include "securec.h" #include "liblcrd.h" #include "collector.h" #include "execution.h" @@ -65,8 +64,8 @@ char *exit_fifo_name(const char *cont_state_path) return NULL; } - ret = sprintf_s(fifo_path, sizeof(fifo_path), "%s/%s", cont_state_path, EXIT_FIFO); - if (ret < 0) { + ret = snprintf(fifo_path, sizeof(fifo_path), "%s/%s", cont_state_path, EXIT_FIFO); + if (ret < 0 || (size_t)ret >= sizeof(fifo_path)) { ERROR("sprintf buffer failed"); return NULL; } @@ -84,8 +83,8 @@ char *exit_fifo_create(const char *cont_state_path) return NULL; } - ret = sprintf_s(fifo_path, sizeof(fifo_path), "%s/%s", cont_state_path, EXIT_FIFO); - if (ret < 0) { + ret = snprintf(fifo_path, sizeof(fifo_path), "%s/%s", cont_state_path, EXIT_FIFO); + if (ret < 0 || (size_t)ret >= sizeof(fifo_path)) { ERROR("sprintf buffer failed"); return NULL; } diff --git a/src/services/execution/spec/specs.c b/src/services/execution/spec/specs.c index d7f9563..bb1785d 100644 --- a/src/services/execution/spec/specs.c +++ b/src/services/execution/spec/specs.c @@ -29,7 +29,6 @@ #include #include "error.h" -#include "securec.h" #include "log.h" #include "specs.h" #include "oci_runtime_spec.h" @@ -119,9 +118,9 @@ static int make_annotations_log_console(const oci_runtime_spec *oci_spec, const } } - nret = sprintf_s(tmp_str, sizeof(tmp_str), "%llu", - (unsigned long long)(custom_conf->log_config->log_file_rotate)); - if (nret < 0) { + nret = snprintf(tmp_str, sizeof(tmp_str), "%llu", + (unsigned long long)(custom_conf->log_config->log_file_rotate)); + if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) { ERROR("create rotate string failed"); ret = -1; goto out; @@ -218,8 +217,8 @@ static int make_annotations_oom_score_adj(const oci_runtime_spec *oci_spec, cons // oom_score_adj default value is 0, So there is no need to explicitly set this value if (host_spec->oom_score_adj != 0) { - int nret = sprintf_s(tmp_str, sizeof(tmp_str), "%d", host_spec->oom_score_adj); - if (nret < 0) { + int nret = snprintf(tmp_str, sizeof(tmp_str), "%d", host_spec->oom_score_adj); + if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) { ERROR("create oom score adj string failed"); ret = -1; goto out; @@ -244,8 +243,8 @@ static int make_annotations_files_limit(const oci_runtime_spec *oci_spec, const if (host_spec->files_limit != 0) { // need create new file limit item in annotations int64_t filelimit = host_spec->files_limit; - int nret = sprintf_s(tmp_str, sizeof(tmp_str), "%lld", (long long)filelimit); - if (nret < 0) { + int nret = snprintf(tmp_str, sizeof(tmp_str), "%lld", (long long)filelimit); + if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) { ERROR("create files limit string failed"); ret = -1; goto out; @@ -1469,7 +1468,8 @@ static int change_tmpfs_mount_size(const oci_runtime_spec *oci_spec, int64_t mem goto out; } /* set tmpfs mount size to half of container memory limit */ - if (sprintf_s(size_opt, sizeof(size_opt), "size=%lldk", (long long int)(memory_limit / 2048)) < 0) { + int nret = snprintf(size_opt, sizeof(size_opt), "size=%lldk", (long long int)(memory_limit / 2048)); + if (nret < 0 || (size_t)nret >= sizeof(size_opt)) { ERROR("Out of memory"); ret = -1; goto out; @@ -1834,8 +1834,8 @@ oci_runtime_spec *read_oci_config(const char *rootpath, const char *name) parser_error err = NULL; oci_runtime_spec *ociconfig = NULL; - nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, name, OCICONFIGJSON); - if (nret < 0) { + nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, name, OCICONFIGJSON); + if (nret < 0 || (size_t)nret >= sizeof(filename)) { ERROR("Failed to print string"); goto out; } diff --git a/src/services/execution/spec/specs_extend.c b/src/services/execution/spec/specs_extend.c index 2281de5..fb5a0c9 100644 --- a/src/services/execution/spec/specs_extend.c +++ b/src/services/execution/spec/specs_extend.c @@ -29,7 +29,6 @@ #include #include "error.h" -#include "securec.h" #include "log.h" #include "oci_runtime_spec.h" #include "host_config.h" @@ -284,7 +283,8 @@ static int do_append_env(char ***env, size_t *env_len, const char *key, const ch ERROR("Out of memory"); return -1; } - if (sprintf_s(tmp_env, tmp_env_len, "%s=%s", key, value) < 0) { + int nret = snprintf(tmp_env, tmp_env_len, "%s=%s", key, value); + if (nret < 0 || (size_t)nret >= tmp_env_len) { ERROR("Out of memory"); free(tmp_env); return -1; @@ -473,8 +473,8 @@ static int read_user_file(const char *basefs, const char *user_path, FILE **stre char path[PATH_MAX] = {0}; char real_path[PATH_MAX] = {0}; - nret = sprintf_s(path, sizeof(path), "%s/%s", basefs, user_path); - if (nret < 0) { + nret = snprintf(path, sizeof(path), "%s/%s", basefs, user_path); + if (nret < 0 || (size_t)nret >= sizeof(path)) { ERROR("Path is too long"); return -1; } @@ -1166,11 +1166,8 @@ int trans_ulimit_to_rlimit(oci_runtime_spec_process_rlimits_element **rlimit_dst goto out; } - if (strcat_s(typename, namelen, RLIMIT_PRE) != EOK) { - ERROR("Failed to cat string"); - ret = -1; - goto out; - } + (void)strcat(typename, RLIMIT_PRE); + for (j = 0; j < strlen(ulimit->name); j++) { typename[j + strlen(RLIMIT_PRE)] = (char)toupper((int)(ulimit->name[j])); } diff --git a/src/services/execution/spec/specs_mount.c b/src/services/execution/spec/specs_mount.c index 3cc643f..3c5cfa4 100644 --- a/src/services/execution/spec/specs_mount.c +++ b/src/services/execution/spec/specs_mount.c @@ -29,7 +29,6 @@ #include #include "error.h" -#include "securec.h" #include "log.h" #include "oci_runtime_spec.h" #include "oci_runtime_hooks.h" @@ -190,8 +189,8 @@ static bool valid_dirent_info(const char *dir, const struct dirent *info_archivo return false; } - nret = sprintf_s(fullpath, PATH_MAX, "%s/%s", dir, info_archivo->d_name); - if (nret < 0) { + nret = snprintf(fullpath, PATH_MAX, "%s/%s", dir, info_archivo->d_name); + if (nret < 0 || nret >= PATH_MAX) { ERROR("get_devices: Failed to combine device path"); return false; } @@ -302,8 +301,8 @@ static int get_devices(const char *dir, char ***devices, size_t *device_len, closedir(midir); return -1; } - nret = sprintf_s(fullpath, PATH_MAX, "%s/%s", dir, info_archivo->d_name); - if (nret < 0) { + nret = snprintf(fullpath, PATH_MAX, "%s/%s", dir, info_archivo->d_name); + if (nret < 0 || nret >= PATH_MAX) { ERROR("get_devices: Failed to combine device path"); closedir(midir); free(fullpath); @@ -782,9 +781,9 @@ static host_config_devices_element *parse_one_device(const char *device_path, co device_map->path_on_host = util_strdup_s(device_path); if (dir_container != NULL) { - nret = sprintf_s(tmp_container_path, sizeof(tmp_container_path), "%s/%s", - dir_container, device_path + strlen(dir_host)); - if (nret < 0 || (unsigned int)nret >= sizeof(tmp_container_path)) { + nret = snprintf(tmp_container_path, sizeof(tmp_container_path), "%s/%s", + dir_container, device_path + strlen(dir_host)); + if (nret < 0 || (size_t)nret >= sizeof(tmp_container_path)) { ERROR("Failed to sprintf device path in container %s/%s", dir_container, device_path + strlen(dir_host)); goto erro_out; } @@ -1380,8 +1379,8 @@ static container_config_v2_common_config_mount_points_element *defs_mnt_to_mount ERROR("Out of memory"); goto cleanup; } - pret = sprintf_s(new_mode, len, "%s,%s", mode, mnt->options[i]); - if (pret < 0) { + pret = snprintf(new_mode, len, "%s,%s", mode, mnt->options[i]); + if (pret < 0 || (size_t)pret >= len) { ERROR("Sprintf failed"); free(new_mode); goto cleanup; @@ -1926,7 +1925,9 @@ static int change_dev_shm_size(oci_runtime_spec *oci_spec, int64_t shm_size) size_t j = 0; char size_opt[MOUNT_PROPERTIES_SIZE] = { 0 }; char *tmp = NULL; - if (sprintf_s(size_opt, sizeof(size_opt), "size=%lld", (long long int)shm_size) < 0) { + + int nret = snprintf(size_opt, sizeof(size_opt), "size=%lld", (long long int)shm_size); + if (nret < 0 || (size_t)nret >= sizeof(size_opt)) { ERROR("Out of memory"); return -1; } diff --git a/src/services/execution/spec/specs_security.c b/src/services/execution/spec/specs_security.c index e077557..e9ff6fd 100644 --- a/src/services/execution/spec/specs_security.c +++ b/src/services/execution/spec/specs_security.c @@ -33,7 +33,6 @@ #endif #include "error.h" -#include "securec.h" #include "log.h" #include "oci_runtime_spec.h" #include "docker_seccomp.h" @@ -170,8 +169,8 @@ static int tweak_adds_capabilities(char ***new_caps, size_t *new_caps_len, const continue; } - nret = sprintf_s(tmpcap, sizeof(tmpcap), "CAP_%s", adds[i]); - if (nret < 0) { + nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", adds[i]); + if (nret < 0 || (size_t)nret >= sizeof(tmpcap)) { ERROR("Failed to print string"); ret = -1; goto out; @@ -211,8 +210,8 @@ static bool valid_drops_cap(const char **drops, size_t drops_len) continue; } - nret = sprintf_s(tmpcap, sizeof(tmpcap), "CAP_%s", drops[i]); - if (nret < 0) { + nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", drops[i]); + if (nret < 0 || (size_t)nret >= sizeof(tmpcap)) { ERROR("Failed to print string"); return false; } diff --git a/src/services/execution/spec/sysinfo.c b/src/services/execution/spec/sysinfo.c index 3abf5d7..8c77181 100644 --- a/src/services/execution/spec/sysinfo.c +++ b/src/services/execution/spec/sysinfo.c @@ -23,7 +23,6 @@ #include "liblcrd.h" #include "sysinfo.h" #include "log.h" -#include "securec.h" #include "read_file.h" // Cgroup Item Definition @@ -244,17 +243,9 @@ static char *cgroup_legacy_must_prefix_named(const char *entry) ERROR("Out of memory"); return NULL; } + (void)memcpy(prefixed, prefix, strlen(prefix)); + (void)memcpy(prefixed + strlen(prefix), entry, len); - if (memcpy_s(prefixed, len + strlen(prefix) + 1, prefix, strlen(prefix)) != EOK) { - ERROR("Failed to copy memory!"); - free(prefixed); - return NULL; - } - if (memcpy_s(prefixed + strlen(prefix), len + 1, entry, len) != EOK) { - ERROR("Failed to copy memory!"); - free(prefixed); - return NULL; - } prefixed[len + strlen(prefix)] = '\0'; return prefixed; } @@ -521,8 +512,8 @@ static bool cgroup_enabled(const char *mountpoint, const char *name) char path[PATH_MAX] = { 0 }; int nret; - nret = sprintf_s(path, sizeof(path), "%s/%s", mountpoint, name); - if (nret < 0) { + nret = snprintf(path, sizeof(path), "%s/%s", mountpoint, name); + if (nret < 0 || (size_t)nret >= sizeof(path)) { ERROR("Path is too long"); return false; } @@ -790,7 +781,6 @@ static void check_cgroup_blkio_info(struct layer **layers, bool quiet, cgroup_bl static void check_cgroup_cpuset_info(struct layer **layers, bool quiet, cgroup_cpuset_info_t *cpusetinfo) { size_t file_size = 0; - errno_t nret = EOK; char *mountpoint = NULL; char cpuset_cpus_path[PATH_MAX] = { 0 }; char cpuset_mems_path[PATH_MAX] = { 0 }; @@ -801,8 +791,8 @@ static void check_cgroup_cpuset_info(struct layer **layers, bool quiet, cgroup_c return; } - nret = sprintf_s(cpuset_cpus_path, sizeof(cpuset_cpus_path), "%s/%s", mountpoint, CGROUP_CPUSET_CPUS); - if (nret < 0) { + int nret = snprintf(cpuset_cpus_path, sizeof(cpuset_cpus_path), "%s/%s", mountpoint, CGROUP_CPUSET_CPUS); + if (nret < 0 || (size_t)nret >= sizeof(cpuset_cpus_path)) { ERROR("Path is too long"); goto error; } @@ -813,8 +803,8 @@ static void check_cgroup_cpuset_info(struct layer **layers, bool quiet, cgroup_c goto error; } - nret = sprintf_s(cpuset_mems_path, sizeof(cpuset_mems_path), "%s/%s", mountpoint, CGROUP_CPUSET_MEMS); - if (nret < 0) { + nret = snprintf(cpuset_mems_path, sizeof(cpuset_mems_path), "%s/%s", mountpoint, CGROUP_CPUSET_MEMS); + if (nret < 0 || (size_t)nret >= sizeof(cpuset_mems_path)) { ERROR("Path is too long"); goto error; } @@ -962,8 +952,8 @@ static void check_cgroup_hugetlb(struct layer **layers, bool quiet, cgroup_huget WARN("Your kernel does not support cgroup hugetlb limit"); return; } - nret = sprintf_s(hugetlbpath, sizeof(hugetlbpath), "hugetlb.%s.limit_in_bytes", defaultpagesize); - if (nret < 0) { + nret = snprintf(hugetlbpath, sizeof(hugetlbpath), "hugetlb.%s.limit_in_bytes", defaultpagesize); + if (nret < 0 || (size_t)nret >= sizeof(hugetlbpath)) { WARN("Failed to print hugetlb path"); goto free_out; } @@ -1068,8 +1058,8 @@ static bool is_huge_pagesize_valid(const char *pagesize) } for (it = hps; *it; it++) { - nret = sprintf_s(hpsbuf, sizeof(hpsbuf), "%s ", *it); - if (nret < 0) { + nret = snprintf(hpsbuf, sizeof(hpsbuf), "%s ", *it); + if (nret < 0 || (size_t)nret >= sizeof(hpsbuf)) { ERROR("hps buf is too short"); goto free_out; } diff --git a/src/services/graphdriver/driver.c b/src/services/graphdriver/driver.c index 251a277..ce40134 100644 --- a/src/services/graphdriver/driver.c +++ b/src/services/graphdriver/driver.c @@ -159,8 +159,8 @@ void graphdriver_umount_mntpoint(void) if (strcmp(driver_name, "overlay2") == 0) { driver_name[strlen(driver_name) - 1] = '\0'; } - nret = sprintf_s(mp, sizeof(mp), "%s/%s", root, driver_name); - if (nret < 0) { + nret = snprintf(mp, sizeof(mp), "%s/%s", root, driver_name); + if (nret < 0 || (size_t)nret >= sizeof(mp)) { WARN("Failed to print string"); goto cleanup; } diff --git a/src/services/image/image_cb.c b/src/services/image/image_cb.c index b32a933..26fdee8 100644 --- a/src/services/image/image_cb.c +++ b/src/services/image/image_cb.c @@ -30,7 +30,6 @@ #include "liblcrd.h" #include "log.h" #include "image.h" -#include "securec.h" #include "engine.h" #include "lcrd_config.h" #include "mediatype.h" diff --git a/src/sha256/sha256.c b/src/sha256/sha256.c index 534ba79..5f43c9b 100644 --- a/src/sha256/sha256.c +++ b/src/sha256/sha256.c @@ -25,7 +25,6 @@ #include #include "sha256.h" -#include "securec.h" #include "log.h" #include "utils.h" diff --git a/src/sysctl_tools.c b/src/sysctl_tools.c index ad631b2..305575d 100644 --- a/src/sysctl_tools.c +++ b/src/sysctl_tools.c @@ -20,7 +20,6 @@ #include #include #include -#include #include "sysctl_tools.h" #include "utils.h" @@ -34,8 +33,8 @@ int get_sysctl(const char *sysctl, char **err) char fullpath[PATH_MAX] = { 0 }; char buff[MAX_BUFFER_SIZE] = { 0 }; - ret = sprintf_s(fullpath, PATH_MAX, "%s/%s", SYSCTL_BASE, sysctl); - if (ret < 0) { + ret = snprintf(fullpath, PATH_MAX, "%s/%s", SYSCTL_BASE, sysctl); + if (ret < 0 || ret >= PATH_MAX) { *err = util_strdup_s("Out of memory"); goto free_out; } @@ -81,13 +80,13 @@ int set_sysctl(const char *sysctl, int new_value, char **err) char fullpath[PATH_MAX] = { 0 }; char buff[LCRD_NUMSTRLEN64] = { 0 }; - ret = sprintf_s(fullpath, PATH_MAX, "%s/%s", SYSCTL_BASE, sysctl); - if (ret < 0) { + ret = snprintf(fullpath, PATH_MAX, "%s/%s", SYSCTL_BASE, sysctl); + if (ret < 0 || ret >= PATH_MAX) { *err = util_strdup_s("Out of memory"); goto free_out; } - ret = sprintf_s(buff, LCRD_NUMSTRLEN64, "%d", new_value); - if (ret < 0) { + ret = snprintf(buff, LCRD_NUMSTRLEN64, "%d", new_value); + if (ret < 0 || ret >= LCRD_NUMSTRLEN64) { *err = util_strdup_s("Out of memory"); goto free_out; } diff --git a/src/tar/lcrdtar.c b/src/tar/lcrdtar.c index 6a6d07b..4a693e5 100644 --- a/src/tar/lcrdtar.c +++ b/src/tar/lcrdtar.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include "lcrdtar.h" @@ -240,8 +239,8 @@ static int get_rebase_name(const char *path, const char *real_path, char *path_base = NULL; char *resolved_base = NULL; - nret = sprintf_s(resolved, PATH_MAX, "%s", real_path); - if (nret < 0) { + nret = snprintf(resolved, PATH_MAX, "%s", real_path); + if (nret < 0 || nret >= PATH_MAX) { ERROR("Failed to print string"); return -1; } @@ -319,8 +318,8 @@ int resolve_host_source_path(const char *path, bool follow_link, format_errorf(err, "Can not get real path of %s: %s", dirpath, strerror(errno)); goto cleanup; } - nret = sprintf_s(resolved, sizeof(resolved), "%s/%s", real_path, basepath); - if (nret < 0) { + nret = snprintf(resolved, sizeof(resolved), "%s/%s", real_path, basepath); + if (nret < 0 || (size_t)nret >= sizeof(resolved)) { ERROR("Path is too long"); goto cleanup; } @@ -538,8 +537,8 @@ static char *format_transform_of_tar(const char *srcbase, const char *dstbase) ERROR("Out of memory"); return NULL; } - nret = sprintf_s(transform, len, "s/%s/%s/", src_escaped, dst_escaped); - if (nret < 0) { + nret = snprintf(transform, len, "s/%s/%s/", src_escaped, dst_escaped); + if (nret < 0 || (size_t)nret >= len) { ERROR("Failed to print string"); free(transform); return NULL; diff --git a/src/types_def.c b/src/types_def.c index a099253..883f06e 100644 --- a/src/types_def.c +++ b/src/types_def.c @@ -13,8 +13,9 @@ * Description: provide typedef functions ********************************************************************************/ #include "types_def.h" -#include #include +#include +#include #include "log.h" #include "utils.h" @@ -105,10 +106,7 @@ bool get_timestamp(const char *str_time, types_timestamp_t *timestamp) int32_t nanos = 0; struct tm tm_day; - if (memset_s(&tm_day, sizeof(tm_day), 0, sizeof(tm_day)) != EOK) { - ERROR("Failed to memset memory"); - return false; - } + (void)memset(&tm_day, 0, sizeof(tm_day)); if (timestamp == NULL || str_time == NULL) { return false; @@ -162,13 +160,13 @@ bool get_time_buffer(const types_timestamp_t *timestamp, char *timebuffer, size_ } if (tm_zone >= 0) { - nret = sprintf_s(timebuffer + strlen(timebuffer), - maxsize - strlen(timebuffer), ".%09d+%02d:00", nanos, tm_zone); + nret = snprintf(timebuffer + strlen(timebuffer), + maxsize - strlen(timebuffer), ".%09d+%02d:00", nanos, tm_zone); } else { - nret = sprintf_s(timebuffer + strlen(timebuffer), - maxsize - strlen(timebuffer), ".%09d-%02d:00", nanos, -tm_zone); + nret = snprintf(timebuffer + strlen(timebuffer), + maxsize - strlen(timebuffer), ".%09d-%02d:00", nanos, -tm_zone); } - if (nret < 0) { + if (nret < 0 || nret >= maxsize - strlen(timebuffer)) { ERROR("sprintf timebuffer failed"); return false; } @@ -614,7 +612,7 @@ static bool check_human_duration_less_1_sec(int64_t seconds) static int gen_human_duration_less_1_sec(int64_t secondes, char *str, size_t len) { - return sprintf_s(str, len, "Less than a second"); + return snprintf(str, len, "Less than a second"); } static bool check_human_duration_less_60_secs(int64_t seconds) @@ -624,7 +622,7 @@ static bool check_human_duration_less_60_secs(int64_t seconds) static int gen_human_duration_less_60_secs(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld seconds", (long long)seconds); + return snprintf(str, len, "%lld seconds", (long long)seconds); } static bool check_human_duration_eq_1_min(int64_t seconds) @@ -634,7 +632,7 @@ static bool check_human_duration_eq_1_min(int64_t seconds) static int gen_human_duration_eq_1_min(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "About a minute"); + return snprintf(str, len, "About a minute"); } static bool check_human_duration_less_60_mins(int64_t seconds) @@ -644,7 +642,7 @@ static bool check_human_duration_less_60_mins(int64_t seconds) static int gen_human_duration_less_60_mins(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld minutes", (long long)seconds / 60); + return snprintf(str, len, "%lld minutes", (long long)seconds / 60); } static bool check_human_duration_eq_1_hour(int64_t seconds) @@ -654,7 +652,7 @@ static bool check_human_duration_eq_1_hour(int64_t seconds) static int gen_human_duration_eq_1_hour(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "About an hour"); + return snprintf(str, len, "About an hour"); } static bool check_human_duration_less_48_hours(int64_t seconds) @@ -664,7 +662,7 @@ static bool check_human_duration_less_48_hours(int64_t seconds) static int gen_human_duration_less_48_hours(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld hours", (long long)seconds / (60 * 60)); + return snprintf(str, len, "%lld hours", (long long)seconds / (60 * 60)); } static bool check_human_duration_less_7_days(int64_t seconds) @@ -674,7 +672,7 @@ static bool check_human_duration_less_7_days(int64_t seconds) static int gen_human_duration_less_7_days(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld days", (long long)seconds / (60 * 60 * 24)); + return snprintf(str, len, "%lld days", (long long)seconds / (60 * 60 * 24)); } static bool check_human_duration_less_90_days(int64_t seconds) @@ -684,7 +682,7 @@ static bool check_human_duration_less_90_days(int64_t seconds) static int gen_human_duration_less_90_days(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld weeks", (long long)seconds / (60 * 60 * 24 * 7)); + return snprintf(str, len, "%lld weeks", (long long)seconds / (60 * 60 * 24 * 7)); } static bool check_human_duration_less_2_years(int64_t seconds) @@ -694,7 +692,7 @@ static bool check_human_duration_less_2_years(int64_t seconds) static int gen_human_duration_less_2_years(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld months", (long long)seconds / (60 * 60 * 24 * 30)); + return snprintf(str, len, "%lld months", (long long)seconds / (60 * 60 * 24 * 30)); } static bool check_human_duration_default(int64_t seconds) @@ -704,7 +702,7 @@ static bool check_human_duration_default(int64_t seconds) static int gen_human_duration_default(int64_t seconds, char *str, size_t len) { - return sprintf_s(str, len, "%lld years", (long long)seconds / (60 * 60 * 24 * 365)); + return snprintf(str, len, "%lld years", (long long)seconds / (60 * 60 * 24 * 365)); } typedef struct time_human_duration_rule_t time_human_duration_rule; @@ -769,7 +767,7 @@ static bool time_human_duration(int64_t seconds, char *str, size_t len) } } - if (nret < 0) { + if (nret < 0 || nret >= len) { ERROR("Sprintf buffer failed"); return false; } @@ -779,7 +777,8 @@ static bool time_human_duration(int64_t seconds, char *str, size_t len) static int time_format_duration_bad(char *out, size_t len) { - if (sprintf_s(out, len, "-") < 0) { + int nret = snprintf(out, len, "-"); + if (nret < 0 || (size_t)nret >= len) { return -1; /* format failed, return -1 */ } return 1; /* format ok with bad data, return 1 */ @@ -831,10 +830,7 @@ int time_format_duration_ago(const char *in, char *out, size_t len) } if (strcmp(out, "-") != 0 && strlen(out) + 5 < len) { - if (strcat_s(out, len, " ago") != EOK) { - ERROR("Strcat string error"); - return -1; - } + (void)strcat(out, " ago"); } return 0; diff --git a/src/websocket/service/ws_server.cc b/src/websocket/service/ws_server.cc index 2d8b1a8..718df10 100644 --- a/src/websocket/service/ws_server.cc +++ b/src/websocket/service/ws_server.cc @@ -19,7 +19,6 @@ #include #include #include -#include #include "cxxutils.h" #include "log.h" #include "utils.h" @@ -115,10 +114,8 @@ int WebsocketServer::CreateContext() m_url.SetScheme("ws"); m_url.SetHost("localhost:" + std::to_string(m_listenPort)); - if (memset_s(&info, sizeof(struct lws_context_creation_info), 0, sizeof(info)) != EOK) { - ERROR("Failed to set memory!"); - return -1; - } + + (void)memset(&info, 0, sizeof(info)); lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG, WebsocketServer::EmitLog); info.port = m_listenPort; @@ -285,10 +282,7 @@ int WebsocketServer::Wswrite(struct lws *wsi, void *in, size_t len) ERROR("ERROR %d writing to socket, hanging up", n); return -1; } - if (memset_s(buf, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1, - 0, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1) != EOK) { - ERROR("Failed to set memory"); - } + (void)memset(buf, 0, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1); it->second.buf_mutex->unlock(); } @@ -302,10 +296,7 @@ void WebsocketServer::Receive(struct lws *wsi, void *user, void *in, size_t len) pss->final = lws_is_final_fragment(wsi); pss->binary = lws_frame_is_binary(wsi); - if (memcpy_s(&pss->buf[LWS_PRE], MAX_ECHO_PAYLOAD, in, len) != EOK) { - ERROR("failed to copy memory!"); - return; - } + (void)memcpy(&pss->buf[LWS_PRE], in, len); pss->len = (unsigned int)len; pss->rx += len; lws_rx_flow_control(wsi, 0); @@ -436,15 +427,10 @@ ssize_t WsWriteToClient(void *context, const void *data, size_t len) it->second.buf_mutex->lock(); auto &buf = it->second.buf; // Determine if it is standard output channel or error channel? - if (memset_s(buf, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1, - 0, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1) != EOK) { - ERROR("Failed to set memory"); - } + (void)memset(buf, 0, LWS_PRE + MAX_MSG_BUFFER_SIZE + 1); buf[LWS_PRE] = STDOUTCHANNEL; - if (memcpy_s(&buf[LWS_PRE + 1], MAX_MSG_BUFFER_SIZE, (void *)data, len) != EOK) { - ERROR("failed to copy memory!"); - return 0; - } + + (void)memcpy(&buf[LWS_PRE + 1], (void *)data, len); auto start = std::chrono::system_clock::now(); lws_callback_on_writable(wsi); it->second.buf_mutex->unlock(); diff --git a/test/llt.sh b/test/llt.sh old mode 100644 new mode 100755 diff --git a/tools/static_check b/tools/static_check old mode 100644 new mode 100755 diff --git a/update-version.bash b/update-version.bash old mode 100644 new mode 100755 -- Gitee