Fixed the mingw build by adding compatibility code for vsnprintf()

PiperOrigin-RevId: 448611947
diff --git a/BUILD b/BUILD
index ef032b9..c4d796f 100644
--- a/BUILD
+++ b/BUILD
@@ -83,6 +83,9 @@
 
 cc_library(
     name = "port",
+    hdrs = [
+        "upb/internal/vsnprintf_compat.h",
+    ],
     copts = UPB_DEFAULT_COPTS,
     textual_hdrs = [
         "upb/port_def.inc",
diff --git a/bazel/workspace_deps.bzl b/bazel/workspace_deps.bzl
index 8c1b844..655a668 100644
--- a/bazel/workspace_deps.bzl
+++ b/bazel/workspace_deps.bzl
@@ -24,7 +24,7 @@
         _github_archive,
         name = "com_google_protobuf",
         repo = "https://github.com/protocolbuffers/protobuf",
-        commit = "b180b2809f7e77fdf7dd075d26a7421085bac58f",
+        commit = "14803e6f63d4785ecd95adeeae3ac42a728b3857",
         patches = ["//bazel:protobuf.patch"],
     )
 
diff --git a/python/pb_unit_tests/well_known_types_test_wrapper.py b/python/pb_unit_tests/well_known_types_test_wrapper.py
index 6662457..ef36925 100644
--- a/python/pb_unit_tests/well_known_types_test_wrapper.py
+++ b/python/pb_unit_tests/well_known_types_test_wrapper.py
@@ -27,10 +27,5 @@
 import os
 import unittest
 
-if os.name == 'nt':
-  # TODO(b/231335093): This currently trigggers an assertion failure on Windows
-  # for unknown reasons.
-  StructTest.__unittest_skip__ = True
-
 if __name__ == '__main__':
   unittest.main(verbosity=2)
diff --git a/upb/internal/vsnprintf_compat.h b/upb/internal/vsnprintf_compat.h
new file mode 100644
index 0000000..a9d84bb
--- /dev/null
+++ b/upb/internal/vsnprintf_compat.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009-2021, Google LLC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of Google LLC nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UPB_INTERNAL_VSNPRINTF_COMPAT_H_
+#define UPB_INTERNAL_VSNPRINTF_COMPAT_H_
+
+#include <stdio.h>
+
+// Must be last.
+#include "upb/port_def.inc"
+
+UPB_INLINE int _upb_vsnprintf(char* buf, size_t size, const char* fmt,
+                              va_list ap) {
+#if defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER)
+  // The msvc runtime has a non-conforming vsnprintf() that requires the
+  // following compatibility code to become conformant.
+  int n = -1;
+  if (size != 0) n = _vsnprintf_s(buf, size, _TRUNCATE, fmt, ap);
+  if (n == -1) n = _vscprintf(fmt, ap);
+  return n;
+#else
+  return vsnprintf(buf, size, fmt, ap);
+#endif
+}
+
+#include "upb/port_undef.inc"
+
+#endif  // UPB_INTERNAL_VSNPRINTF_COMPAT_H_
diff --git a/upb/json_encode.c b/upb/json_encode.c
index 9668a42..9af7a13 100644
--- a/upb/json_encode.c
+++ b/upb/json_encode.c
@@ -37,6 +37,7 @@
 #include <string.h>
 
 #include "upb/decode.h"
+#include "upb/internal/vsnprintf_compat.h"
 #include "upb/reflection.h"
 #include "upb/upb_internal.h"
 
@@ -112,7 +113,7 @@
   va_list args;
 
   va_start(args, fmt);
-  n = vsnprintf(e->ptr, have, fmt, args);
+  n = _upb_vsnprintf(e->ptr, have, fmt, args);
   va_end(args);
 
   if (UPB_LIKELY(have > n)) {
diff --git a/upb/text_encode.c b/upb/text_encode.c
index 9612375..4f5710b 100644
--- a/upb/text_encode.c
+++ b/upb/text_encode.c
@@ -34,6 +34,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "upb/internal/vsnprintf_compat.h"
 #include "upb/reflection.h"
 #include "upb/upb_internal.h"
 
@@ -76,7 +77,7 @@
   va_list args;
 
   va_start(args, fmt);
-  n = vsnprintf(e->ptr, have, fmt, args);
+  n = _upb_vsnprintf(e->ptr, have, fmt, args);
   va_end(args);
 
   if (UPB_LIKELY(have > n)) {
diff --git a/upb/util/def_to_proto.c b/upb/util/def_to_proto.c
index b392327..ae9288c 100644
--- a/upb/util/def_to_proto.c
+++ b/upb/util/def_to_proto.c
@@ -32,6 +32,7 @@
 #include <setjmp.h>
 #include <stdio.h>
 
+#include "upb/internal/vsnprintf_compat.h"
 #include "upb/reflection.h"
 
 /* Must be last. */
@@ -89,7 +90,7 @@
   CHK_OOM(p);
   va_list args;
   va_start(args, fmt);
-  size_t n = vsnprintf(p, max, fmt, args);
+  size_t n = _upb_vsnprintf(p, max, fmt, args);
   va_end(args);
   UPB_ASSERT(n < max);
   return (upb_StringView){.data = p, .size = n};
diff --git a/upb/util/required_fields.c b/upb/util/required_fields.c
index 0e97949..90f40b6 100644
--- a/upb/util/required_fields.c
+++ b/upb/util/required_fields.c
@@ -32,6 +32,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "upb/internal/vsnprintf_compat.h"
 #include "upb/reflection.h"
 
 // Must be last.
@@ -55,7 +56,7 @@
   va_list args;
 
   va_start(args, fmt);
-  n = vsnprintf(a->ptr, have, fmt, args);
+  n = _upb_vsnprintf(a->ptr, have, fmt, args);
   va_end(args);
 
   if (UPB_LIKELY(have > n)) {