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

Commit 6615e11

Browse files
committed
Merge pull request #248 from google/windows_vsnprintf
Fix vsnprintf error in MSVC++
2 parents 9e434dc + 6c8c459 commit 6615e11

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

examples/clean_text.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static std::string cleantext(GumboNode* node) {
3131
node->v.element.tag != GUMBO_TAG_STYLE) {
3232
std::string contents = "";
3333
GumboVector* children = &node->v.element.children;
34-
for (int i = 0; i < children->length; ++i) {
34+
for (unsigned int i = 0; i < children->length; ++i) {
3535
const std::string text = cleantext((GumboNode*) children->data[i]);
3636
if (i != 0 && !text.empty()) {
3737
contents.append(" ");

examples/find_links.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void search_for_links(GumboNode* node) {
3535
}
3636

3737
GumboVector* children = &node->v.element.children;
38-
for (int i = 0; i < children->length; ++i) {
38+
for (unsigned int i = 0; i < children->length; ++i) {
3939
search_for_links(static_cast<GumboNode*>(children->data[i]));
4040
}
4141
}

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)