提交 c28341fc 编写于 作者: O openeuler-iSula

lcr: remove seucrec new

Signed-off-by: Nopeneuler-iSula <isula@huawei.com>
上级 3b37e6bf
......@@ -81,7 +81,6 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
OPTION(ENABLE_LLT "llt switch" OFF)
IF(ENABLE_LLT)
enable_testing()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test)
ENDIF(ENABLE_LLT)
# install all files
......
......@@ -14,13 +14,6 @@ endmacro()
find_program(CMD_PYTHON python)
_CHECK(CMD_PYTHON "CMD_PYTHON-NOTFOUND" "python")
# check securec
find_path(LIBSECUREC_INCLUDE_DIR securec.h)
_CHECK(LIBSECUREC_INCLUDE_DIR "LIBSECUREC_INCLUDE_DIR-NOTFOUND" "securec.h")
find_library(LIBSECUREC_LIBRARY securec)
_CHECK(LIBSECUREC_LIBRARY "LIBSECUREC_LIBRARY-NOTFOUND" "libsecurec.so")
# check liblxc
pkg_check_modules(PC_LIBLXC REQUIRED "lxc>=3")
find_path(LIBLXC_INCLUDE_DIR lxc/lxccontainer.h
......
......@@ -14,7 +14,6 @@ BuildRequires: cmake
BuildRequires: lxc
BuildRequires: lxc-devel
BuildRequires: yajl yajl-devel
BuildRequires: libsecurec libsecurec-devel
Requires: rsync bridge-utils lxc
ExclusiveArch: x86_64 aarch64
......
......@@ -15,12 +15,10 @@ message("-- Get top json srcs: " ${topjsonsrcs})
add_library(liblcr ${LIBTYPE} ${topsrcs} ${topjsonsrcs} ${commonjsonsrcs} ${generatesrcs})
set(check_incs
${LIBSECUREC_INCLUDE_DIR}
${LIBLXC_INCLUDE_DIR}
${LIBYAJL_INCLUDE_DIR}
)
set(check_libs
${LIBSECUREC_LIBRARY}
${LIBLXC_LIBRARY}
${LIBYAJL_LIBRARY}
)
......
......@@ -16,7 +16,6 @@
#include <stdio.h>
#include <string.h>
#include <securec.h>
#include "buffer.h"
#include "log.h"
......@@ -84,7 +83,6 @@ static bool buffer_has_space(const Buffer *buf, size_t desired_length)
static int buffer_grow(Buffer *buf, size_t minimum_size)
{
size_t factor = 0;
errno_t ret = 0;
size_t new_size = 0;
char *tmp = NULL;
......@@ -112,12 +110,7 @@ static int buffer_grow(Buffer *buf, size_t minimum_size)
return -1;
}
ret = memcpy_s(tmp, new_size, buf->contents, buf->total_size);
if (ret != EOK) {
ERROR("Failed to copy memory");
free(tmp);
return -1;
}
(void)memcpy(tmp, buf->contents, buf->total_size);
free(buf->contents);
buf->contents = tmp;
......@@ -192,7 +185,7 @@ int buffer_nappendf(Buffer *buf, size_t length, const char *format, ...)
}
va_start(argp, format);
status = vsprintf_s(tmp, printf_length, format, argp);
status = vsprintf(tmp, format, argp);
va_end(argp);
if (status < 0) {
goto error;
......@@ -229,11 +222,7 @@ char *buffer_to_s(const Buffer *buf)
if (result == NULL) {
return NULL;
}
if (strncpy_s(result, len + 1, buf->contents, len) != EOK) {
ERROR("Failed to copy string!");
free(result);
return NULL;
}
(void)strncpy(result, buf->contents, len);
return result;
}
......@@ -29,7 +29,6 @@
#include "utils.h"
#include "log.h"
#include "buffer.h"
#include "securec.h"
#define SUB_UID_PATH "/etc/subuid"
#define SUB_GID_PATH "/etc/subgid"
......@@ -299,7 +298,6 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len)
size_t sep_len;
size_t result_len;
size_t iter;
int nret = 0;
sep_len = strlen(sep);
if (valid_sep_len(sep_len, len) == false) {
......@@ -322,20 +320,10 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len)
}
for (iter = 0; iter < len - 1; iter++) {
nret = strcat_s(result, result_len + 1, &(parts[iter][4]));
if (nret != EOK) {
goto err_out;
}
nret = strcat_s(result, result_len + 1, sep);
if (nret != EOK) {
goto err_out;
}
}
nret = strcat_s(result, result_len + 1, &(parts[len - 1][4]));
if (nret != EOK) {
goto err_out;
(void)strcat(result, &(parts[iter][4]));
(void)strcat(result, sep);
}
(void)strcat(result, &(parts[len - 1][4]));
// Lower case
for (iter = 0; iter < result_len; iter++) {
......@@ -345,10 +333,6 @@ static char *capabilities_join(const char *sep, const char **parts, size_t len)
}
return result;
err_out:
free(result);
return NULL;
}
#define UID_MAX_SIZE 21
......@@ -360,8 +344,8 @@ static int trans_oci_process_init_uid(const oci_runtime_spec_process *proc, stru
int nret;
int ret = -1;
if (proc->user != NULL && proc->user->uid != INVALID_INT) {
nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid);
if (nret < 0) {
nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->uid);
if (nret < 0 || (size_t)nret >= sizeof(buf)) {
goto out;
}
......@@ -384,8 +368,8 @@ static int trans_oci_process_init_gid(const oci_runtime_spec_process *proc, stru
int nret;
int ret = -1;
if (proc->user != NULL && proc->user->gid != INVALID_INT) {
nret = sprintf_s(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid);
if (nret < 0) {
nret = snprintf(buf, sizeof(buf), "%u", (unsigned int)proc->user->gid);
if (nret < 0 || (size_t)nret >= sizeof(buf)) {
goto out;
}
......@@ -418,16 +402,16 @@ static int trans_oci_process_init_groups(const oci_runtime_spec_process *proc, s
goto out;
}
nret = sprintf_s(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0]));
if (nret < 0) {
nret = snprintf(gids, total_len, "%u", (unsigned int)(proc->user->additional_gids[0]));
if (nret < 0 || (size_t)nret >= total_len) {
free(gids);
goto out;
}
for (i = 1; i < proc->user->additional_gids_len; i++) {
size_t old_len = strlen(gids);
nret = sprintf_s(gids + old_len, total_len - old_len, " %u",
(unsigned int)(proc->user->additional_gids[i]));
if (nret < 0) {
nret = snprintf(gids + old_len, total_len - old_len, " %u",
(unsigned int)(proc->user->additional_gids[i]));
if (nret < 0 || (size_t)nret >= (total_len - old_len)) {
free(gids);
goto out;
}
......@@ -577,16 +561,16 @@ static int trans_oci_process_prlimit(const oci_runtime_spec_process *proc, struc
}
// Skip `RLIMIT_`
nret = sprintf_s(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7]));
nret = snprintf(buf_key, sizeof(buf_key), "lxc.prlimit.%s", &(type[7]));
free(type);
if (nret < 0) {
if (nret < 0 || (size_t)nret >= sizeof(buf_key)) {
goto out;
}
// We always use format `soft_limit:hard_limit`
nret = sprintf_s(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft,
(unsigned long long)lr->hard);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%llu:%llu", (unsigned long long)lr->soft,
(unsigned long long)lr->hard);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -766,8 +750,8 @@ static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, stru
goto out;
}
value = tmpvalue;
nret = sprintf_s(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation);
if (nret < 0) {
nret = snprintf(value + strlen(value), newsize - strlen(value), ",%s", linux->rootfs_propagation);
if (nret < 0 || (size_t)nret >= (newsize - strlen(value))) {
ERROR("Failed to print string");
goto out;
}
......@@ -952,8 +936,8 @@ static struct lcr_list *trans_mount_auto_to_lxc(const defs_mount *mount)
goto out_free;
}
ret = sprintf_s(buf, buf_len, "%s:%s", type, options);
if (ret < 0) {
ret = snprintf(buf, buf_len, "%s:%s", type, options);
if (ret < 0 || (size_t)ret >= buf_len) {
DEBUG("Failed to print string");
goto out_free;
}
......@@ -1000,8 +984,8 @@ static struct lcr_list *trans_mount_entry_to_lxc(const defs_mount *mount)
goto out_free;
}
ret = sprintf_s(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options);
if (ret < 0) {
ret = snprintf(buf, buf_len, "%s %s %s %s 0 0", replaced_source, replaced_dest + 1, mount->type, options);
if (ret < 0 || (size_t)ret >= buf_len) {
ERROR("Failed to print string");
goto out_free;
}
......@@ -1121,8 +1105,8 @@ static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, cons
char buf_value[300] = { 0 };
char subid[ID_MAP_LEN] = { 0 };
nret = sprintf_s(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%s %u %u %u", typ, id->container_id, id->host_id, id->size);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
return -1;
}
......@@ -1132,8 +1116,8 @@ static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, cons
}
lcr_list_add_tail(conf, node);
nret = sprintf_s(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size);
if (nret < 0) {
nret = snprintf(subid, sizeof(subid), "%u:%u:%u", id->container_id, id->host_id, id->size);
if (nret < 0 || (size_t)nret >= sizeof(subid)) {
return -1;
}
nret = util_atomic_write_file(path, subid);
......@@ -1208,8 +1192,8 @@ static int trans_conf_int(struct lcr_list *conf, const char *lxc_key, int val)
char buf_value[300] = { 0 };
int nret;
nret = sprintf_s(buf_value, sizeof(buf_value), "%d", val);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%d", val);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
return -1;
}
node = create_lcr_list_node(lxc_key, buf_value);
......@@ -1226,8 +1210,8 @@ static int trans_conf_uint32(struct lcr_list *conf, const char *lxc_key, uint32_
char buf_value[300] = { 0 };
int nret;
nret = sprintf_s(buf_value, sizeof(buf_value), "%u", (unsigned int)val);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%u", (unsigned int)val);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
return -1;
}
node = create_lcr_list_node(lxc_key, buf_value);
......@@ -1244,8 +1228,8 @@ static int trans_conf_int64(struct lcr_list *conf, const char *lxc_key, int64_t
char buf_value[300] = { 0 };
int nret;
nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)val);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)val);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
return -1;
}
node = create_lcr_list_node(lxc_key, buf_value);
......@@ -1262,8 +1246,8 @@ static int trans_conf_uint64(struct lcr_list *conf, const char *lxc_key, uint64_
char buf_value[300] = { 0 };
int nret;
nret = sprintf_s(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%llu", (unsigned long long)val);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
return -1;
}
node = create_lcr_list_node(lxc_key, buf_value);
......@@ -1412,11 +1396,11 @@ static int trans_resources_devices_no_match(const oci_runtime_defs_linux_device_
{
int ret = 0;
if (lrd->minor != WILDCARD) {
ret = sprintf_s(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
(long long)lrd->minor, lrd->access ? lrd->access : "rwm");
ret = snprintf(buf_value, size, "%s %lld:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
(long long)lrd->minor, lrd->access ? lrd->access : "rwm");
} else {
ret = sprintf_s(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
lrd->access ? lrd->access : "rwm");
ret = snprintf(buf_value, size, "%s %lld:* %s", lrd->type ? lrd->type : "a", (long long)(lrd->major),
lrd->access ? lrd->access : "rwm");
}
return ret;
......@@ -1426,10 +1410,10 @@ static int trans_resources_devices_match(const oci_runtime_defs_linux_device_cgr
{
int ret = 0;
if (lrd->minor != WILDCARD) {
ret = sprintf_s(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor),
lrd->access ? lrd->access : "rwm");
ret = snprintf(buf_value, size, "%s *:%lld %s", lrd->type ? lrd->type : "a", (long long)(lrd->minor),
lrd->access ? lrd->access : "rwm");
} else {
ret = sprintf_s(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm");
ret = snprintf(buf_value, size, "%s *:* %s", lrd->type ? lrd->type : "a", lrd->access ? lrd->access : "rwm");
}
return ret;
......@@ -1610,9 +1594,9 @@ static int trans_blkio_wdevice(const oci_runtime_config_linux_resources_block_io
int nret;
oci_runtime_defs_linux_block_io_device_weight *wd = block_io->weight_device[i];
if ((wd != NULL) && wd->weight != INVALID_INT) {
nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor,
wd->weight);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major), (long long)wd->minor,
wd->weight);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -1623,9 +1607,9 @@ static int trans_blkio_wdevice(const oci_runtime_config_linux_resources_block_io
lcr_list_add_tail(conf, node);
}
if ((wd != NULL) && wd->leaf_weight != INVALID_INT) {
nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major),
(long long)(wd->minor), wd->leaf_weight);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %d", (long long)(wd->major),
(long long)(wd->minor), wd->leaf_weight);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -1657,9 +1641,9 @@ static int trans_blkio_throttle(oci_runtime_defs_linux_block_io_device_throttle
if (throttle[i] && throttle[i]->rate != INVALID_INT) {
int nret;
char buf_value[300] = { 0x00 };
nret = sprintf_s(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major,
(long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate));
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%lld:%lld %llu", (long long)throttle[i]->major,
(long long)(throttle[i]->minor), (unsigned long long)(throttle[i]->rate));
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -1727,7 +1711,8 @@ static int trans_resources_hugetlb(const oci_runtime_config_linux_resources *res
for (i = 0; i < res->hugepage_limits_len; i++) {
oci_runtime_config_linux_resources_hugepage_limits_element *lrhl = res->hugepage_limits[i];
if (lrhl->page_size != NULL) {
if (sprintf_s(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size) < 0) {
int nret = snprintf(buf_key, sizeof(buf_key), "lxc.cgroup.hugetlb.%s.limit_in_bytes", lrhl->page_size);
if (nret < 0 || (size_t)nret >= sizeof(buf_key)) {
goto out;
}
......@@ -1762,7 +1747,8 @@ static int trans_resources_network(const oci_runtime_config_linux_resources *res
for (i = 0; i < res->network->priorities_len; i++) {
oci_runtime_defs_linux_network_interface_priority *lrnp = res->network->priorities[i];
if ((lrnp != NULL) && lrnp->name != NULL && lrnp->priority != INVALID_INT) {
if (sprintf_s(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority) < 0) {
int nret = snprintf(buf_value, sizeof(buf_value), "%s %u", lrnp->name, lrnp->priority);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -1792,11 +1778,11 @@ static int trans_resources_pids(const oci_runtime_config_linux_resources *res, s
if (res->pids->limit != INVALID_INT) {
int nret;
if (res->pids->limit == -1) {
nret = sprintf_s(buf_value, sizeof(buf_value), "max");
nret = snprintf(buf_value, sizeof(buf_value), "max");
} else {
nret = sprintf_s(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit));
nret = snprintf(buf_value, sizeof(buf_value), "%lld", (long long)(res->pids->limit));
}
if (nret < 0) {
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
goto out;
}
......@@ -2001,10 +1987,10 @@ static struct lcr_list *trans_oci_linux_devices(const oci_runtime_config_linux *
/* lxc.populate_device = PATH_IN_CONTAINER:DEVICETYPE:MAJOR:MINOR:MODE:UID:GID
* For e.g. lxc.populate_device = /dev/sda:b:8:0:0666:0:0
*/
nret = sprintf_s(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type,
(long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid,
device->gid);
if (nret < 0) {
nret = snprintf(buf_value, sizeof(buf_value), "%s:%s:%lld:%lld:%d:%u:%u", device->path, device->type,
(long long int)(device->major), (long long int)(device->minor), device->file_mode, device->uid,
device->gid);
if (nret < 0 || (size_t)nret >= sizeof(buf_value)) {
ERROR("Failed to get populate device string");
goto out_free;
}
......@@ -2293,8 +2279,8 @@ static struct lcr_list *trans_oci_linux_sysctl(const json_map_string_string *sys
for (i = 0; i < sysctl->len; i++) {
char sysk[BUFSIZ] = { 0 };
int nret = sprintf_s(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]);
if (nret < 0) {
int nret = snprintf(sysk, sizeof(sysk), "lxc.sysctl.%s", sysctl->keys[i]);
if (nret < 0 || (size_t)nret >= sizeof(sysk)) {
ERROR("Failed to print string");
goto out_free;
}
......
......@@ -13,8 +13,9 @@
* Description: provide container error definition
******************************************************************************/
#include "utils.h"
#include "securec.h"
#include "error.h"
#include <stdarg.h>
#include <stdlib.h>
// record the lcr error
__thread engine_error_t g_lcr_error = {
......@@ -58,7 +59,7 @@ void lcr_set_error_message(lcr_errno_t errcode, const char *format, ...)
va_list argp;
va_start(argp, format);
ret = vsprintf_s(errbuf, BUFSIZ, format, argp);
ret = vsprintf(errbuf, format, argp);
va_end(argp);
clear_error_message(&g_lcr_error);
if (ret < 0) {
......@@ -79,7 +80,7 @@ void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...)
return;
}
va_start(argp, format);
ret = vsprintf_s(errbuf, BUFSIZ, format, argp);
ret = vsprintf(errbuf, format, argp);
va_end(argp);
clear_error_message(&g_lcr_error);
if (ret < 0) {
......@@ -99,7 +100,7 @@ void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...)
va_list argp;
va_start(argp, format);
ret = vsprintf_s(errbuf, BUFSIZ, format, argp);
ret = vsprintf(errbuf, format, argp);
va_end(argp);
if (ret < 0) {
g_lcr_error.errcode = LCR_ERR_FORMAT;
......
......@@ -16,9 +16,9 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <securec.h>
#include "read_file.h"
#include "oci_runtime_hooks.h"
#include <stdlib.h>
#define PARSE_ERR_BUFFER_SIZE 1024
......@@ -30,7 +30,6 @@ char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, co
const unsigned char *gen_buf = NULL;
char *json_buf = NULL;
size_t gen_len = 0;
errno_t eret;
if (ptr == NULL || err == NULL) {
return NULL;
......@@ -67,11 +66,7 @@ char *oci_runtime_spec_hooks_generate_json(const oci_runtime_spec_hooks *ptr, co
*err = strdup("Out of memory");
goto free_out;
}
eret = memcpy_s((void *)json_buf, gen_len + 1, (void *)gen_buf, gen_len);
if (eret != EOK) {
*err = strdup("Memcpy failed");
goto free_out;
}
(void)memcpy((void *)json_buf, (void *)gen_buf, gen_len);
json_buf[gen_len] = '\0';
free_out:
......
......@@ -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:
......
......@@ -30,13 +30,13 @@ CODE = '''// Auto generated file. Do not edit!
# ifndef _JSON_COMMON_H
# define _JSON_COMMON_H
# include <stdlib.h>
# include <stdbool.h>
# include <stdio.h>
# include <string.h>
# include <stdint.h>
# include <yajl/yajl_tree.h>
# include <yajl/yajl_gen.h>
# include "securec.h"
# ifdef __cplusplus
extern "C" {
......
......@@ -23,7 +23,6 @@
#include <stdint.h>
#include <config.h>
#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;
}
rc = memset_s(buf, off, 0, off);
if (rc != EOK) {
goto out;
}
(void)memcpy(tmpbuf, buf, off);
(void)memset(buf, 0, off);
free(buf);
}
......
......@@ -797,7 +797,6 @@ def src_reflect(structs, schema_info, c_file, root_typ):
c_file.write("#endif\n")
c_file.write('#include <string.h>\n')
c_file.write('#include <read_file.h>\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:
......
......@@ -190,8 +190,8 @@ static bool create_container_dir(const struct lxc_container *c)
goto out;
}
nret = sprintf_s(s, length, "%s/%s", c->config_path, c->name);
if (nret < 0) {
nret = snprintf(s, length, "%s/%s", c->config_path, c->name);
if (nret < 0 || (size_t)nret >= length) {
goto out;
}
// create container dir
......@@ -224,8 +224,8 @@ static bool remove_container_dir(const struct lxc_container *c)
return false;
}
ret = sprintf_s(s, length, "%s/%s", c->config_path, c->name);
if (ret < 0) {
ret = snprintf(s, length, "%s/%s", c->config_path, c->name);
if (ret < 0 || (size_t)ret >= length) {
free(s);
return false;
}
......@@ -318,8 +318,8 @@ static bool lcr_save_ocihooks(const char *name, const char *lcrpath, const oci_r
return false;
}
nret = sprintf_s(ocihook, sizeof(ocihook), "%s/%s", bundle, OCIHOOKSFILE);
if (nret < 0) {
nret = snprintf(ocihook, sizeof(ocihook), "%s/%s", bundle, OCIHOOKSFILE);
if (nret < 0 || (size_t)nret >= sizeof(ocihook)) {
ERROR("Failed to print string");
goto out_free;
}
......@@ -374,8 +374,8 @@ static bool lcr_save_container(const char *name, const char *lcrpath, const oci_
goto out_free;
}
nret = sprintf_s(ociconfig, sizeof(ociconfig), "%s/%s", bundle, OCICONFIGFILE);
if (nret < 0) {
nret = snprintf(ociconfig, sizeof(ociconfig), "%s/%s", bundle, OCICONFIGFILE);
if (nret < 0 || (size_t)nret >= sizeof(ociconfig)) {
ERROR("Failed to print string");
goto out_free;
}
......@@ -428,8 +428,8 @@ static bool mount_get_bundle_file(char **bundle, const char *container_name, con
if (*bundle == NULL) {
return false;
}
nret = sprintf_s(*bundle, len, "%s/%s/%s", path, container_name, filename);
if (nret < 0) {
nret = snprintf(*bundle, len, "%s/%s/%s", path, container_name, filename);
if (nret < 0 || (size_t)nret >= len) {
return false;
}
return true;
......@@ -446,8 +446,8 @@ static bool mount_file(oci_runtime_spec *container, const char *bundle, const ch
int nret = 0;
defs_mount *tmp_mounts = NULL;
nret = sprintf_s(dest, sizeof(dest), "%s/%s", targetdir, filename);
if (nret < 0) {
nret = snprintf(dest, sizeof(dest), "%s/%s", targetdir, filename);
if (nret < 0 || (size_t)nret >= sizeof(dest)) {
ERROR("Failed to print string");
goto out_free;
}
......@@ -616,9 +616,9 @@ static bool mount_hosts(oci_runtime_spec *container, const struct lxc_container
goto out_free;
}
nret = sprintf_s(content, content_len, "%s%s%s\n", default_config, loop_ip, container->hostname);
if (nret < 0) {
ERROR("Sprintf_s failed");
nret = snprintf(content, content_len, "%s%s%s\n", default_config, loop_ip, container->hostname);
if (nret < 0 || (size_t)nret >= content_len) {
ERROR("Snprintf failed");
goto out_free;
}
/* 4.get file path for hosts */
......@@ -662,8 +662,8 @@ static bool copy_host_file_to_bundle(const struct lxc_container *c, const char *
bool ret = true;
int nret;
nret = sprintf_s(full_path, sizeof(full_path), "%s%s%s", rootfs, "/etc/", filename);
if (nret < 0) {
nret = snprintf(full_path, sizeof(full_path), "%s%s%s", rootfs, "/etc/", filename);
if (nret < 0 || (size_t)nret >= sizeof(full_path)) {
goto out_free;
}
......@@ -819,8 +819,8 @@ static int create_partial(const struct lxc_container *c)
return -1;
}
ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name);
if (ret < 0) {
ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
if (ret < 0 || (size_t)ret >= len) {
ERROR("Error writing partial pathname");
goto out_free;
}
......@@ -866,8 +866,8 @@ static void remove_partial(const struct lxc_container *c)
return;
}
ret = sprintf_s(path, len, "%s/%s/partial", c->config_path, c->name);
if (ret < 0) {
ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
if (ret < 0 || (size_t)ret >= len) {
ERROR("Error writing partial pathname");
goto out_free;
}
......@@ -1007,8 +1007,8 @@ static bool lcr_start_check_config(const char *lcrpath, const char *name)
return false;
}
nret = sprintf_s(config, sizeof(config), "%s/%s/config", lcrpath, name);
if (nret < 0) {
nret = snprintf(config, sizeof(config), "%s/%s/config", lcrpath, name);
if (nret < 0 || (size_t)nret >= sizeof(config)) {
SYSERROR("Failed to allocated memory");
return false;
}
......@@ -1059,8 +1059,8 @@ static int save_container_config_file(const char *rootpath, const char *id, cons
if (json_data == NULL || strlen(json_data) == 0) {
goto out;
}
nret = sprintf_s(filename, sizeof(filename), "%s/%s/%s", rootpath, id, fname);
if (nret < 0) {
nret = snprintf(filename, sizeof(filename), "%s/%s/%s", rootpath, id, fname);
if (nret < 0 || (size_t)nret >= sizeof(filename)) {
ERROR("Failed to print string");
ret = -1;
goto out;
......
......@@ -29,7 +29,6 @@
#include "utils.h"
#include "log.h"
#include "error.h"
#include "securec.h"
#include "oci_runtime_spec.h"
// Cgroup Item Definition
......@@ -188,7 +187,8 @@ static int update_resources_cpu_shares(struct lxc_container *c, const struct lcr
char numstr[128] = { 0 }; /* max buffer */
if (cr->cpu_shares != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_shares)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_shares));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -210,7 +210,8 @@ static int update_resources_cpu_period(struct lxc_container *c, const struct lcr
char numstr[128] = { 0 }; /* max buffer */
if (cr->cpu_period != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_period)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_period));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -232,7 +233,8 @@ static int update_resources_cpu_quota(struct lxc_container *c, const struct lcr_
char numstr[128] = { 0 }; /* max buffer */
if (cr->cpu_quota != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_quota)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->cpu_quota));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -283,7 +285,8 @@ static int update_resources_memory_limit(struct lxc_container *c, const struct l
char numstr[128] = { 0 }; /* max buffer */
if (cr->memory_limit != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_limit)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_limit));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -305,7 +308,8 @@ static int update_resources_memory_swap(struct lxc_container *c, const struct lc
char numstr[128] = { 0 }; /* max buffer */
if (cr->memory_swap != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_swap)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_swap));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -327,7 +331,8 @@ static int update_resources_memory_reservation(struct lxc_container *c, const st
char numstr[128] = { 0 }; /* max buffer */
if (cr->memory_reservation != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_reservation)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->memory_reservation));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -370,7 +375,8 @@ static int update_resources_blkio_weight(struct lxc_container *c, const struct l
char numstr[128] = { 0 }; /* max buffer */
if (cr->blkio_weight != 0) {
if (sprintf_s(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->blkio_weight)) < 0) {
int num = snprintf(numstr, sizeof(numstr), "%llu", (unsigned long long)(cr->blkio_weight));
if (num < 0 || (size_t)num >= sizeof(numstr)) {
ret = -1;
goto out;
}
......@@ -742,7 +748,6 @@ static void stat_get_blk_stats(struct lxc_container *c, const char *item, struct
size_t len = 0;
char **lines = NULL;
char **cols = NULL;
errno_t nret = 0;
len = (size_t)c->get_cgroup_item(c, item, buf, sizeof(buf));
if (len == 0 || len >= sizeof(buf)) {
......@@ -755,10 +760,7 @@ static void stat_get_blk_stats(struct lxc_container *c, const char *item, struct
return;
}
nret = memset_s(stats, sizeof(struct blkio_stats), 0, sizeof(struct blkio_stats));
if (nret != EOK) {
goto err_out;
}
(void)memset(stats, 0, sizeof(struct blkio_stats));
for (i = 0; lines[i]; i++) {
cols = lcr_string_split_and_trim(lines[i], ' ');
......@@ -849,9 +851,7 @@ void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs)
const char *state = NULL;
clear_error_message(&g_lcr_error);
if (memset_s(lcs, sizeof(struct lcr_container_state), 0x00, sizeof(struct lcr_container_state)) != EOK) {
ERROR("Can not set memory");
}
(void)memset(lcs, 0x00, sizeof(struct lcr_container_state));
lcs->name = util_strdup_s(c->name);
......@@ -929,7 +929,8 @@ static void execute_lxc_attach(const char *name, const char *path, const struct
if (request->timeout != 0) {
char timeout_str[LCR_NUMSTRLEN64] = { 0 };
add_array_elem(params, args_len, &i, "--timeout");
if (sprintf_s(timeout_str, LCR_NUMSTRLEN64, "%lld", (long long)request->timeout) < 0) {
int num = snprintf(timeout_str, LCR_NUMSTRLEN64, "%lld", (long long)request->timeout);
if (num < 0 || num >= LCR_NUMSTRLEN64) {
COMMAND_ERROR("Invaild attach timeout value :%lld", (long long)request->timeout);
free(params);
exit(EXIT_FAILURE);
......@@ -1069,7 +1070,8 @@ void execute_lxc_start(const char *name, const char *path, const struct lcr_star
if (request->start_timeout != 0) {
char start_timeout_str[LCR_NUMSTRLEN64] = { 0 };
add_array_elem(params, PARAM_NUM, &i, "--start-timeout");
if (sprintf_s(start_timeout_str, LCR_NUMSTRLEN64, "%u", request->start_timeout) < 0) {
int num = snprintf(start_timeout_str, LCR_NUMSTRLEN64, "%u", request->start_timeout);
if (num < 0 || num >= LCR_NUMSTRLEN64) {
COMMAND_ERROR("Invaild start timeout value: %u", request->start_timeout);
exit(EXIT_FAILURE);
}
......
......@@ -127,8 +127,8 @@ static int make_annotations(oci_runtime_spec *container, const struct lxc_contai
}
if (!anno->values[fpos]) {
nret = sprintf_s(default_path, PATH_MAX, "%s/%s/%s", c->config_path, c->name, "console.log");
if (nret < 0) {
nret = snprintf(default_path, PATH_MAX, "%s/%s/%s", c->config_path, c->name, "console.log");
if (nret < 0 || nret >= PATH_MAX) {
ERROR("create default path: %s failed", default_path);
goto out;
}
......@@ -373,8 +373,8 @@ static int lcr_spec_write_seccomp_line(int fd, const char *seccomp)
goto cleanup;
}
nret = sprintf_s(line, len, "%s = %s", "lxc.seccomp.profile", seccomp);
if (nret < 0) {
nret = snprintf(line, len, "%s = %s", "lxc.seccomp.profile", seccomp);
if (nret < 0 || (size_t)nret >= len) {
ERROR("Sprintf failed");
goto cleanup;
}
......@@ -400,8 +400,8 @@ static char *lcr_save_seccomp_file(const char *bundle, const char *seccomp_conf)
int nret;
ssize_t written_cnt;
nret = sprintf_s(seccomp, sizeof(seccomp), "%s/seccomp", bundle);
if (nret < 0) {
nret = snprintf(seccomp, sizeof(seccomp), "%s/seccomp", bundle);
if (nret < 0 || (size_t)nret >= sizeof(seccomp)) {
goto cleanup;
}
......@@ -733,8 +733,8 @@ static int lcr_open_config_file(const char *bundle)
int fd = -1;
int nret;
nret = sprintf_s(config, sizeof(config), "%s/config", bundle);
if (nret < 0) {
nret = snprintf(config, sizeof(config), "%s/config", bundle);
if (nret < 0 || (size_t)nret >= sizeof(config)) {
goto out;
}
......@@ -776,8 +776,8 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf)
goto cleanup;
}
nret = sprintf_s(line, len, "%s = %s", item->name, item->value);
if (nret < 0) {
nret = snprintf(line, len, "%s = %s", item->name, item->value);
if (nret < 0 || (size_t)nret >= len) {
ERROR("Sprintf failed");
goto cleanup;
}
......@@ -818,8 +818,8 @@ char *lcr_get_bundle(const char *lcrpath, const char *name)
goto cleanup;
}
nret = sprintf_s(bundle, len, "%s/%s", lcrpath, name);
if (nret < 0) {
nret = snprintf(bundle, len, "%s/%s", lcrpath, name);
if (nret < 0 || (size_t)nret >= len) {
ERROR("Print bundle string failed");
goto cleanup;
}
......
......@@ -25,7 +25,6 @@
#include <unistd.h>
#include <inttypes.h>
#include "securec.h"
#include "log.h"
#include "utils.h"
......@@ -234,11 +233,11 @@ int engine_log_append(const struct engine_log_event *event, const char *format,
struct timespec timestamp;
va_start(args, format);
rc = vsprintf_s(msg, MAX_MSG_LENGTH, format, args);
rc = vsprintf(msg, format, args);
va_end(args);
if (rc < 0 || rc >= MAX_MSG_LENGTH) {
rc = sprintf_s(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!");
if (rc < 0) {
if (rc < 0) {
rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!");
if (rc < 0 || rc >= MAX_MSG_LENGTH) {
return 0;
}
}
......@@ -296,17 +295,17 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest
if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH);
}
nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "",
timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func,
event->locinfo->line, msg);
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "",
timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func,
event->locinfo->line, msg);
if (nret < 0) {
nret = sprintf_s(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file,
event->locinfo->func, event->locinfo->line, "Large log message");
if (nret < 0) {
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority],
g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file,
event->locinfo->func, event->locinfo->line, "Large log message");
if (nret < 0 || (size_t)nret >= sizeof(log_buffer)) {
return;
}
}
......@@ -400,14 +399,14 @@ int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *t
/* Calculate the real seconds */
real_seconds = (((time->tv_sec - trans_to_sec) - hours_to_sec) - (real_minutes * 60));
ret = sprintf_s(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec);
ret = snprintf(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec);
if (ret < 0 || ret >= LCR_NUMSTRLEN64) {
return -1;
}
/* Create the final timestamp */
ret = sprintf_s(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s",
real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns);
ret = snprintf(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s",
real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns);
if (ret < 0 || (size_t)ret >= bufsize) {
return -1;
}
......
......@@ -26,7 +26,6 @@
#include <fcntl.h>
#include "constants.h"
#include "utils.h"
#include "securec.h"
#include "log.h"
#define ISSLASH(C) ((C) == '/')
......@@ -55,7 +54,6 @@ static int do_clean_path(const char *respath, const char *limit_respath, const c
{
char *dest = *dst;
const char *endpos = stpos;
errno_t ret;
for (; *stpos; stpos = endpos) {
while (ISSLASH(*stpos)) {
......@@ -84,11 +82,7 @@ static int do_clean_path(const char *respath, const char *limit_respath, const c
return -1;
}
ret = memcpy_s(dest, (size_t)(endpos - stpos), stpos, (size_t)(endpos - stpos));
if (ret != EOK) {
ERROR("Failed at cleanpath memcpy");
return -1;
}
(void)memcpy(dest, stpos, (size_t)(endpos - stpos));
dest += endpos - stpos;
*dest = '\0';
}
......@@ -102,7 +96,6 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len)
char *dest = NULL;
const char *stpos = NULL;
const char *limit_respath = NULL;
errno_t ret;
if (path == NULL || path[0] == '\0' || realpath == NULL || (realpath_len < PATH_MAX)) {
return NULL;
......@@ -110,11 +103,7 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len)
respath = realpath;
ret = memset_s(respath, realpath_len, 0, realpath_len);
if (ret != EOK) {
ERROR("Failed at cleanpath memset");
goto error;
}
(void)memset(respath, 0, realpath_len);
limit_respath = respath + PATH_MAX;
if (!IS_ABSOLUTE_FILE_NAME(path)) {
......@@ -128,11 +117,11 @@ static char *cleanpath(const char *path, char *realpath, size_t realpath_len)
ERROR("Failed to get the end of respath");
goto error;
}
ret = strcat_s(respath, PATH_MAX, path);
if (ret != EOK) {
ERROR("Failed at cleanpath strcat");
if (strlen(path) >= (PATH_MAX - 1) - strlen(respath)) {
ERROR("%s path too long", path);
goto error;
}
(void)strcat(respath, path);
stpos = path;
} else {
dest = respath;
......@@ -235,21 +224,11 @@ static char *do_string_join(const char *sep, const char **parts, size_t parts_le
if (res_string == NULL) {
return NULL;
}
for (iter = 0; iter < parts_len - 1; iter++) {
if (strcat_s(res_string, result_len + 1, parts[iter]) != EOK) {
free(res_string);
return NULL;
}
if (strcat_s(res_string, result_len + 1, sep) != EOK) {
free(res_string);
return NULL;
}
}
if (strcat_s(res_string, result_len + 1, parts[parts_len - 1]) != EOK) {
free(res_string);
return NULL;
(void)strcat(res_string, parts[iter]);
(void)strcat(res_string, sep);
}
(void)strcat(res_string, parts[parts_len - 1]);
return res_string;
}
......@@ -436,12 +415,7 @@ int lcr_grow_array(void ***orig_array, size_t *orig_capacity, size_t size, size_
return -1;
}
if (*orig_array) {
if (memcpy_s(add_array, add_capacity * sizeof(void *), *orig_array, *orig_capacity * sizeof(void *)) !=
EOK) {
ERROR("Failed to memcpy memory");
free(add_array);
return -1;
}
(void)memcpy(add_array, *orig_array, *orig_capacity * sizeof(void *));
free((void *)*orig_array);
}
......@@ -477,7 +451,6 @@ void *util_common_calloc_s(size_t size)
int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
{
int nret = 0;
void *addr = NULL;
if (newptr == NULL) {
......@@ -494,11 +467,7 @@ int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
}
if (oldptr != NULL) {
nret = memcpy_s(addr, newsize, oldptr, oldsize);
if (nret != EOK) {
free(addr);
goto err_out;
}
(void)memcpy(addr, oldptr, oldsize);
free(oldptr);
}
......@@ -791,8 +760,8 @@ static void util_rmdir_one(const char *dirpath, const struct dirent *pdirent, in
return;
}
nret = sprintf_s(fname, PATH_MAX, "%s/%s", dirpath, pdirent->d_name);
if (nret < 0) {
nret = snprintf(fname, PATH_MAX, "%s/%s", dirpath, pdirent->d_name);
if (nret < 0 || nret >= PATH_MAX) {
ERROR("Pathname too long");
*failure = 1;
return;
......@@ -878,27 +847,18 @@ static ssize_t util_string_replace_one(const char *needle, const char *replace,
for (next_p = (char *)haystack, p = strstr(next_p, needle); p != NULL; next_p = p, p = strstr(next_p, needle)) {
part_len = (size_t)(p - next_p);
if ((res_string != NULL) && part_len > 0) {
if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) {
ERROR("Failed to copy memory!");
return -1;
}
(void)memcpy(&res_string[length], next_p, part_len);
}
length += (ssize_t)part_len;
if ((res_string != NULL) && replace_len > 0) {
if (memcpy_s(&res_string[length], replace_len, replace, replace_len) != EOK) {
ERROR("Failed to copy memory!");
return -1;
}
(void)memcpy(&res_string[length], replace, replace_len);
}
length += (ssize_t)replace_len;
p += nl_len;
}
part_len = strlen(next_p);
if ((res_string != NULL) && part_len > 0) {
if (memcpy_s(&res_string[length], part_len, next_p, part_len) != EOK) {
ERROR("Failed to copy memory!");
return -1;
}
(void)memcpy(&res_string[length], next_p, part_len);
}
length += (ssize_t)part_len;
return length;
......@@ -1117,14 +1077,8 @@ char *util_string_append(const char *post, const char *pre)
if (res_string == NULL) {
return NULL;
}
if (strcat_s(res_string, length, pre) != EOK) {
free(res_string);
return NULL;
}
if (strcat_s(res_string, length, post) != EOK) {
free(res_string);
return NULL;
}
(void)strcat(res_string, pre);
(void)strcat(res_string, post);
return res_string;
}
......@@ -1152,11 +1106,7 @@ char *util_string_split_prefix(size_t prefix_len, const char *file)
if (path == NULL) {
return NULL;
}
if (strncpy_s(path, len + 1, file + prefix_len, len) != EOK) {
ERROR("Failed to copy string!");
free(path);
return NULL;
}
(void)strncpy(path, file + prefix_len, len);
path[len] = '\0';
return path;
......@@ -1374,11 +1324,7 @@ int util_array_append(char ***array, const char *element)
return -1;
}
if (*array) {
if (memcpy_s(new_array, (len + 2) * sizeof(char *), *array, len * sizeof(char *)) != EOK) {
ERROR("Failed to memcpy memory");
free(new_array);
return -1;
}
(void)memcpy(new_array, *array, len * sizeof(char *));
free((void *)*array);
}
*array = new_array;
......@@ -1603,7 +1549,8 @@ static int append_new_content_to_file(FILE *fp, const char *content)
ret = -1;
goto out;
}
if (sprintf_s(tmp_str, content_len, "%s\n", content) < 0) {
int num = snprintf(tmp_str, content_len, "%s\n", content);
if (num < 0 || (size_t)num >= content_len) {
ERROR("Failed to print string");
ret = -1;
goto out;
......
project(lcr_LLT)
# setup testing
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
add_subdirectory(utils)
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Description: define mock method
* Author: wangyushui
* Create: 2019-6-10
*/
#ifndef MOCK_H
#define MOCK_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MOCK_STRUCT_INIT(...) \
{ __VA_ARGS__ }
#define DEFINE_RETURN_MOCK(fn, ret) \
bool ut_ ## fn ## _mocked = false; \
ret ut_ ## fn
#define DEFINE_RETURN_MOCK_V(fn, ret, dargs) \
bool ut_ ## fn ## _mocked = false; \
ret(* ut_ ## fn) dargs
/*
* For controlling mocked function behavior, setting
* and getting values from the stub, the _P macros are
* for mocking functions that return pointer values.
*/
#define MOCK_SET(fn, val) \
ut_ ## fn ## _mocked = true; \
ut_ ## fn = val
#define MOCK_SET_V(fn, fun) \
ut_ ## fn ## _mocked = true; \
ut_ ## fn = fun
#define MOCK_GET(fn) \
ut_ ## fn
#define MOCK_GET_V(fn, args) \
ut_ ## fn args
#define MOCK_CLEAR(fn) \
ut_ ## fn ## _mocked = false;
#define MOCK_CLEAR_P(fn) \
ut_ ## fn ## _mocked = false; \
ut_ ## fn = NULL;
/* for declaring function protoypes for wrappers */
#define DECLARE_WRAPPER(fn, ret, args) \
extern bool ut_ ## fn ## _mocked; \
extern ret ut_ ## fn; \
ret __wrap_ ## fn args; \
ret __real_ ## fn args;
#define DECLARE_WRAPPER_V(fn, ret, args) \
extern bool ut_ ## fn ## _mocked; \
extern ret(* ut_ ## fn) args; \
ret __wrap_ ## fn args; \
ret __real_ ## fn args;
/* for defining the implmentation of wrappers for syscalls */
#define DEFINE_WRAPPER(fn, ret, dargs, pargs) \
DEFINE_RETURN_MOCK(fn, ret); \
ret __wrap_ ## fn dargs \
{ \
if (!ut_ ## fn ## _mocked) { \
return __real_ ## fn pargs; \
} else { \
return MOCK_GET(fn); \
} \
}
#define DEFINE_WRAPPER_V(fn, ret, dargs, pargs) \
DEFINE_RETURN_MOCK_V(fn, ret, dargs); \
__attribute__((used)) ret __wrap_ ## fn dargs \
{ \
if (!ut_ ## fn ## _mocked) { \
return __real_ ## fn pargs; \
} else { \
return MOCK_GET_V(fn, pargs); \
} \
}
/* DEFINE_STUB is for defining the implmentation of stubs for funcs. */
#define DEFINE_STUB(fn, ret, dargs, val) \
bool ut_ ## fn ## _mocked = true; \
ret ut_ ## fn = val; \
ret fn dargs; \
ret fn dargs \
{ \
return MOCK_GET(fn); \
}
/* DEFINE_STUB_V macro is for stubs that don't have a return value */
#define DEFINE_STUB_V(fn, dargs) \
void fn dargs; \
void fn dargs \
{ \
}
#define HANDLE_RETURN_MOCK(fn) \
if (ut_ ## fn ## _mocked) { \
return ut_ ## fn; \
}
#ifdef __cplusplus
}
#endif
#endif /* MOCK_H */
#######################################################################
##- @Copyright (C) Huawei Technologies., Ltd. 2019. All rights reserved.
# - lcr licensed under the Mulan PSL v1.
# - You can use this software according to the terms and conditions of the Mulan PSL v1.
# - You may obtain a copy of Mulan PSL v1 at:
# - http://license.coscl.org.cn/MulanPSL
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v1 for more details.
##- @Description: generate cetification
##- @Author: wujing
##- @Create: 2019-04-25
#######################################################################
#! /bin/bash
#set -xe
usage()
{
echo "Usage: sh llt.sh [OPTIONS]"
echo "Use llt.sh to control llt operation"
echo ""
echo "Misc:"
echo " -h, --help Print this help, then exit"
echo
echo "Compile Options:"
echo " -m, --cmake <option> use cmake genenate Makefile, eg: -m(default), -mcoverage, --cmake, --cmake=coverage"
echo " -c, --compile Enable compile"
echo " -e, --empty Enable compile empty(make clean)"
echo
echo "TestRun Options"
echo " -r, --run-llt <option> Run all llt, eg: -r, -rscreen(default), -rxml, --run-llt, --run-llt=screen, --run-llt=xml"
echo " -s, --specify-llt FILE Only Run specify llt executable FILE, eg: -smain_llt, --specify-llt=main_llt"
echo
echo "Coverage Options"
echo " -t, --cover-report <option> Enable coverage report. eg: -t, -thtml(default), -ttxt, --cover-report, --cover-report=html, --cover-report=txt"
echo " -f, --cover-file FILE Specified FILE coverage report, eg: -fmain.c, --cover-file=main.c"
echo
}
ARGS=`getopt -o "hcer::m::t::s:f:" -l "help,cmake::,empty,cover-report::,run-llt::,specify-llt:,cover-file:" -n "run_llt.sh" -- "$@"`
if [ $? != 0 ]; then
usage
exit
fi
eval set -- "${ARGS}"
if [ x"$ARGS" = x" --" ]; then
#set default value
COMPILE_ENABLE=no
COVERAGE_ENABLE=no
EMPTY_ENABLE=no
RUN_LLT=yes
RUN_MODE=screen #value: screen or xml
COVER_REPORT_ENABLE=no
fi
while true; do
case "${1}" in
-h|--help)
usage; exit 0;;
-m|--cmake)
CMAKE_ENABLE=yes
case "$2" in
"") shift 2 ;;
coverage) COVERAGE_ENABLE=yes; shift 2 ;;
*) echo "Error param: $2";exit 1;;
esac;;
-c|--compile)
COMPILE_ENABLE=yes
shift;;
-e|--empty)
EMPTY_ENABLE=yes
shift;;
-r|--run-llt)
RUN_LLT=yes
case "$2" in
"") RUN_MODE=screen;shift 2 ;;
screen) RUN_MODE=screen;shift 2 ;;
xml) RUN_MODE=xml;shift 2 ;;
*)echo "Error param: $2";exit 1;;
esac;;
-t|--cover-report)
COVER_REPORT_ENABLE=yes
case "$2" in
"") COVER_STYLE=html;shift 2 ;;
html) COVER_STYLE=html;shift 2 ;;
txt) COVER_STYLE=txt;shift 2 ;;
*)echo "Error param: $2";exit 1;;
esac;;
-s|--specify-llt)
SPECIFY_LLT=$2
shift 2;;
-f|--cover-file)
COVER_FILE=$2
shift 2;;
--)
shift;break;;
esac
done
function llt_empty()
{
if [ x"${EMPTY_ENABLE}" = x"yes" ]; then
echo ---------------------- llt empty begin ----------------------
set -x
make clean
find -name "*.gcda" |xargs rm -f
find -name "*.gcno" |xargs rm -f
find -name "*.gcov" |xargs rm -f
find ../ -name "cmake_install.cmake" |xargs rm -f
find ../ -name "Makefile" |xargs rm -f
find ../ -name "CMakeFiles" |xargs rm -rf
find ../ -name "CMakeCache.txt"|xargs rm -f
find ../ -name "CTestTestfile.cmake"|xargs rm -f
rm -rf ../conf ../grpc ../json
rm coverage -rf
rm test_result.log -f
set +x
echo ---------------------- llt empty end ------------------------
fi
}
function llt_cmake()
{
ret=0
if [ x"${CMAKE_ENABLE}" = x"yes" ]; then
echo ---------------------- llt cmake begin ----------------------
cd ..
if [ x"${COVERAGE_ENABLE}" = x"yes" ]; then
cmake ./ -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=1 -DENABLE_LLT=ON
ret=$?
else
cmake -DENABLE_LLT=ON ./
ret=$?
fi
cd -
echo ---------------------- llt cmake end ------------------------
echo
fi
return $ret
}
function llt_compile()
{
ret=0
if [ x"${COMPILE_ENABLE}" = x"yes" ]; then
echo ---------------------- llt compile begin ----------------------
make -j
ret=$?
echo ---------------------- llt compile end ------------------------
echo
fi
return $ret
}
function llt_run_all_test()
{
if [ x"${RUN_LLT}" = x"yes" ]; then
echo ---------------------- llt run begin --------------------------
if [ x"${RUN_MODE}" = x"screen" ]; then
RUN_MODE=0
elif [ x"${RUN_MODE}" = x"xml" ]; then
RUN_MODE=1
elif [ x"${RUN_MODE}" = x"" ]; then
RUN_MODE=0
else
echo "not suport run mode <${RUN_MODE}>"
usage
exit 1
fi
if [ x"${SPECIFY_LLT}" = x"" ]; then
SPECIFY_LLT=`find -name "*_llt"` # run all test
else
SPECIFY_LLT=`find -name "${SPECIFY_LLT}"`
fi
TEST_LOG=test_result.log
>$TEST_LOG
ret=0
for TEST in $SPECIFY_LLT
do
echo $TEST
$TEST $RUN_MODE
if [ $? != 0 ];then
echo $TEST FAILED >> $TEST_LOG
ret=1
else
echo $TEST success >> $TEST_LOG
fi
done
echo ""
echo '######################test result begin######################'
cat $TEST_LOG
echo '#######################test result end#######################'
echo ""
echo ---------------------- llt run end --------------------------
echo
return $ret
fi
}
function llt_coverage()
{
if [ x"${COVER_REPORT_ENABLE}" = x"yes" ]; then
echo ------------------ llt generate coverage begin --------------
if [ x"${COVER_STYLE}" = x"txt" ]; then
GCDAS=`find -name "${COVER_FILE}.gcda"`
if [ x"$GCDAS" = x"" ]; then
echo "not find ${COVER_FILE}.gcda"
echo
exit 1
fi
for GCDA in $GCDAS
do
gcov $GCDA
done
find -name "*.h.gcov" | xargs rm -f
echo '#################################'
find -name "${COVER_FILE}.gcov"
echo '#################################'
exit
elif [ x"${COVER_STYLE}" = x"html" ]; then
if [ -d coverage ]; then
rm -rf coverage
fi
mkdir coverage
if [ x"${COVER_FILE}" = x"" ]; then
LCOV_CMD="-d ./"
else
GCDAS=`find -name "${COVER_FILE}.gcda"`
if [ $? != 0 ]; then
echo "not match ${COVER_FILE}.gcda"
exit 1
fi
for GCDA in ${GCDAS}
do
TMP_STR=" -d ${GCDA}";
LCOV_CMD="${LCOV_CMD} ${TMP_STR}";
done
fi
#lcov -c ${LCOV_CMD} -o coverage/coverage.info --exclude '*_llt.c' --include '*.c' --include '*.cpp' --include '*.cc' --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph
lcov -c ${LCOV_CMD} -b $(dirname $(pwd)) --no-external --exclude '*_llt.c' -o coverage/coverage.info --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph
if [ $? != 0 ]; then
echo "lcov generate coverage.info fail."
exit 1
fi
genhtml coverage/coverage.info -o coverage/html --branch-coverage --rc lcov_branch_coverage=1 -s --legend --ignore-errors source
if [ $? != 0 ]; then
echo "genhtml fail."
exit 1
fi
chmod 755 -R coverage
fi
echo ------------------ llt generate coverage end ----------------
fi
}
llt_cmake
if [[ $? -ne 0 ]];then
exit 1
fi
llt_empty
llt_compile
if [[ $? -ne 0 ]];then
exit 1
fi
llt_run_all_test
if [[ $? -ne 0 ]];then
exit 1
fi
llt_coverage
project(lcr_LLT)
SET(EXE utils_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../src/log.c
${CMAKE_BINARY_DIR}/json/json_common.c
utils_convert_llt.cc
utils_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../../src
${CMAKE_BINARY_DIR}/json
)
set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,util_strdup_s -Wl,--wrap,calloc -Wl,--wrap,strcat_s")
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz)
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Description: utils_convert llt
* Author: tanyifeng
* Create: 2019-07-08
*/
#include <stdlib.h>
#include <stdio.h>
#include <climits>
#include <securec.h>
#include <gtest/gtest.h>
#include "mock.h"
#include "utils.h"
TEST(utils_convert, test_util_safe_int)
{
int ret;
int converted;
ret = util_safe_int("123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 123456);
ret = util_safe_int("123456", NULL);
ASSERT_NE(ret, 0);
ret = util_safe_int("-123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, -123456);
ret = util_safe_int("0", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 0);
ret = util_safe_int("1.23", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_int("1x", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_int(std::to_string((long long)INT_MIN - 1).c_str(), &converted);
ASSERT_NE(ret, 0);
ret = util_safe_int(std::to_string((long long)INT_MAX + 1).c_str(), &converted);
ASSERT_NE(ret, 0);
ret = util_safe_int("NULL", &converted);
ASSERT_NE(ret, 0);
}
TEST(utils_convert, test_util_safe_uint)
{
int ret;
unsigned int converted;
ret = util_safe_uint("123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 123456);
ret = util_safe_uint("123456", NULL);
ASSERT_NE(ret, 0);
ret = util_safe_uint("-123456", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_uint("0", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 0);
ret = util_safe_uint("1.23", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_uint("1x", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_uint(std::to_string((long long)UINT_MAX + 1).c_str(), &converted);
ASSERT_NE(ret, 0);
ret = util_safe_uint("NULL", &converted);
ASSERT_NE(ret, 0);
}
TEST(utils_convert, test_util_safe_llong)
{
int ret;
long long converted;
ret = util_safe_llong("123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 123456);
ret = util_safe_llong("123456", NULL);
ASSERT_NE(ret, 0);
ret = util_safe_llong("-123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, -123456);
ret = util_safe_llong("0", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 0);
ret = util_safe_llong("1.23", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_llong("1x", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_llong("-9223372036854775809", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_llong("9223372036854775808", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_llong("NULL", &converted);
ASSERT_NE(ret, 0);
}
TEST(utils_convert, test_util_safe_ullong)
{
int ret;
unsigned long long converted;
ret = util_safe_ullong("123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 123456);
ret = util_safe_ullong("123456", NULL);
ASSERT_NE(ret, 0);
ret = util_safe_ullong("-123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, -123456);
ret = util_safe_ullong("0", &converted);
ASSERT_EQ(ret, 0);
ASSERT_EQ(converted, 0);
ret = util_safe_ullong("1.23", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_ullong("1x", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_ullong("18446744073709551616", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_ullong("NULL", &converted);
ASSERT_NE(ret, 0);
}
TEST(utils_convert, test_util_safe_strtod)
{
int ret;
double converted;
ret = util_safe_strtod("123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_DOUBLE_EQ(converted, 123456);
ret = util_safe_strtod("123456", NULL);
ASSERT_NE(ret, 0);
ret = util_safe_strtod("-123456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_DOUBLE_EQ(converted, -123456);
ret = util_safe_strtod("0", &converted);
ASSERT_EQ(ret, 0);
ASSERT_DOUBLE_EQ(converted, 0);
ret = util_safe_strtod("123.456", &converted);
ASSERT_EQ(ret, 0);
ASSERT_DOUBLE_EQ(converted, 123.456);
ret = util_safe_strtod("1x", &converted);
ASSERT_NE(ret, 0);
ret = util_safe_strtod("NULL", &converted);
ASSERT_NE(ret, 0);
}
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* lcr licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Description: utils_string llt
* Author: tanyifeng
* Create: 2019-07-08
*/
#include <stdlib.h>
#include <stdio.h>
#include <securec.h>
#include <gtest/gtest.h>
#include "mock.h"
#include "utils.h"
extern "C" {
DECLARE_WRAPPER(util_strdup_s, char *, (const char *str));
DEFINE_WRAPPER(util_strdup_s, char *, (const char *str), (str));
DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size));
DEFINE_WRAPPER(calloc, void *, (size_t nmemb, size_t size), (nmemb, size));
DECLARE_WRAPPER_V(strcat_s, errno_t, (char *strDest, size_t destMax, const char *strSrc));
DEFINE_WRAPPER_V(strcat_s, errno_t, (char *strDest, size_t destMax, const char *strSrc),
(strDest, destMax, strSrc));
}
static int g_strcat_s_cnt = 0;
static errno_t strcat_s_fail(char *strDest, size_t destMax, const char *strSrc)
{
return (errno_t)EINVAL;
}
static errno_t strcat_s_second_fail(char *strDest, size_t destMax, const char *strSrc)
{
g_strcat_s_cnt++;
if (g_strcat_s_cnt == 1) {
return __real_strcat_s(strDest, destMax, strSrc);
}
return (errno_t)EINVAL;
}
TEST(utils_string_llt, test_str_skip_str)
{
const char *str = "abcdefghij1234567890";
const char *substr = "abcdefgh";
const char *result = nullptr;
result = str_skip_str(str, substr);
ASSERT_STREQ(result, "ij1234567890");
result = str_skip_str(str, "habc");
ASSERT_STREQ(result, nullptr);
result = str_skip_str(str, "");
ASSERT_STREQ(result, str);
result = str_skip_str(str, nullptr);
ASSERT_STREQ(result, nullptr);
result = str_skip_str("a", "a");
ASSERT_STREQ(result, "");
result = str_skip_str("", "");
ASSERT_STREQ(result, "");
result = str_skip_str(nullptr, "");
ASSERT_STREQ(result, nullptr);
}
TEST(utils_string_llt, test_util_string_join)
{
const char *array_long[] = { "abcd", "1234", "5678", "", "&^%abc" };
size_t array_long_len = sizeof(array_long) / sizeof(array_long[0]);
const char *array_short[] = { "abcd" };
size_t array_short_len = sizeof(array_short) / sizeof(array_short[0]);
const char *array_nullptr[] = { nullptr };
size_t array_nullptr_len = sizeof(array_nullptr) / sizeof(array_nullptr[0]);
char *result = nullptr;
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, "abcd 1234 5678 &^%abc");
free(result);
result = util_string_join(" ", array_short, array_short_len);
ASSERT_STREQ(result, "abcd");
free(result);
result = util_string_join(" ", array_nullptr, array_nullptr_len);
ASSERT_EQ(result, nullptr);
result = util_string_join(" ", nullptr, 0);
ASSERT_EQ(result, nullptr);
result = util_string_join("", array_long, array_long_len);
ASSERT_STREQ(result, "abcd12345678&^%abc");
free(result);
result = util_string_join(nullptr, array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_join(" ", array_short, array_short_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
g_strcat_s_cnt = 0;
MOCK_SET_V(strcat_s, strcat_s_second_fail);
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
}
TEST(utils_string_llt, test_util_string_append)
{
char *result = nullptr;
result = util_string_append("abc", "123");
ASSERT_STREQ(result, "123abc");
free(result);
result = util_string_append("abc", "");
ASSERT_STREQ(result, "abc");
free(result);
result = util_string_append("abc", nullptr);
ASSERT_STREQ(result, "abc");
free(result);
result = util_string_append("", "123");
ASSERT_STREQ(result, "123");
free(result);
result = util_string_append("", "");
ASSERT_STREQ(result, "");
free(result);
result = util_string_append("", nullptr);
ASSERT_STREQ(result, "");
free(result);
result = util_string_append(nullptr, "123");
ASSERT_STREQ(result, "123");
free(result);
result = util_string_append(nullptr, "");
ASSERT_STREQ(result, "");
free(result);
result = util_string_append(nullptr, nullptr);
ASSERT_STREQ(result, nullptr);
MOCK_SET(calloc, nullptr);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(calloc);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
g_strcat_s_cnt = 0;
MOCK_SET_V(strcat_s, strcat_s_second_fail);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
}
文件模式从 100644 更改为 100755
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册