diff options
-rw-r--r-- | contrib/libottery/ottery_entropy_rdrand.c | 28 | ||||
-rw-r--r-- | contrib/snowball/compiler/space.c | 6 |
2 files changed, 14 insertions, 20 deletions
diff --git a/contrib/libottery/ottery_entropy_rdrand.c b/contrib/libottery/ottery_entropy_rdrand.c index c5f12609b..36ae60b83 100644 --- a/contrib/libottery/ottery_entropy_rdrand.c +++ b/contrib/libottery/ottery_entropy_rdrand.c @@ -21,14 +21,8 @@ extern int ottery_valgrind_; /** Helper: invoke the RDRAND instruction to get 4 random bytes in the output - * value. Return 0 on success, and an error on failure. */ -static int -rdrand(uint32_t *therand) { - unsigned char status; - __asm volatile(".byte 0x0F, 0xC7, 0xF0 ; setc %1" - : "=a" (*therand), "=qm" (status)); - return (status)==1 ? 0 : OTTERY_ERR_INIT_STRONG_RNG; -} + * value. Return 1 on success, and 0 on failure. */ +#define rdrand32(x) ({ unsigned char err = 0; asm volatile(".byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1":"=a"(*x), "=qm"(err)); err; }) /** Generate bytes using the Intel RDRAND instruction. */ static int @@ -36,23 +30,23 @@ ottery_get_entropy_rdrand(const struct ottery_entropy_config *cfg, struct ottery_entropy_state *state, uint8_t *out, size_t outlen) { - int err; - uint32_t *up = (uint32_t *) out; + uint32_t up; (void) cfg; (void) state; if (! (ottery_get_cpu_capabilities_() & OTTERY_CPUCAP_RAND) || ottery_valgrind_) return OTTERY_ERR_INIT_STRONG_RNG; while (outlen >= 4) { - if ((err = rdrand(up))) - return err; - up += 1; + if (rdrand32(&up) != 1) + return OTTERY_ERR_INIT_STRONG_RNG; + memcpy (out, &up, sizeof (up)); + out += sizeof (up); outlen -= 4; } + if (outlen) { - uint32_t tmp; - if ((err = rdrand(&tmp))) - return err; - memcpy(up, &tmp, outlen); + if (rdrand32(&up) != 1) + return OTTERY_ERR_INIT_STRONG_RNG; + memcpy(out, &up, outlen); } return 0; } diff --git a/contrib/snowball/compiler/space.c b/contrib/snowball/compiler/space.c index cd5fd863d..310024e76 100644 --- a/contrib/snowball/compiler/space.c +++ b/contrib/snowball/compiler/space.c @@ -97,7 +97,7 @@ int space_count = 0; extern void * check_malloc(int n) { space_count++; - return malloc(n); + return calloc(1, n); } extern void check_free(void * p) { @@ -109,7 +109,7 @@ extern void check_free(void * p) { extern char * b_to_s(symbol * p) { int n = SIZE(p); - char * s = (char *)malloc(n + 1); + char * s = (char *)calloc(1, n + 1); { int i; for (i = 0; i < n; i++) { @@ -155,7 +155,7 @@ struct str { /* Create a new string. */ extern struct str * str_new() { - struct str * output = (struct str *) malloc(sizeof(struct str)); + struct str * output = (struct str *) calloc(1, sizeof(struct str)); output->data = create_b(0); return output; } |