Merge pull request #414 from libtom/fix/411

Fix/411
(cherry picked from commit 8972027b46dc5ce3151deafd8eee5ea39a7119ff)
This commit is contained in:
Steffen Jaeckel 2018-06-22 11:33:14 +02:00 committed by Steffen Jaeckel
parent 07b626d7a1
commit bf5ad76c28
4 changed files with 22 additions and 36 deletions

View File

@ -65,9 +65,10 @@ int main(int argc, char **argv)
/* get and print the length of the names (and values) list */ /* get and print the length of the names (and values) list */
if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE); if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
/* get and print the names (and values) list */ /* get and print the names (and values) list */
names_list = malloc(names_list_len); if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE); if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
printf("%s\n", names_list); printf("%s\n", names_list);
free(names_list);
} }
} else if (argc == 3) { } else if (argc == 3) {
if (strcmp(argv[1], "-s") == 0) { if (strcmp(argv[1], "-s") == 0) {

View File

@ -42,9 +42,10 @@ int main(int argc, char **argv)
printf(" need to allocate %u bytes \n\n", sizes_list_len); printf(" need to allocate %u bytes \n\n", sizes_list_len);
/* get and print the names (and sizes) list */ /* get and print the names (and sizes) list */
sizes_list = malloc(sizes_list_len); if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE); if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
printf(" supported sizes:\n\n%s\n\n", sizes_list); printf(" supported sizes:\n\n%s\n\n", sizes_list);
free(sizes_list);
} else if (argc == 2) { } else if (argc == 2) {
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
char* base = strdup(basename(argv[0])); char* base = strdup(basename(argv[0]));
@ -60,9 +61,10 @@ int main(int argc, char **argv)
/* get and print the length of the names (and sizes) list */ /* get and print the length of the names (and sizes) list */
if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE); if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
/* get and print the names (and sizes) list */ /* get and print the names (and sizes) list */
sizes_list = malloc(sizes_list_len); if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE); if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
printf("%s\n", sizes_list); printf("%s\n", sizes_list);
free(sizes_list);
} }
} else if (argc == 3) { } else if (argc == 3) {
if (strcmp(argv[1], "-s") == 0) { if (strcmp(argv[1], "-s") == 0) {

View File

@ -252,20 +252,16 @@ int crypt_get_constant(const char* namein, int *valueout) {
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) { int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
int i; int i;
unsigned int total_len = 0; unsigned int total_len = 0;
char number[32], *ptr; char *ptr;
int number_len; int number_len;
int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]); int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
/* calculate amount of memory required for the list */ /* calculate amount of memory required for the list */
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1; number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
/* the above +1 is for the commas */ if (number_len < 0)
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
if ((number_len < 0) ||
((unsigned int)number_len >= sizeof(number)))
return -1; return -1;
total_len += number_len + 1; total_len += number_len;
/* this last +1 is for newlines (and ending NULL) */
} }
if (names_list == NULL) { if (names_list == NULL) {
@ -277,16 +273,11 @@ int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
/* build the names list */ /* build the names list */
ptr = names_list; ptr = names_list;
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
strcpy(ptr, _crypt_constants[i].name); number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
ptr += strlen(_crypt_constants[i].name); if (number_len < 0) return -1;
strcpy(ptr, ","); if ((unsigned int)number_len > total_len) return -1;
ptr += 1; total_len -= number_len;
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
strcpy(ptr, number);
ptr += number_len; ptr += number_len;
strcpy(ptr, "\n");
ptr += 1;
} }
/* to remove the trailing new-line */ /* to remove the trailing new-line */
ptr -= 1; ptr -= 1;

View File

@ -307,19 +307,16 @@ int crypt_get_size(const char* namein, unsigned int *sizeout) {
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) { int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
int i; int i;
unsigned int total_len = 0; unsigned int total_len = 0;
char number[32], *ptr; char *ptr;
int number_len; int number_len;
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]); int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
/* calculate amount of memory required for the list */ /* calculate amount of memory required for the list */
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
total_len += (unsigned int)strlen(_crypt_sizes[i].name) + 1; number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
/* the above +1 is for the commas */ if (number_len < 0)
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
if ((number_len < 0) ||
((unsigned int)number_len >= sizeof(number)))
return -1; return -1;
total_len += (unsigned int)strlen(number) + 1; total_len += number_len;
/* this last +1 is for newlines (and ending NULL) */ /* this last +1 is for newlines (and ending NULL) */
} }
@ -332,16 +329,11 @@ int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
/* build the names list */ /* build the names list */
ptr = names_list; ptr = names_list;
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
strcpy(ptr, _crypt_sizes[i].name); number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
ptr += strlen(_crypt_sizes[i].name); if (number_len < 0) return -1;
strcpy(ptr, ","); if ((unsigned int)number_len > total_len) return -1;
ptr += 1; total_len -= number_len;
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
strcpy(ptr, number);
ptr += number_len; ptr += number_len;
strcpy(ptr, "\n");
ptr += 1;
} }
/* to remove the trailing new-line */ /* to remove the trailing new-line */
ptr -= 1; ptr -= 1;