projects/pwm

changeset 33:fa93d2ff9c62

Prevent potential division by zero

Add safeguard ensuring that there is at least one possible character to
generate passwords from in order to exclude the possibility of a division by
zero error in rand_uniform.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue Jul 30 20:38:08 2019 +0200 (15 months ago)
parents b5ebed168e59
children 34ada71ff3e5
files pw.c
line diff
     1.1 --- a/pw.c	Tue Nov 28 17:16:24 2017 +0100
     1.2 +++ b/pw.c	Tue Jul 30 20:38:08 2019 +0200
     1.3 @@ -56,6 +56,10 @@
     1.4  	for (i = 0; i < groups_len; i++) {
     1.5  		chars_len += strlen(groups[i].chars);
     1.6  	}
     1.7 +	if (chars_len == 0) {
     1.8 +		/* there must be at least one character to choose from */
     1.9 +		return (-1);
    1.10 +	}
    1.11  
    1.12  	chars = xmalloc(chars_len + 1);
    1.13  	chars[0] = '\0';