Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.

Commit ad656bf

Browse files
committed
Fix vsnprintf error in MSVC++
1 parent 2db1796 commit ad656bf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/error.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ static int print_message(GumboParser* parser, GumboStringBuffer* output,
3939
int remaining_capacity = output->capacity - output->length;
4040
int bytes_written = vsnprintf(output->data + output->length,
4141
remaining_capacity, format, args);
42+
#ifdef _MSC_VER
43+
if (bytes_written == -1) {
44+
// vsnprintf returns -1 on MSVC++ if there's not enough capacity, instead of
45+
// returning the number of bytes that would've been written had there been
46+
// enough. In this case, we'll double the buffer size and hope it fits when
47+
// we retry (letting it fail and returning 0 if it doesn't), since there's
48+
// no way to smartly resize the buffer.
49+
gumbo_string_buffer_reserve(parser, output->capacity * 2, output);
50+
int result = vsnprintf(output->data + output->length,
51+
remaining_capacity, format, args);
52+
va_end(args);
53+
return result == -1 ? 0 : result;
54+
}
55+
#else
56+
// -1 in standard C99 indicates an encoding error. Return 0 and do nothing.
57+
if (bytes_written == -1) {
58+
va_end(args);
59+
return 0;
60+
}
61+
#endif
62+
4263
if (bytes_written > remaining_capacity) {
4364
gumbo_string_buffer_reserve(
4465
parser, output->capacity + bytes_written, output);

0 commit comments

Comments
 (0)