Skip to content

Commit

Permalink
Merge pull request spmfilter#12 from degaart/fix-rng
Browse files Browse the repository at this point in the history
Fixed random number generation
  • Loading branch information
axelsteiner authored Jan 25, 2017
2 parents 48dae95 + f1c201a commit cf07433
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/cmime_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,8 @@ char *cmime_message_generate_boundary(void) {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789._-=";

srand(time(NULL));

for ( i = 0; i < 20; ++i ) {
str[i] = text[rand() % (sizeof text - 1)];
str[i] = text[cmime_util_rand() % (sizeof text - 1)];
}
str[20] = '\0';

Expand Down Expand Up @@ -992,10 +990,9 @@ char *cmime_message_generate_message_id(void) {
gethostname(hostname,MAXHOSTNAMELEN);

mid = (char *)malloc(20 + strlen(hostname));
srandom(getpid() ^ time((time_t *) 0));
for(i=0; i < 2; i++) {
for (i2=0; i2<8; i2++)
mid[pos++] = base36[random() % 36];
mid[pos++] = base36[cmime_util_rand() % 36];

if (i==0)
mid[pos++] = '.';
Expand Down
12 changes: 12 additions & 0 deletions src/cmime_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,15 @@ CMimeInfo_T *cmime_util_info_get_from_file(const char *filename) {

return(mi);
}

int cmime_util_rand()
{
static unsigned seed = 0;

if(seed == 0)
seed = time((time_t*)0) ^ getpid();

int result = rand_r(&seed);
return result;
}

9 changes: 9 additions & 0 deletions src/cmime_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern "C" {
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>

/*!
* @struct CMimeInfo_T cmime_util.h
Expand Down Expand Up @@ -88,6 +90,13 @@ CMimeInfo_T *cmime_util_info_get_from_string(const char *s);
*/
CMimeInfo_T *cmime_util_info_get_from_file(const char *filename);

/*!
* @fn int cmime_util_rand()
* @brief Generate a random number from 0 to RAND_MAX
* @returns a random integer from 0 to RAND_MAX
*/
int cmime_util_rand();

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit cf07433

Please sign in to comment.