From 83671c0514741a64433dd86a958e3df6bf9508c6 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 17 Nov 2014 11:25:05 -0500 Subject: Don't rely on non-standard 'INT32_MAX' / 'INT32_MIN'. Define safe constants to use when padding enums. Fixes #80. --- src/google/protobuf/compiler/cpp/cpp_enum.cc | 4 ++-- src/google/protobuf/compiler/cpp/cpp_file.cc | 7 ++++++- src/google/protobuf/compiler/plugin.pb.h | 7 ++++++- src/google/protobuf/descriptor.pb.h | 7 ++++++- 4 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/google/protobuf') diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc index 32d5516e..769ea678 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc @@ -106,8 +106,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { // INT32_MIN and INT32_MAX if (descriptor_->value_count() > 0) printer->Print(",\n"); printer->Print(vars, - "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = INT32_MIN,\n" - "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = INT32_MAX"); + "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MIN,\n" + "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MAX"); } printer->Outdent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index dc8bf613..6b265dc6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -105,7 +105,12 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { "#define PROTOBUF_$filename_identifier$__INCLUDED\n" "\n" "#include \n" - "#include \n" // INT32_MIN, INT32_MAX + "#ifndef PROTOBUF_ENUM_MIN\n" + "#define PROTOBUF_ENUM_MIN (int32_t)0x80000000\n" + "#endif\n" + "#ifndef PROTOBUF_ENUM_MAX\n" + "#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff\n" + "#endif\n" "\n", "filename", file_->name(), "filename_identifier", filename_identifier); diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 636992a6..b752d6a1 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -5,7 +5,12 @@ #define PROTOBUF_google_2fprotobuf_2fcompiler_2fplugin_2eproto__INCLUDED #include -#include +#ifndef PROTOBUF_ENUM_MIN +#define PROTOBUF_ENUM_MIN (int32_t)0x80000000 +#endif +#ifndef PROTOBUF_ENUM_MAX +#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff +#endif #include diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index 295b53f3..767517a3 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -5,7 +5,12 @@ #define PROTOBUF_google_2fprotobuf_2fdescriptor_2eproto__INCLUDED #include -#include +#ifndef PROTOBUF_ENUM_MIN +#define PROTOBUF_ENUM_MIN (int32_t)0x80000000 +#endif +#ifndef PROTOBUF_ENUM_MAX +#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff +#endif #include -- cgit v1.2.3 From 8b2aafe7d3f59463fb8693421c09bfefb3bd0104 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 17 Nov 2014 19:35:04 -0500 Subject: Use 'kint32min'/'kint32max' constants, rather than defining locally. Incorporates feedback from @xfxyjwf: https://github.com/google/protobuf/pull/84#discussion_r20474781. --- src/google/protobuf/compiler/cpp/cpp_enum.cc | 4 ++-- src/google/protobuf/compiler/cpp/cpp_file.cc | 6 ------ src/google/protobuf/compiler/plugin.pb.h | 6 ------ src/google/protobuf/descriptor.pb.h | 6 ------ 4 files changed, 2 insertions(+), 20 deletions(-) (limited to 'src/google/protobuf') diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc index 769ea678..3ce1f120 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc @@ -106,8 +106,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { // INT32_MIN and INT32_MAX if (descriptor_->value_count() > 0) printer->Print(",\n"); printer->Print(vars, - "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MIN,\n" - "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MAX"); + "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32min,\n" + "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32max"); } printer->Outdent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index 6b265dc6..6dc88e88 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -105,12 +105,6 @@ void FileGenerator::GenerateHeader(io::Printer* printer) { "#define PROTOBUF_$filename_identifier$__INCLUDED\n" "\n" "#include \n" - "#ifndef PROTOBUF_ENUM_MIN\n" - "#define PROTOBUF_ENUM_MIN (int32_t)0x80000000\n" - "#endif\n" - "#ifndef PROTOBUF_ENUM_MAX\n" - "#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff\n" - "#endif\n" "\n", "filename", file_->name(), "filename_identifier", filename_identifier); diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index b752d6a1..ed1d3faf 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -5,12 +5,6 @@ #define PROTOBUF_google_2fprotobuf_2fcompiler_2fplugin_2eproto__INCLUDED #include -#ifndef PROTOBUF_ENUM_MIN -#define PROTOBUF_ENUM_MIN (int32_t)0x80000000 -#endif -#ifndef PROTOBUF_ENUM_MAX -#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff -#endif #include diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index 767517a3..98e0d502 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -5,12 +5,6 @@ #define PROTOBUF_google_2fprotobuf_2fdescriptor_2eproto__INCLUDED #include -#ifndef PROTOBUF_ENUM_MIN -#define PROTOBUF_ENUM_MIN (int32_t)0x80000000 -#endif -#ifndef PROTOBUF_ENUM_MAX -#define PROTOBUF_ENUM_MAX (int32_t)0x7fffffff -#endif #include -- cgit v1.2.3