Skip to content

Commit 6eed7a2

Browse files
Enforce stricter version checks for Protobuf C++ by removing the notion 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
1 parent 24dfcac commit 6eed7a2

File tree

9 files changed

+44
-119
lines changed

9 files changed

+44
-119
lines changed

src/google/protobuf/compiler/cpp/file.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,21 +1375,14 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* p) {
13751375
IncludeFile("third_party/protobuf/port_def.inc", p);
13761376
p->Emit(
13771377
{
1378-
{"min_version", PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC},
13791378
{"version", PROTOBUF_VERSION},
13801379
},
13811380
R"(
1382-
#if PROTOBUF_VERSION < $min_version$
1383-
#error "This file was generated by a newer version of protoc which is"
1384-
#error "incompatible with your Protocol Buffer headers. Please update"
1385-
#error "your headers."
1386-
#endif // PROTOBUF_VERSION
1387-
1388-
#if $version$ < PROTOBUF_MIN_PROTOC_VERSION
1389-
#error "This file was generated by an older version of protoc which is"
1390-
#error "incompatible with your Protocol Buffer headers. Please"
1391-
#error "regenerate this file with a newer version of protoc."
1392-
#endif // PROTOBUF_MIN_PROTOC_VERSION
1381+
#if PROTOBUF_VERSION != $version$
1382+
#error "Protobuf C++ gencode is built with an incompatible version of"
1383+
#error "Protobuf C++ headers/runtime. See"
1384+
#error "https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp"
1385+
#endif
13931386
)");
13941387
IncludeFile("third_party/protobuf/port_undef.inc", p);
13951388
}

src/google/protobuf/compiler/java/java_features.pb.h

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/plugin.pb.h

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.h

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/descriptor.pb.h

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/port_def.inc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,12 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
192192

193193
#endif
194194

195+
// Defines the Protobuf C++ Version for checking version compatibility at
196+
// compilation time.
195197
#ifdef PROTOBUF_VERSION
196198
#error PROTOBUF_VERSION was previously defined
197199
#endif
198-
#define PROTOBUF_VERSION 4025000
199-
200-
#ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
201-
#error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined
202-
#endif
203-
#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 4025000
204-
205-
#ifdef PROTOBUF_MIN_PROTOC_VERSION
206-
#error PROTOBUF_MIN_PROTOC_VERSION was previously defined
207-
#endif
208-
#define PROTOBUF_MIN_PROTOC_VERSION 4025000
200+
#define PROTOBUF_VERSION 4026000
209201

210202
#ifdef PROTOBUF_VERSION_SUFFIX
211203
#error PROTOBUF_VERSION_SUFFIX was previously defined
@@ -319,10 +311,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
319311
#define PROTOBUF_IGNORE_DEPRECATION_STOP
320312
#endif
321313

322-
// The minimum library version which works with the current version of the
323-
// headers.
324-
#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 4025000
325-
326314
#ifdef PROTOBUF_RTTI
327315
#error PROTOBUF_RTTI was previously defined
328316
#endif

src/google/protobuf/port_undef.inc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
#undef PROTOBUF_MINIMUM_EDITION
4141
#undef PROTOBUF_MAXIMUM_EDITION
4242
#undef PROTOBUF_FIELD_OFFSET
43-
#undef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC
44-
#undef PROTOBUF_MIN_PROTOC_VERSION
4543
#undef PROTOBUF_PREDICT_TRUE
4644
#undef PROTOBUF_PREDICT_FALSE
4745
#undef PROTOBUF_EXPORT

src/google/protobuf/stubs/common.cc

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,21 @@ namespace protobuf {
4141

4242
namespace internal {
4343

44-
void VerifyVersion(int headerVersion,
45-
int minLibraryVersion,
46-
const char* filename) {
47-
if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) {
48-
// Library is too old for headers.
44+
void VerifyVersion(int protobufVersionCompiledWith, const char* filename) {
45+
// If the user's program is linked against a different version of Protobuf,
46+
// GOOGLE_PROTOBUF_VERSION will have a different value.
47+
if (GOOGLE_PROTOBUF_VERSION != protobufVersionCompiledWith) {
4948
ABSL_LOG(FATAL)
50-
<< "This program requires version " << VersionString(minLibraryVersion)
51-
<< " of the Protocol Buffer runtime library, but the installed version "
52-
"is "
49+
<< "This program was compiled with Protobuf C++ version "
50+
<< VersionString(protobufVersionCompiledWith)
51+
<< ", but the linked version is "
5352
<< VersionString(GOOGLE_PROTOBUF_VERSION)
54-
<< ". Please update "
55-
"your library. If you compiled the program yourself, make sure "
56-
"that "
53+
<< ". Please update your library. If you compiled the program "
54+
"yourself, make sure that"
5755
"your headers are from the same version of Protocol Buffers as your "
5856
"link-time library. (Version verification failed in \""
5957
<< filename << "\".)";
6058
}
61-
if (headerVersion < kMinHeaderVersionForLibrary) {
62-
// Headers are too old for library.
63-
ABSL_LOG(FATAL)
64-
<< "This program was compiled against version "
65-
<< VersionString(headerVersion)
66-
<< " of the Protocol Buffer runtime "
67-
"library, which is not compatible with the installed version ("
68-
<< VersionString(GOOGLE_PROTOBUF_VERSION)
69-
<< "). Contact the program "
70-
"author for an update. If you compiled the program yourself, make "
71-
"sure that your headers are from the same version of Protocol "
72-
"Buffers "
73-
"as your link-time library. (Version verification failed in \""
74-
<< filename << "\".)";
75-
}
7659
}
7760

7861
std::string VersionString(int version) {

src/google/protobuf/stubs/common.h

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define GOOGLE_PROTOBUF_COMMON_H__
1414

1515
#include <algorithm>
16+
#include <iostream>
1617
#include <memory>
1718
#include <string>
1819
#include <vector>
@@ -44,27 +45,14 @@ namespace internal {
4445

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

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

52-
// The minimum header version which works with the current version of
53-
// the library. This constant should only be used by protoc's C++ code
54-
// generator.
55-
static const int kMinHeaderVersionForLibrary = 4025000;
56-
57-
// The minimum protoc version which works with the current version of the
58-
// headers.
59-
#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 4025000
60-
61-
// The minimum header version which works with the current version of
62-
// protoc. This constant should only be used in VerifyVersion().
63-
static const int kMinHeaderVersionForProtoc = 4025000;
64-
65-
// Verifies that the headers and libraries are compatible. Use the macro
66-
// below to call this.
67-
void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
53+
// Verifies that the protobuf version a program was compiled with matches what
54+
// it is linked/running with. Use the macro below to call this function.
55+
void PROTOBUF_EXPORT VerifyVersion(int protobufVersionCompiledWith,
6856
const char* filename);
6957

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

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

0 commit comments

Comments
 (0)