Skip to content

Commit 6bbfde6

Browse files
committed
Switch to DownCastToGenerated to fix linking issues
1 parent 3461038 commit 6bbfde6

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

port/protobuf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@
2424
#include "google/protobuf/util/message_differencer.h"
2525
#include "google/protobuf/wire_format.h"
2626

27+
#include "google/protobuf/port_def.inc" // MUST be last header included
28+
#if PROTOBUF_VERSION < 4025000
29+
30+
namespace google {
31+
namespace protobuf {
32+
33+
template <typename T>
34+
const T* DownCastToGenerated(const Message* message) {
35+
return static_cast<const T*>(message);
36+
}
37+
38+
template <typename T>
39+
T* DownCastToGenerated(Message* message) {
40+
const Message* message_const = message;
41+
return const_cast<T*>(DownCastToGenerated<T>(message_const));
42+
}
43+
44+
} // namespace protobuf
45+
} // namespace google
46+
#endif // PROTOBUF_VERSION
47+
#include "google/protobuf/port_undef.inc"
48+
49+
2750
namespace protobuf_mutator {
2851

2952
namespace protobuf = google::protobuf;

src/libfuzzer/libfuzzer_macro.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct PostProcessorRegistration {
119119
RegisterPostProcessor(
120120
Proto::descriptor(),
121121
[callback](protobuf::Message* message, unsigned int seed) {
122-
callback(static_cast<Proto*>(message), seed);
122+
callback(protobuf::DownCastToGenerated<Proto>(message), seed);
123123
});
124124
}
125125
};

src/mutator.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,13 @@ std::unique_ptr<Message> UnpackAny(const Any& any) {
381381

382382
const Any* CastToAny(const Message* message) {
383383
return Any::GetDescriptor() == message->GetDescriptor()
384-
? static_cast<const Any*>(message)
384+
? protobuf::DownCastToGenerated<Any>(message)
385385
: nullptr;
386386
}
387387

388388
Any* CastToAny(Message* message) {
389389
return Any::GetDescriptor() == message->GetDescriptor()
390-
? static_cast<Any*>(message)
390+
? protobuf::DownCastToGenerated<Any>(message)
391391
: nullptr;
392392
}
393393

src/mutator_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
namespace protobuf_mutator {
3131

32+
using protobuf::DownCastToGenerated;
3233
using protobuf::util::MessageDifferencer;
3334
using testing::TestWithParam;
3435
using testing::ValuesIn;
@@ -622,7 +623,7 @@ TYPED_TEST(MutatorTypedTest, RegisterPostProcessor) {
622623
TestFixture::Message::descriptor(),
623624
[=](protobuf::Message* message, unsigned int seed) {
624625
auto test_message =
625-
static_cast<typename TestFixture::Message*>(message);
626+
DownCastToGenerated<typename TestFixture::Message>(message);
626627
if (seed % 2) test_message->set_optional_string(v);
627628
});
628629
}

0 commit comments

Comments
 (0)