comparison pw.c @ 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, 30 Jul 2019 20:38:08 +0200
parents 8768fbd09bc5
children
comparison
equal deleted inserted replaced
32:b5ebed168e59 33:fa93d2ff9c62
54 group_matches = xmalloc(groups_len * sizeof (size_t)); 54 group_matches = xmalloc(groups_len * sizeof (size_t));
55 55
56 for (i = 0; i < groups_len; i++) { 56 for (i = 0; i < groups_len; i++) {
57 chars_len += strlen(groups[i].chars); 57 chars_len += strlen(groups[i].chars);
58 } 58 }
59 if (chars_len == 0) {
60 /* there must be at least one character to choose from */
61 return (-1);
62 }
59 63
60 chars = xmalloc(chars_len + 1); 64 chars = xmalloc(chars_len + 1);
61 chars[0] = '\0'; 65 chars[0] = '\0';
62 for (i = 0; i < groups_len; i++) { 66 for (i = 0; i < groups_len; i++) {
63 strcat(chars, groups[i].chars); 67 strcat(chars, groups[i].chars);