replace sprintf and snprintf as necessary

use_iouring
YuQing 2025-08-07 19:55:41 +08:00
parent 63ef9aa8f4
commit ec2db7cd33
14 changed files with 198 additions and 119 deletions

View File

@ -1,9 +1,10 @@
Version 1.78 2025-08-06
Version 1.78 2025-08-07
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
* add files: spinlock.[hc]
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
* performance opt.: replace sprintf and snprintf as necessary
Version 1.77 2025-03-18
* impl. shorten_path for /./ and /../

View File

@ -47,7 +47,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
start = end;
}
snprintf(name, sizeof(name), "%s-array", name_prefix);
fc_combine_two_string(name_prefix, "array", '-', name);
return fast_allocator_init_ex(&ctx->allocator, name,
obj_size, NULL, regions, region - regions, 0,
0.9999, reclaim_interval, need_lock);

View File

@ -48,7 +48,7 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
return ENOMEM;
}
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
fc_safe_strcpy(writer->filename, filename);
writer->fd = open(writer->filename, O_WRONLY |
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (writer->fd < 0)

View File

@ -540,6 +540,8 @@ static inline int fc_fallocate(int fd, const int64_t size)
#endif
#define FC_MACRO_STRINGIFY(x) #x
#define FC_MACRO_TOSTRING(x) FC_MACRO_STRINGIFY(x)
#ifdef __cplusplus
}

View File

