Skip to content

Commit 8972027

Browse files
authored
Merge pull request #414 from libtom/fix/411
Fix/411
2 parents 927b196 + 73426f4 commit 8972027

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

demos/constants.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ int main(int argc, char **argv)
6565
/* get and print the length of the names (and values) list */
6666
if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
6767
/* get and print the names (and values) list */
68-
names_list = malloc(names_list_len);
68+
if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
6969
if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
7070
printf("%s\n", names_list);
71+
free(names_list);
7172
}
7273
} else if (argc == 3) {
7374
if (strcmp(argv[1], "-s") == 0) {

demos/sizes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ int main(int argc, char **argv)
4242
printf(" need to allocate %u bytes \n\n", sizes_list_len);
4343

4444
/* get and print the names (and sizes) list */
45-
sizes_list = malloc(sizes_list_len);
45+
if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
4646
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
4747
printf(" supported sizes:\n\n%s\n\n", sizes_list);
48+
free(sizes_list);
4849
} else if (argc == 2) {
4950
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
5051
char* base = strdup(basename(argv[0]));
@@ -60,9 +61,10 @@ int main(int argc, char **argv)
6061
/* get and print the length of the names (and sizes) list */
6162
if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
6263
/* get and print the names (and sizes) list */
63-
sizes_list = malloc(sizes_list_len);
64+
if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
6465
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
6566
printf("%s\n", sizes_list);
67+
free(sizes_list);
6668
}
6769
} else if (argc == 3) {
6870
if (strcmp(argv[1], "-s") == 0) {

src/misc/crypt/crypt_constants.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,16 @@ int crypt_get_constant(const char* namein, int *valueout) {
258258
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
259259
int i;
260260
unsigned int total_len = 0;
261-
char number[32], *ptr;
261+
char *ptr;
262262
int number_len;
263263
int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
264264

265265
/* calculate amount of memory required for the list */
266266
for (i=0; i<count; i++) {
267-
total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
268-
/* the above +1 is for the commas */
269-
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
270-
if ((number_len < 0) ||
271-
((unsigned int)number_len >= sizeof(number)))
267+
number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
268+
if (number_len < 0)
272269
return -1;
273-
total_len += number_len + 1;
274-
/* this last +1 is for newlines (and ending NULL) */
270+
total_len += number_len;
275271
}
276272

277273
if (names_list == NULL) {
@@ -283,16 +279,11 @@ int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
283279
/* build the names list */
284280
ptr = names_list;
285281
for (i=0; i<count; i++) {
286-
strcpy(ptr, _crypt_constants[i].name);
287-
ptr += strlen(_crypt_constants[i].name);
288-
strcpy(ptr, ",");
289-
ptr += 1;
290-
291-
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
292-
strcpy(ptr, number);
282+
number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
283+
if (number_len < 0) return -1;
284+
if ((unsigned int)number_len > total_len) return -1;
285+
total_len -= number_len;
293286
ptr += number_len;
294-
strcpy(ptr, "\n");
295-
ptr += 1;
296287
}
297288
/* to remove the trailing new-line */
298289
ptr -= 1;

src/misc/crypt/crypt_sizes.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,16 @@ int crypt_get_size(const char* namein, unsigned int *sizeout) {
319319
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
320320
int i;
321321
unsigned int total_len = 0;
322-
char number[32], *ptr;
322+
char *ptr;
323323
int number_len;
324324
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
325325

326326
/* calculate amount of memory required for the list */
327327
for (i=0; i<count; i++) {
328-
total_len += (unsigned int)strlen(_crypt_sizes[i].name) + 1;
329-
/* the above +1 is for the commas */
330-
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
331-
if ((number_len < 0) ||
332-
((unsigned int)number_len >= sizeof(number)))
328+
number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
329+
if (number_len < 0)
333330
return -1;
334-
total_len += (unsigned int)strlen(number) + 1;
331+
total_len += number_len;
335332
/* this last +1 is for newlines (and ending NULL) */
336333
}
337334

@@ -344,16 +341,11 @@ int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
344341
/* build the names list */
345342
ptr = names_list;
346343
for (i=0; i<count; i++) {
347-
strcpy(ptr, _crypt_sizes[i].name);
348-
ptr += strlen(_crypt_sizes[i].name);
349-
strcpy(ptr, ",");
350-
ptr += 1;
351-
352-
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
353-
strcpy(ptr, number);
344+
number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
345+
if (number_len < 0) return -1;
346+
if ((unsigned int)number_len > total_len) return -1;
347+
total_len -= number_len;
354348
ptr += number_len;
355-
strcpy(ptr, "\n");
356-
ptr += 1;
357349
}
358350
/* to remove the trailing new-line */
359351
ptr -= 1;

0 commit comments

Comments
 (0)