1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-03-24 17:14:16 +05:30

asprintf.c: fix the critical bug: copy the arguments

This commit is contained in:
パチュリー・ノーレッジ 2024-12-30 23:49:50 +03:00
parent 545ac7e706
commit ae71ff9c42
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -12,9 +12,13 @@ ssize_t asprintf(char** strp, char* format, ...) {
va_list args;
va_start(args, format);
va_list args_copy;
va_copy(args_copy, args);
ssize_t result = (ssize_t) vsnprintf(NULL, 0, format, args);
if (result < 0) {
va_end(args);
va_end(args_copy);
return -1;
}
@ -22,11 +26,13 @@ ssize_t asprintf(char** strp, char* format, ...) {
*strp = malloc(size * sizeof(char));
if (*strp == NULL) {
va_end(args);
va_end(args_copy);
return -1;
}
result = (ssize_t) vsnprintf(*strp, size, format, args);
result = (ssize_t) vsnprintf(*strp, size, format, args_copy);
va_end(args);
va_end(args_copy);
if (result < 0) {
free(*strp);