From cbd1c519c46186eb7c70590ba02f126297ee251f Mon Sep 17 00:00:00 2001 From: David Reiss Date: Thu, 13 Dec 2018 13:14:11 -0800 Subject: [PATCH] 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 --- caffe2/utils/proto_utils.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/caffe2/utils/proto_utils.cc b/caffe2/utils/proto_utils.cc index 5e41e02..b438fb9 100644 --- a/caffe2/utils/proto_utils.cc +++ b/caffe2/utils/proto_utils.cc @@ -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( -- 2.7.4