@ -584,7 +584,7 @@ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
if (name != NULL)
{
snprintf(mblock->info.name, sizeof(mblock->info.name), "%s", name);
fc_safe_strcpy(mblock->info.name, name);
}
else
{

View File

@ -142,7 +142,7 @@ int free_queue_init_ex2(struct fast_task_queue *queue, const char *name,
(int64_t)max_data_size);
*/
snprintf(aname, sizeof(aname), "%s-task", name);
fc_combine_two_string(name, "task", '-', aname);
return fast_mblock_init_ex1(&queue->allocator, aname,
queue->block_size, alloc_once, max_connections,
(fast_mblock_object_init_func)task_alloc_init,

View File

@ -198,7 +198,7 @@ static int iniAnnotationFuncLocalIpGet(IniContext *context,
(int)(square_start - param), param);
index = atoi(square_start + 1);
} else {
snprintf(name_part, sizeof(name_part) - 1, "%s", param);
fc_safe_strcpy(name_part, param);
index = -2;
}
@ -322,7 +322,7 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
logWarning("file: "__FILE__", line: %d, "
"NO set directives before, set value to %s",
__LINE__, param);
snprintf(output, FAST_INI_ITEM_VALUE_SIZE, "%s", param);
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
return output;
}
@ -652,7 +652,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
if (IS_URL_RESOURCE(szFilename))
{
*pContext->config_path = '\0';
snprintf(full_filename, sizeof(full_filename), "%s", szFilename);
fc_safe_strcpy(full_filename, szFilename);
}
else
{
@ -675,8 +675,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
memcpy(pContext->config_path, szFilename, len);
*(pContext->config_path + len) = '\0';
snprintf(full_filename, sizeof(full_filename), \
"%s", szFilename);
fc_safe_strcpy(full_filename, szFilename);
}
else
{
@ -699,9 +698,8 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
*(pContext->config_path + len) = '\0';
}
snprintf(full_filename, sizeof(full_filename), \
"%s/%s", pContext->config_path, szFilename);
fc_combine_full_filename(pContext->config_path,
szFilename, full_filename);
pLast = strrchr(szFilename, '/');
if (pLast != NULL)
{
@ -914,7 +912,7 @@ static int iniAddAnnotation(char *params)
return EFAULT;
}
snprintf(symbol, sizeof(symbol), "%s_init_annotation", func_name);
fc_combine_two_string(func_name, "init_annotation", '_', symbol);
init_func = dlsym(dlhandle, symbol);
if (init_func == NULL)
{
@ -1013,13 +1011,11 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
strncasecmp(pLine+1, "include", 7) == 0 && \
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
{
snprintf(pIncludeFilename, sizeof(pIncludeFilename),
"%s", pLine + 9);
fc_safe_strcpy(pIncludeFilename, pLine + 9);
fc_trim(pIncludeFilename);
if (IS_URL_RESOURCE(pIncludeFilename))
{
snprintf(full_filename, sizeof(full_filename),
"%s", pIncludeFilename);
fc_safe_strcpy(full_filename, pIncludeFilename);
}
else
{
@ -1034,21 +1030,19 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
if (*pTrueFilename == '/')
{
snprintf(full_filename, sizeof(full_filename), \
"%s", pTrueFilename);
fc_safe_strcpy(full_filename, pTrueFilename);
}
else
{
snprintf(full_filename, sizeof(full_filename), \
"%s/%s", pContext->config_path, \
pTrueFilename);
fc_combine_full_filename(pContext->config_path,
pTrueFilename, full_filename);
}
if (!fileExists(full_filename))
{
logError("file: "__FILE__", line: %d, " \
"include file \"%s\" not exists, " \
"line: \"%s\"", __LINE__, \
logError("file: "__FILE__", line: %d, "
"include file \"%s\" not exists, "
"line: \"%s\"", __LINE__,
pTrueFilename, pLine);
result = ENOENT;
break;
@ -2816,7 +2810,7 @@ do { \
break; \
} \
\
snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \
fc_safe_strcpy(targetItem.name, szItemName); \
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
pSection->count, sizeof(IniItem), iniCompareByItemName); \
} while (0)

View File

@ -50,16 +50,16 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock);
static int check_and_mk_log_dir(const char *base_path)
{
char data_path[MAX_PATH_SIZE];
char log_path[MAX_PATH_SIZE];
snprintf(data_path, sizeof(data_path), "%s/logs", base_path);
if (!fileExists(data_path))
fc_combine_full_filename(base_path, "logs", log_path);
if (!fileExists(log_path))
{
if (mkdir(data_path, 0755) != 0)
if (mkdir(log_path, 0755) != 0)
{
fprintf(stderr, "mkdir \"%s\" fail, " \
"errno: %d, error info: %s\n", \
data_path, errno, STRERROR(errno));
fprintf(stderr, "mkdir \"%s\" fail, "
"errno: %d, error info: %s\n",
log_path, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
}
@ -249,7 +249,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
if (*(pContext->log_filename) == '\0')
{
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
return log_open(pContext);
}
@ -258,7 +258,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
return 0;
}
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
return log_reopen_ex(pContext);
}
@ -390,7 +390,7 @@ static int log_delete_old_file(LogContext *pContext,
}
else
{
snprintf(full_filename, sizeof(full_filename), "%s", old_filename);
fc_safe_strcpy(full_filename, old_filename);
}
if (unlink(full_filename) != 0)

View File

@ -313,8 +313,8 @@ static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
{
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
if (ip_prefix != NULL) {
ginfo->filter.ip_prefix.len = snprintf(ginfo->filter.prefix_buff,
sizeof(ginfo->filter.prefix_buff) - 1, "%s", ip_prefix);
ginfo->filter.ip_prefix.len = fc_safe_strcpy(
ginfo->filter.prefix_buff, ip_prefix);
}
}

View File

@ -146,7 +146,7 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
if (szPath[0] == '/')
{
snprintf(szAbsPath, pathSize, "%s", szPath);
fc_strlcpy(szAbsPath, szPath, pathSize);
}
else
{
@ -166,11 +166,12 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
if (szPath[0] != '\0')
{
snprintf(szAbsPath, pathSize, "%s/%s", cwd, szPath);
fc_get_full_filepath_ex(cwd, strlen(cwd), szPath,
strlen(szPath), szAbsPath, pathSize);
}
else
{
snprintf(szAbsPath, pathSize, "%s", cwd);
fc_strlcpy(szAbsPath, cwd, pathSize);
}
}
@ -206,8 +207,7 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
filename = exeFilename;
for (i=0; i<3; i++)
{
snprintf(cwd, sizeof(cwd), "%s/%s", \
search_paths[i], filename);
fc_combine_full_filename(search_paths[i], filename, cwd);
if (fileExists(cwd))
{
strcpy(szPath, search_paths[i]);
@ -227,8 +227,8 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
}
else
{
snprintf(szAbsFilename, maxSize, "%s/%s", \
szPath, filename);
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
strlen(filename), szAbsFilename, maxSize);
return szAbsFilename;
}
}
@ -242,7 +242,8 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
if (*szPath == '/')
{
snprintf(szAbsFilename, maxSize, "%s/%s", szPath, filename);
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
strlen(filename), szAbsFilename, maxSize);
}
else
{
@ -262,13 +263,13 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
if (*szPath != '\0')
{
snprintf(szAbsFilename, maxSize, "%s/%s/%s", \
snprintf(szAbsFilename, maxSize, "%s/%s/%s",
cwd, szPath, filename);
}
else
{
snprintf(szAbsFilename, maxSize, "%s/%s", \
cwd, filename);
fc_get_full_filename_ex(cwd, strlen(cwd), filename,
strlen(filename), szAbsFilename, maxSize);
}
}
@ -328,7 +329,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
continue;
}
sprintf(fullpath, "%s/%s", path, dirp->d_name);
fc_combine_full_filepath(path, dirp->d_name, fullpath);
memset(&statbuf, 0, sizeof(statbuf));
if (lstat(fullpath, &statbuf) < 0)
{
@ -337,7 +338,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
{
sprintf(filepath, "%s/cmdline", fullpath);
fc_combine_full_filename(fullpath, "cmdline", filepath);
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
{
continue;
@ -358,11 +359,11 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
ptr = strrchr(buf, '/');
if (ptr == NULL)
{
snprintf(procname, 64, "%s", buf);
fc_safe_strcpy(procname, buf);
}
else
{
snprintf(procname, 64, "%s", ptr + 1);
fc_safe_strcpy(procname, ptr + 1);
}
if (strcmp(procname, pTargetProg) == 0)
@ -1452,13 +1453,13 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
return 0;
}
int safeWriteToFile(const char *filename, const char *buff, \
int safeWriteToFile(const char *filename, const char *buff,
const int file_size)
{
char tmpFilename[PATH_MAX];
int result;
snprintf(tmpFilename, sizeof(tmpFilename), "%s.tmp", filename);
fc_combine_two_string(filename, "tmp", '.', tmpFilename);
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
{
return result;
@ -1555,13 +1556,11 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path)
fname = strrchr(src_filename, '/');
if (fname == NULL)
{
snprintf(dest_filename, sizeof(dest_filename),
"%s/%s", dest_path, src_filename);
fc_combine_full_filename(dest_path, src_filename, dest_filename);
}
else
{
snprintf(dest_filename, sizeof(dest_filename),
"%s%s", dest_path, fname);
fc_combine_full_filename(dest_path, fname, dest_filename);
}
return fc_copy_file(src_filename, dest_filename);
@ -3233,8 +3232,7 @@ static void add_thousands_separator(char *str, const int len)
const char *int2str(const int n, char *buff, const bool thousands_separator)
{
int len;
len = fc_itoa(n, buff);
*(buff + len) = '\0';
len = fc_ltostr(n, buff);
if (thousands_separator)
{
add_thousands_separator(buff, len);
@ -3245,8 +3243,7 @@ const char *int2str(const int n, char *buff, const bool thousands_separator)
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
{
int len;
len = fc_itoa(n, buff);
*(buff + len) = '\0';
len = fc_ltostr(n, buff);
if (thousands_separator)
{
add_thousands_separator(buff, len);
@ -4216,15 +4213,13 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
{
char full_filename[PATH_MAX];
snprintf(full_filename, sizeof(full_filename), "%s/%s",
file_path, redo_filename);
fc_combine_full_filename(file_path, redo_filename, full_filename);
if ((fi->filename=fc_strdup(full_filename)) == NULL)
{
return ENOMEM;
}
snprintf(full_filename, sizeof(full_filename), "%s/%s",
file_path, tmp_filename);
fc_combine_full_filename(file_path, tmp_filename, full_filename);
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
{
return ENOMEM;

View File

@ -1230,6 +1230,67 @@ static inline int resolve_path(const char *from, const char *filename,
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
}
static inline int fc_combine_two_string_ex(
const char *first_str, const int first_len,
const char *second_str, const int second_len,
const char seperator, char *combined_str, const int size)
{
char *p;
if (first_len + 1 + second_len >= size) {
return snprintf(combined_str, size, "%s%c%s",
first_str, seperator, second_str);
}
memcpy(combined_str, first_str, first_len);
p = combined_str + first_len;
*p++ = seperator;
memcpy(p, second_str, second_len);
p += second_len;
*p = '\0';
return p - combined_str;
}
#define fc_combine_two_string_s(first, second, seperator, combined, size) \
fc_combine_two_string_ex(first, strlen(first), second, strlen(second), \
seperator, combined, size)
#define fc_combine_two_string(first, second, seperator, combined) \
fc_combine_two_string_s(first, second, seperator, \
combined, sizeof(combined))
static inline int fc_get_full_filename_ex(
const char *base_path_str, const int base_path_len,
const char *filename_str, const int filename_len,
char *full_filename, const int size)
{
const char seperator = '/';
return fc_combine_two_string_ex(base_path_str, base_path_len,
filename_str, filename_len, seperator, full_filename, size);
}
#define fc_get_full_filename(base_path_str, base_path_len, \
filename_str, filename_len, full_filename) \
fc_get_full_filename_ex(base_path_str, base_path_len, \
filename_str, filename_len, full_filename, sizeof(full_filename))
#define fc_combine_full_filename(base_path, filename, full_filename) \
fc_get_full_filename(base_path, strlen(base_path), \
filename, strlen(filename), full_filename)
#define fc_get_full_filepath_ex(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename, size) \
fc_get_full_filename_ex(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename, size)
#define fc_get_full_filepath(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename) \
fc_get_full_filename(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename)
#define fc_combine_full_filepath(base_path, filepath, full_filename) \
fc_combine_full_filename(base_path, filepath, full_filename)
/** get gzip command full filename
* return: the gzip command full filename
*/
@ -1365,6 +1426,29 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
*/
int fc_itoa(int64_t n, char *buff);
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
{
int len;
int fill_len;
len = fc_itoa(n, buff);
*(buff + len) = '\0';
if (padding_len <= len) {
return len;
}
fill_len = padding_len - len;
memmove(buff + fill_len, buff, len + 1);
memset(buff, '0', fill_len);
return padding_len;
}
static inline int fc_ltostr(int64_t n, char *buff)
{
const int padding_len = 0;
return fc_ltostr_ex(n, buff, padding_len);
}
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
{
int len;
@ -1381,6 +1465,8 @@ static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
return len;
}
#define fc_safe_strcpy(dest, src) fc_strlcpy(dest, src, sizeof(dest))
/** sleep in microseconds
* parameters:
* microseconds: microseconds to sleep

View File

@ -1194,7 +1194,7 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize)
}
else
{
snprintf(buff, bufferSize, "%s", ent->h_name);
fc_strlcpy(buff, ent->h_name, bufferSize);
}
return buff;
@ -1402,14 +1402,12 @@ int nbaccept(int sock, const int timeout, int *err_no)
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
{
sockaddr_convert_t convert;
char bind_ip_prompt[256];
int result;
memset(&convert, 0, sizeof(convert));
convert.sa.addr.sa_family = af;
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
{
*bind_ip_prompt = '\0';
if (af == AF_INET)
{
convert.len = sizeof(convert.sa.addr4);
@ -1429,11 +1427,15 @@ int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
{
return result;
}
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
}
if (bind(sock, &convert.sa.addr, convert.len) < 0)
{
if (bind(sock, &convert.sa.addr, convert.len) < 0) {
char bind_ip_prompt[256];
if (bind_ipaddr == NULL || *bind_ipaddr == '\0') {
*bind_ip_prompt = '\0';
} else {
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
}
logError("file: "__FILE__", line: %d, "
"%sbind port %d failed, "
"errno: %d, error info: %s.",
@ -2577,10 +2579,14 @@ static inline int formatifmac(char *buff, const int buff_size,
return 0;
}
dest = buff + sprintf(buff, "%02x", *hwaddr);
dest = buff;
*dest++ = g_lower_hex_chars[*hwaddr >> 4];
*dest++ = g_lower_hex_chars[*hwaddr & 0x0F];
for (ptr=hwaddr+1; ptr<end; ptr++)
{
dest += sprintf(dest, ":%02x", *ptr);
*dest++ = ':';
*dest++ = g_lower_hex_chars[*ptr >> 4];
*dest++ = g_lower_hex_chars[*ptr & 0x0F];
}
return dest - buff;
}
@ -2629,7 +2635,7 @@ static int getifmac(FastIFConfig *config)
fc_trim(output);
if (*output != '\0')
{
snprintf(config->mac, sizeof(config->mac), "%s", output);
fc_safe_strcpy(config->mac, output);
}
}
}
@ -2733,7 +2739,7 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
return ENOSPC;
}
sprintf(config->name, "%s", ifc->ifa_name);
strcpy(config->name, ifc->ifa_name);
(*count)++;
}

View File

@ -194,9 +194,9 @@ int get_boot_time(struct timeval *boot_time)
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
do { \
snprintf(left.f_fstypename, sizeof(left.f_fstypename), "%s", fstypename); \
snprintf(left.f_mntfromname, sizeof(left.f_mntfromname), "%s", mntfromname); \
snprintf(left.f_mntonname, sizeof(left.f_mntonname), "%s", mntonname); \
fc_safe_strcpy(left.f_fstypename, fstypename); \
fc_safe_strcpy(left.f_mntfromname, mntfromname); \
fc_safe_strcpy(left.f_mntonname, mntonname); \
} while (0)
#ifdef OS_LINUX
@ -286,12 +286,9 @@ int get_mounted_filesystems(struct fast_statfs *stats,
toLowercase(fstypename);
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
{
snprintf(stats[*count].f_mntfromname, sizeof(stats[*count].
f_mntfromname), "%s", mntfromname);
snprintf(stats[*count].f_mntonname, sizeof(stats[*count].
f_mntonname), "%s", mntonname);
snprintf(stats[*count].f_fstypename, sizeof(stats[*count].
f_fstypename), "%s", fstypename);
fc_safe_strcpy(stats[*count].f_mntfromname, mntfromname);
fc_safe_strcpy(stats[*count].f_mntonname, mntonname);
fc_safe_strcpy(stats[*count].f_fstypename, fstypename);
(*count)++;
}
}
@ -841,8 +838,7 @@ int get_processes(struct fast_process_info **processes, int *count)
for (i=0; i<nproc; i++)
{
process->field_count = 9;
snprintf(process->comm, sizeof(process->comm),
"%s", procs[i].ki_comm);
fc_safe_strcpy(process->comm, procs[i].ki_comm);
process->pid = procs[i].ki_pid;
process->ppid = procs[i].ki_ppid;
process->starttime = procs[i].ki_start;
@ -1041,8 +1037,7 @@ static int get_block_size_by_write(const char *path, int *block_size)
return result;
}
snprintf(tmp_filename, sizeof(tmp_filename),
"%s/.blksize-test.tmp", path);
fc_combine_full_filename(path, ".blksize-test.tmp", tmp_filename);
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
O_DIRECT | O_CLOEXEC, 0755)) < 0)
{

View File

@ -210,7 +210,7 @@ int fc_thread_pool_init_ex(FCThreadPool *pool, const char *name,
return result;
}
snprintf(pool->name, sizeof(pool->name), "%s", name);
fc_safe_strcpy(pool->name, name);
pool->stack_size = stack_size;
pool->max_idle_time = max_idle_time;
if (min_idle_count > limit) {