mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-12-27 05:40:17 +05:30
asprintf.c: fix use of malloc
1. Pass `size_t` variable to `malloc` 2. Multiply the count of bytes to allocate by `sizeof(char)`
This commit is contained in:
parent
cde36888a3
commit
b10cdd7215
@ -11,19 +11,20 @@ ssize_t asprintf(char** strp, char* format, ...) {
|
|||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
ssize_t size = (ssize_t) vsnprintf(NULL, 0, format, args);
|
ssize_t result = (ssize_t) vsnprintf(NULL, 0, format, args);
|
||||||
if (size < 0) {
|
if (result < 0) {
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*strp = malloc(++size);
|
size_t size = (size_t) result + 1;
|
||||||
|
*strp = malloc(size * sizeof(char));
|
||||||
if (*strp == NULL) {
|
if (*strp == NULL) {
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t result = (ssize_t) vsnprintf(*strp, size, format, args);
|
result = (ssize_t) vsnprintf(*strp, size, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user