Subject: | various bug fixes |
Some fixes to memory leaks and for 64-bit processors. Also applied and
alternative approach to enhance the seed of the random number generator.
--- bloom.c 2010-05-31 19:49:14.000000000 -0500
+++ new_bloom.c 2012-12-17 17:10:57.000000000 -0600
@@ -23,7 +23,7 @@
}
if (hashes < 1) {
- fprintf(stderr,"hashes was %d,size %ld\n",hashes,size);
+ fprintf(stderr,"hashes was %d,size %lld\n",hashes,size);
return -1;
} else {
bloom->stat.ideal_hashes = hashes;
@@ -62,8 +62,8 @@
/* allocate our array of bytes. where m is the size of our desired
* bit vector, we allocate m/8 + 1 bytes. */
- if ((bloom->vector = (char *)malloc(sizeof(char) *
((int)(bloom->stat.elements / 8) + 1))) == NULL) {
- perror("malloc");
+ if ((bloom->vector = (char *)calloc(1,sizeof(char) *
((int)(bloom->stat.elements / 8) + 1))) == NULL) {
+ perror("calloc");
return -1;
}
/* generate a collection of random integers, to use later
@@ -77,6 +77,7 @@
{
free(bloom->vector);
free(bloom->random_nums.num);
+ free(bloom);
}
int bloom_check(bloom *bloom,char *str)
@@ -121,7 +122,6 @@
char *newstr;
char salt[100];
BIGNUM ret = 0;
- BIGNUM hash;
if (i > 0) {
sprintf(salt,"%d",bloom->random_nums.num[i]);
@@ -179,8 +179,29 @@
int sketchy_randoms(randoms *rands,int cnt)
{
+ unsigned int seed;
+ int result;
+ FILE *urandom_rp;
int i;
- srand(CONS);
+
+ /* intialize the random number generator */
+ /* TODO: update errno definitions */
+ /* XXX: return value not actually used */
+ unrandom_rp = fopen("/dev/urandom", "r");
+ if ( urandom_rp == NULL ) {
+ fprintf(stderr, "file open error /dev/random\n");
+ errno = ERR_UNKNOWN;
+ return -1;
+ }
+ result = fread(&seed, sizeof(int), 1, urandom_fp);
+ if ( result != 1 ) {
+ fprintf(stderr, "file read error /dev/urandom\n" );
+ errno = ERR_UNKNOWN;
+ return -1;
+ }
+ fclose(urandom_fp);
+ srand(seed);
+
if ((rands->num = (int *)malloc(sizeof(int) * (cnt+1))) == NULL) {
perror("malloc");
errno = ERR_MALLOC;
@@ -262,10 +283,12 @@
return NULL;
}
if ((fp = fopen(fname,"r")) == NULL) {
+ free(ret);
perror("fopen");
return NULL;
}
if (fread(ret,sizeof(bloom),1,fp) != 1) {
+ free(ret);
perror("fread");
return NULL;
}
@@ -284,11 +307,14 @@
//printf("randsr %d\n",ret->random_nums.cnt);
if ((ret->random_nums.num = (int *)malloc(sizeof(int) *
ret->random_nums.cnt)) == NULL) {
+ free(ret);
perror("malloc");
return NULL;
}
if
((fread(ret->random_nums.num,sizeof(int)*ret->random_nums.cnt,1,fp)) != 1) {
+ free(ret->random_nums.num);
+ free(ret);
perror("fwrite");
return NULL;
}
@@ -300,11 +326,16 @@
//printf("elemsr = %d\n",elems);
if ((ret->vector = (char *)malloc(sizeof(char) * elems)) == NULL) {
+ free(ret->random_nums.num);
+ free(ret);
perror("malloc");
return NULL;
}
if (fread(ret->vector,sizeof(char)*elems,1,fp) != 1) {
+ free(ret->random_nums.num);
+ free(ret->vector);
+ free(ret);
perror("fread");
return NULL;
}