Skip to content

Commit bf5ad76

Browse files
committed
Merge pull request #414 from libtom/fix/411
Fix/411 (cherry picked from commit 8972027)
1 parent 07b626d commit bf5ad76

4 files changed

Lines changed: 22 additions & 36 deletions

File tree

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
@@ -252,20 +252,16 @@ int crypt_get_constant(const char* namein, int *valueout) {
252252
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
253253
int i;
254254
unsigned int total_len = 0;
255-
char number[32], *ptr;
255+
char *ptr;
256256
int number_len;
257257
int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
258258

259259
/* calculate amount of memory required for the list */
260260
for (i=0; i<count; i++) {
261-
total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
262-
/* the above +1 is for the commas */
263-
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
264-
if ((number_len < 0) ||
265-
((unsigned int)number_len >= sizeof(number)))
261+
number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
262+
if (number_len < 0)
266263
return -1;
267-
total_len += number_len + 1;
268-
/* this last +1 is for newlines (and ending NULL) */
264+
total_len += number_len;
269265
}
270266

271267
if (names_list == NULL) {
@@ -277,16 +273,11 @@ int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
277273
/* build the names list */
278274
ptr = names_list;
279275
for (i=0; i<count; i++) {
280-
strcpy(ptr, _crypt_constants[i].name);
281-
ptr += strlen(_crypt_constants[i].name);
282-
strcpy(ptr, ",");
283-
ptr += 1;
284-
285-
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
286-
strcpy(ptr, number);
276+
number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
277+
if (number_len < 0) return -1;
278+
if ((unsigned int)number_len > total_len) return -1;
279+
total_len -= number_len;
287280
ptr += number_len;
288-
strcpy(ptr, "\n");
289-
ptr += 1;
290281
}
291282
/* to remove the trailing new-line */
292283
ptr -= 1;

src/misc/crypt/crypt_sizes.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,16 @@ int crypt_get_size(const char* namein, unsigned int *sizeout) {
307307
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
308308
int i;
309309
unsigned int total_len = 0;
310-
char number[32], *ptr;
310+
char *ptr;
311311
int number_len;
312312
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
313313

314314
/* calculate amount of memory required for the list */
315315
for (i=0; i<count; i++) {
316-
total_len += (unsigned int)strlen(_crypt_sizes[i].name) + 1;
317-
/* the above +1 is for the commas */
318-
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
319-
if ((number_len < 0) ||
320-
((unsigned int)number_len >= sizeof(number)))
316+
number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
317+
if (number_len < 0)
321318
return -1;
322-
total_len += (unsigned int)strlen(number) + 1;
319+
total_len += number_len;
323320
/* this last +1 is for newlines (and ending NULL) */
324321
}
325322

@@ -332,16 +329,11 @@ int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
332329
/* build the names list */
333330
ptr = names_list;
334331
for (i=0; i<count; i++) {
335-
strcpy(ptr, _crypt_sizes[i].name);
336-
ptr += strlen(_crypt_sizes[i].name);
337-
strcpy(ptr, ",");
338-
ptr += 1;
339-
340-
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
341-
strcpy(ptr, number);
332+
number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
333+
if (number_len < 0) return -1;
334+
if ((unsigned int)number_len > total_len) return -1;
335+
total_len -= number_len;
342336
ptr += number_len;
343-
strcpy(ptr, "\n");
344-
ptr += 1;
345337
}
346338
/* to remove the trailing new-line */
347339
ptr -= 1;

0 commit comments

Comments
 (0)