Compare commits

..

2 Commits

8 changed files with 57 additions and 17 deletions

View File

@ -1,4 +1,7 @@
Version 1.85 2026-06-08
* add functions fc_safe_srand and fc_safe_rand for more security under Linux
Version 1.84 2026-01-16
* fast_task_queue.h: add function free_queue_task_arg_offset
* add function double2str and double_to_comma_str

View File

@ -238,7 +238,7 @@ int fast_allocator_init_ex(struct fast_allocator_context *acontext,
struct fast_region_info *pRegion;
struct fast_region_info *region_end;
srand(time(NULL));
set_rand_seed();
memset(acontext, 0, sizeof(*acontext));
if (region_count <= 0)
{

View File

@ -22,6 +22,7 @@
#include <errno.h>
#include <assert.h>
#include "logger.h"
#include "shared_func.h"
#include "fc_memory.h"
#include "flat_skiplist.h"
@ -111,7 +112,7 @@ int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count,
sl->compare_func = compare_func;
sl->free_func = free_func;
srand(time(NULL));
set_rand_seed();
return 0;
}

View File

@ -22,6 +22,7 @@
#include <errno.h>
#include <assert.h>
#include "logger.h"
#include "shared_func.h"
#include "fc_memory.h"
#include "multi_skiplist.h"
@ -118,7 +119,7 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count,
sl->compare_func = compare_func;
sl->free_func = free_func;
srand(time(NULL));
set_rand_seed();
return 0;
}

View File

@ -2486,19 +2486,15 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes)
int set_rand_seed()
{
struct timeval tv;
time_t ts;
int64_t ns;
uint32_t seed;
if (gettimeofday(&tv, NULL) != 0)
{
logError("file: "__FILE__", line: %d, " \
"call gettimeofday fail, " \
"errno=%d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
srand(tv.tv_sec ^ tv.tv_usec);
return 0;
ns = get_current_time_ns();
time(&ts);
seed = ts ^ (ns & 0xFFFFFFFF);
srand(seed);
return 0;
}
int get_time_item_from_conf_ex(IniFullContext *ini_ctx,
@ -4555,3 +4551,32 @@ const char *double2str(const double d, const int scale,
memcpy(buff + new_len, fragment, tail_len + 1);
return buff;
}
#ifdef OS_LINUX
#ifndef __NR_getrandom
#define __NR_getrandom 318
#endif
void fc_safe_srand()
{
uint32_t seed;
if (syscall(__NR_getrandom, &seed, sizeof(seed), 0) != sizeof(seed)) {
set_rand_seed();
} else {
srand(seed);
}
}
int fc_safe_rand()
{
uint32_t n;
if (syscall(__NR_getrandom, &n, sizeof(n), 0) != sizeof(n)) {
return rand();
}
return (n & RAND_MAX);
}
#endif

View File

@ -1867,6 +1867,14 @@ static inline size_t fc_iov_get_bytes(const
}
}
#ifdef OS_LINUX
void fc_safe_srand();
int fc_safe_rand();
#else
#define fc_safe_srand() set_rand_seed()
#define fc_safe_rand() rand()
#endif
#ifdef __cplusplus
}
#endif

View File

@ -22,6 +22,7 @@
#include <errno.h>
#include <assert.h>
#include "logger.h"
#include "shared_func.h"
#include "fc_memory.h"
#include "skiplist_set.h"
@ -110,7 +111,7 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count,
sl->compare_func = compare_func;
sl->free_func = free_func;
srand(time(NULL));
set_rand_seed();
return 0;
}

View File

@ -21,6 +21,7 @@
#include <string.h>
#include <errno.h>
#include "logger.h"
#include "shared_func.h"
#include "fc_memory.h"
#include "uniq_skiplist.h"
@ -138,7 +139,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory,
factory->delay_free_seconds = delay_free_seconds;
factory->arg = arg;
srand(time(NULL));
set_rand_seed();
return 0;
}