mirror of https://github.com/openssl/openssl.git
Improve Malloc Failure Test
Allow 2 digits after the comma in percentage in OPENSSL_MALLOC_FAILURES. Add OPENSSL_MALLOC_SEED to allow for some randomization. Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/22127)
This commit is contained in:
parent
ac0677bd23
commit
3df5736cf3
|
@ -100,6 +100,9 @@ void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount)
|
||||||
* or 100;100@25;0
|
* or 100;100@25;0
|
||||||
* This means 100 mallocs succeed, then next 100 fail 25% of the time, and
|
* This means 100 mallocs succeed, then next 100 fail 25% of the time, and
|
||||||
* all remaining (count is zero) succeed.
|
* all remaining (count is zero) succeed.
|
||||||
|
* The failure percentge can have 2 digits after the comma. For example:
|
||||||
|
* 0@0.01
|
||||||
|
* This means 0.01% of all allocations will fail.
|
||||||
*/
|
*/
|
||||||
static void parseit(void)
|
static void parseit(void)
|
||||||
{
|
{
|
||||||
|
@ -112,7 +115,7 @@ static void parseit(void)
|
||||||
/* Get the count (atol will stop at the @ if there), and percentage */
|
/* Get the count (atol will stop at the @ if there), and percentage */
|
||||||
md_count = atol(md_failstring);
|
md_count = atol(md_failstring);
|
||||||
atsign = strchr(md_failstring, '@');
|
atsign = strchr(md_failstring, '@');
|
||||||
md_fail_percent = atsign == NULL ? 0 : atoi(atsign + 1);
|
md_fail_percent = atsign == NULL ? 0 : (int)(atof(atsign + 1) * 100 + 0.5);
|
||||||
|
|
||||||
if (semi != NULL)
|
if (semi != NULL)
|
||||||
md_failstring = semi;
|
md_failstring = semi;
|
||||||
|
@ -131,7 +134,7 @@ static void parseit(void)
|
||||||
*/
|
*/
|
||||||
static int shouldfail(void)
|
static int shouldfail(void)
|
||||||
{
|
{
|
||||||
int roll = (int)(random() % 100);
|
int roll = (int)(random() % 10000);
|
||||||
int shoulditfail = roll < md_fail_percent;
|
int shoulditfail = roll < md_fail_percent;
|
||||||
# ifndef _WIN32
|
# ifndef _WIN32
|
||||||
/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
|
/* suppressed on Windows as POSIX-like file descriptors are non-inheritable */
|
||||||
|
@ -165,6 +168,8 @@ void ossl_malloc_setup_failures(void)
|
||||||
parseit();
|
parseit();
|
||||||
if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
|
if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
|
||||||
md_tracefd = atoi(cp);
|
md_tracefd = atoi(cp);
|
||||||
|
if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
|
||||||
|
srandom(atoi(cp));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue