Replace non-printable-ascii characters in ProtoDebugString (#14918)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14918

When ProtoBuf-Lite is in use, ProtoDebugString just calls SerializeAsString.
This produces binary output, which is not a very suitable "debug" string.
Specifically, we've observed it causing problems when calling code tries to
add the debug string to a Java exception message (which requires valid UTF-8).
Now, we replace all non-ASCII bytes with "?".

This is not a very fast implementation, but generating debug strings shouldn't
be a performance-sensitive operation in any application.

Reviewed By: dzhulgakov

Differential Revision: D13385540

fbshipit-source-id: 8868172baf20efaf53fecf7d666a6980f59b64f5
This commit is contained in:
David Reiss
2018-12-13 13:14:11 -08:00
committed by Facebook Github Bot
parent 994f72ee3e
commit cbd1c519c4

View File

@ -120,7 +120,13 @@ class IfstreamInputStream : public ::google::protobuf::io::CopyingInputStream {
} // namespace
C10_EXPORT string ProtoDebugString(const MessageLite& proto) {
return proto.SerializeAsString();
string serialized = proto.SerializeAsString();
for (char& c : serialized) {
if (c < 0x20 || c >= 0x7f) {
c = '?';
}
}
return serialized;
}
C10_EXPORT bool ParseProtoFromLargeString(