Skip to content

Commit 6ece227

Browse files
committed
Fix reusal of va_list
1 parent 443cd53 commit 6ece227

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

src/csprintf_s.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@
2929
#include <stdio.h>
3030

3131
char* csprintf_s(char* format, ...) {
32-
va_list args;
33-
va_start(args, format);
34-
35-
int buffSz = vsnprintf(NULL, 0, format, args);
36-
37-
char* buff = (char*) malloc(buffSz + 1);
38-
vsnprintf(buff, buffSz + 1, format, args);
39-
40-
va_end(args);
41-
42-
return buff;
32+
if (format != NULL) {
33+
va_list args;
34+
35+
va_start(args, format);
36+
int buffSz = vsnprintf(NULL, 0, format, args);
37+
va_end(args);
38+
39+
char* buff = (char*) malloc(buffSz + 1);
40+
if (buff != NULL) {
41+
va_start(args, format);
42+
vsnprintf(buff, buffSz + 1, format, args);
43+
va_end(args);
44+
45+
return buff;
46+
} else
47+
return NULL;
48+
} else
49+
return NULL;
4350
}
4451
#endif

0 commit comments

Comments
 (0)