Skip to content

Commit

Permalink
Enforce stricter version checks for Protobuf C++ by removing the noti…
Browse files Browse the repository at this point in the history
…on of "minimal version of protoc/headers"; exact version match is required.

According to https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp, since there's no guarantees for cross-version support even for the micro/patch versions, we shouldn't allow range of versions that could be backward-compatible with, either, and should detach the corresponding version verifications.

PiperOrigin-RevId: 583056663
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 16, 2023
1 parent 24dfcac commit 6eed7a2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 119 deletions.
17 changes: 5 additions & 12 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1375,21 +1375,14 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* p) {
IncludeFile("third_party/protobuf/port_def.inc", p);
p->Emit(
{
{"min_version", PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC},
{"version", PROTOBUF_VERSION},
},
R"(
#if PROTOBUF_VERSION < $min_version$
#error "This file was generated by a newer version of protoc which is"
#error "incompatible with your Protocol Buffer headers. Please update"
#error "your headers."
#endif // PROTOBUF_VERSION
#if $version$ < PROTOBUF_MIN_PROTOC_VERSION
#error "This file was generated by an older version of protoc which is"
#error "incompatible with your Protocol Buffer headers. Please"
#error "regenerate this file with a newer version of protoc."
#endif // PROTOBUF_MIN_PROTOC_VERSION
#if PROTOBUF_VERSION != $version$
#error "Protobuf C++ gencode is built with an incompatible version of"
#error "Protobuf C++ headers/runtime. See"
#error "https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp"
#endif
)");
IncludeFile("third_party/protobuf/port_undef.inc", p);
}
Expand Down
16 changes: 5 additions & 11 deletions src/google/protobuf/compiler/java/java_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,12 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),

#endif

// Defines the Protobuf C++ Version for checking version compatibility at
// compilation time.
#ifdef PROTOBUF_VERSION
#error PROTOBUF_VERSION was previously defined
#endif
#define PROTOBUF_VERSION 4025000

#ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
#error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined
#endif
#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 4025000

#ifdef PROTOBUF_MIN_PROTOC_VERSION
#error PROTOBUF_MIN_PROTOC_VERSION was previously defined
#endif
#define PROTOBUF_MIN_PROTOC_VERSION 4025000
#define PROTOBUF_VERSION 4026000

#ifdef PROTOBUF_VERSION_SUFFIX
#error PROTOBUF_VERSION_SUFFIX was previously defined
Expand Down Expand Up @@ -319,10 +311,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
#define PROTOBUF_IGNORE_DEPRECATION_STOP
#endif

// The minimum library version which works with the current version of the
// headers.
#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 4025000

#ifdef PROTOBUF_RTTI
#error PROTOBUF_RTTI was previously defined
#endif
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/port_undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
#undef PROTOBUF_MINIMUM_EDITION
#undef PROTOBUF_MAXIMUM_EDITION
#undef PROTOBUF_FIELD_OFFSET
#undef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
#undef PROTOBUF_MIN_PROTOC_VERSION
#undef PROTOBUF_PREDICT_TRUE
#undef PROTOBUF_PREDICT_FALSE
#undef PROTOBUF_EXPORT
Expand Down
35 changes: 9 additions & 26 deletions src/google/protobuf/stubs/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,21 @@ namespace protobuf {

namespace internal {

void VerifyVersion(int headerVersion,
int minLibraryVersion,
const char* filename) {
if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) {
// Library is too old for headers.
void VerifyVersion(int protobufVersionCompiledWith, const char* filename) {
// If the user's program is linked against a different version of Protobuf,
// GOOGLE_PROTOBUF_VERSION will have a different value.
if (GOOGLE_PROTOBUF_VERSION != protobufVersionCompiledWith) {
ABSL_LOG(FATAL)
<< "This program requires version " << VersionString(minLibraryVersion)
<< " of the Protocol Buffer runtime library, but the installed version "
"is "
<< "This program was compiled with Protobuf C++ version "
<< VersionString(protobufVersionCompiledWith)
<< ", but the linked version is "
<< VersionString(GOOGLE_PROTOBUF_VERSION)
<< ". Please update "
"your library. If you compiled the program yourself, make sure "
"that "
<< ". Please update your library. If you compiled the program "
"yourself, make sure that"
"your headers are from the same version of Protocol Buffers as your "
"link-time library. (Version verification failed in \""
<< filename << "\".)";
}
if (headerVersion < kMinHeaderVersionForLibrary) {
// Headers are too old for library.
ABSL_LOG(FATAL)
<< "This program was compiled against version "
<< VersionString(headerVersion)
<< " of the Protocol Buffer runtime "
"library, which is not compatible with the installed version ("
<< VersionString(GOOGLE_PROTOBUF_VERSION)
<< "). Contact the program "
"author for an update. If you compiled the program yourself, make "
"sure that your headers are from the same version of Protocol "
"Buffers "
"as your link-time library. (Version verification failed in \""
<< filename << "\".)";
}
}

std::string VersionString(int version) {
Expand Down
27 changes: 7 additions & 20 deletions src/google/protobuf/stubs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define GOOGLE_PROTOBUF_COMMON_H__

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -44,27 +45,14 @@ namespace internal {

// The current version, represented as a single integer to make comparison
// easier: major * 10^6 + minor * 10^3 + micro
#define GOOGLE_PROTOBUF_VERSION 4025000
#define GOOGLE_PROTOBUF_VERSION 4026000

// A suffix string for alpha, beta or rc releases. Empty for stable releases.
#define GOOGLE_PROTOBUF_VERSION_SUFFIX ""

// The minimum header version which works with the current version of
// the library. This constant should only be used by protoc's C++ code
// generator.
static const int kMinHeaderVersionForLibrary = 4025000;

// The minimum protoc version which works with the current version of the
// headers.
#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 4025000

// The minimum header version which works with the current version of
// protoc. This constant should only be used in VerifyVersion().
static const int kMinHeaderVersionForProtoc = 4025000;

// Verifies that the headers and libraries are compatible. Use the macro
// below to call this.
void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
// Verifies that the protobuf version a program was compiled with matches what
// it is linked/running with. Use the macro below to call this function.
void PROTOBUF_EXPORT VerifyVersion(int protobufVersionCompiledWith,
const char* filename);

// Converts a numeric version number to a string.
Expand All @@ -81,9 +69,8 @@ ProtocVersionString(int version); // NOLINT(runtime/string)
// to use the protobuf library) to verify that the version you link against
// matches the headers you compiled against. If a version mismatch is
// detected, the process will abort.
#define GOOGLE_PROTOBUF_VERIFY_VERSION \
::google::protobuf::internal::VerifyVersion( \
GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, __FILE__)
#define GOOGLE_PROTOBUF_VERIFY_VERSION \
::google::protobuf::internal::VerifyVersion(GOOGLE_PROTOBUF_VERSION, __FILE__)

// This lives in message_lite.h now, but we leave this here for any users that
// #include common.h and not message_lite.h.
Expand Down

0 comments on commit 6eed7a2

Please sign in to comment.