Skip to content

Commit

Permalink
Escape trigraphs when emitting string literals.
Browse files Browse the repository at this point in the history
The EscapeTrigraphs function is copied from Google Protobuf.

PiperOrigin-RevId: 562774473
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 5, 2023
1 parent f42ad1e commit 8bcc8cc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions upb/upbc/protoc-gen-upb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "upb/base/descriptor_constants.h"
Expand Down Expand Up @@ -194,16 +195,21 @@ std::string DoubleToCLiteral(double value) {
}
}

// Escape trigraphs by escaping question marks to \?
std::string EscapeTrigraphs(absl::string_view to_escape) {
return absl::StrReplaceAll(to_escape, {{"?", "\\?"}});
}

std::string FieldDefault(upb::FieldDefPtr field) {
switch (field.ctype()) {
case kUpb_CType_Message:
return "NULL";
case kUpb_CType_Bytes:
case kUpb_CType_String: {
upb_StringView str = field.default_value().str_val;
return absl::Substitute(
"upb_StringView_FromString(\"$0\")",
absl::CEscape(absl::string_view(str.data, str.size)));
return absl::Substitute("upb_StringView_FromString(\"$0\")",
EscapeTrigraphs(absl::CEscape(
absl::string_view(str.data, str.size))));
}
case kUpb_CType_Int32:
return absl::Substitute("(int32_t)$0", field.default_value().int32_val);
Expand Down

0 comments on commit 8bcc8cc

Please sign in to comment.