diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index f5cc9a9173..652ce04495 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -1844,7 +1844,7 @@ CommandLineInterface::InterpretArgument(const std::string& name, if (!version_info_.empty()) { std::cout << version_info_ << std::endl; } - std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION) + std::cout << "libprotoc " << internal::ProtocVersionString(PROTOBUF_VERSION) << PROTOBUF_VERSION_SUFFIX << std::endl; return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler. diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index f1a815022d..3fa3d04c75 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -108,6 +108,22 @@ std::string VersionString(int version) { return buffer; } +std::string ProtocVersionString(int version) { + int minor = (version / 1000) % 1000; + int micro = version % 1000; + + // 128 bytes should always be enough, but we use snprintf() anyway to be + // safe. + char buffer[128]; + snprintf(buffer, sizeof(buffer), "%d.%d", minor, micro); + + // Guard against broken MSVC snprintf(). + buffer[sizeof(buffer)-1] = '\0'; + + return buffer; + +} + } // namespace internal // =================================================================== diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index 427df67d4f..64787684bb 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -108,6 +108,9 @@ void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion, // Converts a numeric version number to a string. std::string PROTOBUF_EXPORT VersionString(int version); +// Prints the protoc compiler version (no major version) +std::string PROTOBUF_EXPORT ProtocVersionString(int version); + } // namespace internal // Place this macro in your main() function (or somewhere before you attempt