mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
Reading exported data from shared objects on windows is broken. It requires __declspec(dllimport). However, this is problematic, as we use the same static libraries both from a shared and static context. So we can't just blindly apply dllimport. The linker should have caught this, as data members are exported in a different way. However, due to a bug in native-utils, data member symbols were exposed directly. However, interacting with those data member was completely broken. The only way we can really solve this is to just not use static data members. We're pretty good about this in WPILib itself. However, protobuf is absolutely terrible at this. There are a ton of inline functions that access global data. For the protobuf library itself, we can solve this easily enough. However, for the generated protobuf code, this is much more problematic. The member needed to bypass the global data is private. This means using just the stock protobuf code, this problem is not solvable. But, protobuf generated code has insertion points. Those insertion points let us add our own code into the generated code via a protoc plugin. And it just so happens that an insertion point exists to add extra public methodsto the generated protobuf header. There is also an insertion point to let us add to the cpp file. The methods we need are the getters, for unpacking protobufs. For any protobuf that has a message as a member, we generate a new wpi_x() getter (the existing one is just x(), where x is the field name). We then implement this in the cpp file. A trick we can use is in the cpp file, we can safely call the x() function, as the cpp file is in the same library as the global. Thus we can call that inline method, and not actually need to directly access any internal private state of the protobuf object. TL;DR, all protobuf classes that have messages as fields now have a wpi_x() accessor that must be used instead of x() if you want the code to work on windows. After wpilibsuite/native-utils#212, the bad code will fail to link, rather then just fail at runtime.
36 lines
1.6 KiB
Diff
36 lines
1.6 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Peter Johnson <johnson.peter@gmail.com>
|
|
Date: Mon, 9 Oct 2023 19:28:08 -0700
|
|
Subject: [PATCH 11/13] Avoid use of sprintf
|
|
|
|
---
|
|
src/google/protobuf/stubs/strutil.cc | 14 +++++++++++---
|
|
1 file changed, 11 insertions(+), 3 deletions(-)
|
|
|
|
diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc
|
|
index 3462e91ff273dc071628f06b91698a0f166514fc..58e03d0c4aa7c1b0337b5f650283f00117c12baa 100644
|
|
--- a/src/google/protobuf/stubs/strutil.cc
|
|
+++ b/src/google/protobuf/stubs/strutil.cc
|
|
@@ -503,10 +503,18 @@ int CEscapeInternal(const char* src, int src_len, char* dest,
|
|
(last_hex_escape && isxdigit(*src)))) {
|
|
if (dest_len - used < 4) // need space for 4 letter escape
|
|
return -1;
|
|
- sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"),
|
|
- static_cast<uint8_t>(*src));
|
|
+ dest[used++] = '\\';
|
|
+ if (use_hex) {
|
|
+ constexpr char hexdigits[] = "0123456789abcdef";
|
|
+ dest[used++] = 'x';
|
|
+ dest[used++] = hexdigits[(static_cast<uint8_t>(*src) >> 4) & 0xf];
|
|
+ dest[used++] = hexdigits[static_cast<uint8_t>(*src) & 0xf];
|
|
+ } else {
|
|
+ dest[used++] = '0' + ((static_cast<uint8_t>(*src) >> 6) & 0x3);
|
|
+ dest[used++] = '0' + ((static_cast<uint8_t>(*src) >> 3) & 0x7);
|
|
+ dest[used++] = '0' + (static_cast<uint8_t>(*src) & 0x7);
|
|
+ }
|
|
is_hex_escape = use_hex;
|
|
- used += 4;
|
|
} else {
|
|
dest[used++] = *src; break;
|
|
}
|