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
pull/14710/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 24dfcacc68
commit 6eed7a2f53
  1. 17
      src/google/protobuf/compiler/cpp/file.cc
  2. 16
      src/google/protobuf/compiler/java/java_features.pb.h
  3. 16
      src/google/protobuf/compiler/plugin.pb.h
  4. 16
      src/google/protobuf/cpp_features.pb.h
  5. 16
      src/google/protobuf/descriptor.pb.h
  6. 18
      src/google/protobuf/port_def.inc
  7. 2
      src/google/protobuf/port_undef.inc
  8. 35
      src/google/protobuf/stubs/common.cc
  9. 27
      src/google/protobuf/stubs/common.h

@ -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);
}

@ -11,17 +11,11 @@
#include <utility>
#include "google/protobuf/port_def.inc"
#if PROTOBUF_VERSION < 4025000
#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 4025000 < 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 != 4026000
#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
#include "google/protobuf/port_undef.inc"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/arena.h"

@ -11,17 +11,11 @@
#include <utility>
#include "google/protobuf/port_def.inc"
#if PROTOBUF_VERSION < 4025000
#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 4025000 < 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 != 4026000
#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
#include "google/protobuf/port_undef.inc"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/arena.h"

@ -11,17 +11,11 @@
#include <utility>
#include "google/protobuf/port_def.inc"
#if PROTOBUF_VERSION < 4025000
#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 4025000 < 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 != 4026000
#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
#include "google/protobuf/port_undef.inc"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/arena.h"

@ -11,17 +11,11 @@
#include <utility>
#include "google/protobuf/port_def.inc"
#if PROTOBUF_VERSION < 4025000
#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 4025000 < 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 != 4026000
#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
#include "google/protobuf/port_undef.inc"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/arena.h"

@ -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
@ -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

@ -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

@ -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) {

@ -13,6 +13,7 @@
#define GOOGLE_PROTOBUF_COMMON_H__
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
@ -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.
@ -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.

Loading…
Cancel
Save