Compare commits

..

No commits in common. "592424c114cfcb8d98ab8b3afa367c02bda453d9" and "cb014e58e7b2cc546d276c6885ea79257400724b" have entirely different histories.

8 changed files with 17 additions and 57 deletions

View File

@ -1,7 +1,4 @@
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 Version 1.84 2026-01-16
* fast_task_queue.h: add function free_queue_task_arg_offset * fast_task_queue.h: add function free_queue_task_arg_offset
* add function double2str and double_to_comma_str * 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 *pRegion;
struct fast_region_info *region_end; struct fast_region_info *region_end;
set_rand_seed(); srand(time(NULL));
memset(acontext, 0, sizeof(*acontext)); memset(acontext, 0, sizeof(*acontext));
if (region_count <= 0) if (region_count <= 0)
{ {

View File

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

View File

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

View File

@ -2486,14 +2486,18 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes)
int set_rand_seed() int set_rand_seed()
{ {
time_t ts; struct timeval tv;
int64_t ns;
uint32_t seed;
ns = get_current_time_ns(); if (gettimeofday(&tv, NULL) != 0)
time(&ts); {
seed = ts ^ (ns & 0xFFFFFFFF); logError("file: "__FILE__", line: %d, " \
srand(seed); "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; return 0;
} }
@ -4551,32 +4555,3 @@ const char *double2str(const double d, const int scale,
memcpy(buff + new_len, fragment, tail_len + 1); memcpy(buff + new_len, fragment, tail_len + 1);
return buff; 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,14 +1867,6 @@ 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 #ifdef __cplusplus
} }
#endif #endif

View File

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

View File

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