Skip to content

Commit

Permalink
Use separate va_start() - va_end() for each loop
Browse files Browse the repository at this point in the history
Without this there are segmentation faults on some distributions. The
root cause of the given issue was found by Ilya Shchepetkov.
  • Loading branch information
eunovm committed Jun 2, 2022
1 parent b88cad7 commit 53271d7
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cif.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,11 @@ static char *concat(const char *first, ...)
unsigned long length = 0;
char *res, *cur;

va_start (args, first);
va_start(args, first);
/* Calculate length of resulted string. */
for (arg = first; arg; arg = va_arg(args, const char *))
length += strlen (arg);
length += strlen(arg);
va_end(args);

/* Allocate enough memory to store resulted string. */
res = malloc(length + 1);
Expand All @@ -1005,17 +1006,18 @@ static char *concat(const char *first, ...)
}

/* Concatenate strings. */
va_start(args, first);
cur = res;
for (arg = first; arg; arg = va_arg(args, const char *)) {
unsigned long cur_length = strlen(arg);
memcpy(cur, arg, cur_length);
cur += cur_length;
unsigned long cur_length = strlen(arg);
memcpy(cur, arg, cur_length);
cur += cur_length;
}
*cur = '\0';
va_end (args);
va_end(args);

/* Indeed, this function is used in different places, not only to concatenate directories and names of files, but
* anyway its result should be pretty limited. */
/* Indeed, this function is used in different places, not only to concatenate directory and file names, but anyway
its result should be pretty limited. */
if (strlen(res) >= PATH_MAX) {
fprintf(stderr, "Too long concatenated string.\n");
exit(-1);
Expand All @@ -1029,8 +1031,7 @@ static const char *itoa(unsigned int n)
unsigned int int_digits = 1, order = 10;
char *str = NULL;

/* Obtain the number of digits that are contained in unsigned integer
number. */
/* Obtain the number of digits that are contained in unsigned integer number. */
for (;;) {
if (order > UINT_MAX / 10) {
int_digits++;
Expand Down

0 comments on commit 53271d7

Please sign in to comment.