GameNetworkingSockets icon indicating copy to clipboard operation
GameNetworkingSockets copied to clipboard

build: Fix compilation errors when building with Protobuf 29.0+

Open ManManson opened this issue 7 months ago • 8 comments

Starting from Protobuf 29.0, protobuf::Message::GetTypeName() can return either std::string or absl::string_view, depending on Abseil build options.

For more details, see:

https://github.com/protocolbuffers/protobuf/commit/e13b8e999b3922d0633802c7f90e39af50a31d76

Detect the return type of the method in compile-time and construct a supplementary std::string, from which we can obtain a proper null-terminated buffer and pass it to the internal assertion function, if needed.

Fixes: ValveSoftware/GameNetworkingSockets#370

ManManson avatar Apr 06 '25 09:04 ManManson

string_view::data (std, i assume abseil is the same) does not necessarily return zero-terminated strings, which are required by %s

chriku avatar Apr 11 '25 14:04 chriku

Nice catch! Indeed, it cannot be used this way. Abseil's string_view is exactly the same in this regard. I'll need to rewrite the code to automatically detect the return type of GetTypeName() and construct a null-terminated buffer out of it, if needed.

ManManson avatar Apr 11 '25 15:04 ManManson

Force-pushed the branch to fix the problem discovered by @chriku. SteamNetworkingIdentityToProtobuf macro will attempt to perform a compile-time check to see if msg.GetTypeName() returns std::string or not (in this case, any StringViewLike type will do).

This fix uses direct if constexpr + std::is_same_v combination for comparison instead of relying on various protobuf-related macros since it's both easier to read this way and, AFAIU, the macros defined inside the protobuf aren't exposed to the callers of the library.

ManManson avatar Apr 11 '25 21:04 ManManson

This seems to be a bit much overkill for an error path that terminates the program anyway. if I'd gotten a (std::, I assume abseil just works similarly) string_view or sometimes string, I would have just have yeeted it into the std::string constructor anyway, which has the nice c_str, making the expression something like: %s", std::string(msg.GetTypeName()).c_str().

chriku avatar Apr 13 '25 09:04 chriku

@chriku Sure, you're absolutely right. This is an exceptional path, so indeed there's no need for extra typing to avoid a string copy. I'll post a fix a bit later. Thanks for the review!

ManManson avatar Apr 14 '25 09:04 ManManson

Force-pushed the branch to implement the suggestion from @chriku to simplify the code. Also added a brief explanation about code safety with respect to the latest C++ standard draft.

ManManson avatar Apr 15 '25 21:04 ManManson

c++ lifetimes are always funny, eh? but the new code seems appropriate for the situation.

PS: A small (hopefully helpful) hint: You (@ManManson) sound strangely AI like, be careful that you don't get misidentified and your PRs closed in fear of copyright uncertainties or worse

chriku avatar Apr 16 '25 05:04 chriku

Yep. That's the reason I've provided a thorough comment so that it doesn't make anyone looking at the code to raise an eyebrow.

I think I've been writing like that long before AIs became manistream. I just happen to like explicit and verbose communication style when participating in public projects. 😄

ManManson avatar Apr 16 '25 12:04 ManManson

Is there a reason why this hasn't been merged, since I would like to use the latest version of Protobuf? Additional note: This would also fix the linux CI pipeline failing.

Ggjorven avatar Aug 05 '25 15:08 Ggjorven

This fixed my issues compiling this morning on Arch. This PR should be merged.

lounges avatar Sep 02 '25 19:09 lounges

Sorry, I've been away from this project for a while and I didn't see this. I checked in a separate fix. 096c19525b8d2a9b897a6e668c9612eff750b4c7

zpostfacto avatar Oct 04 '25 05:10 zpostfacto