Skip to content

Commit

Permalink
c-writer.cc: sanitize module/field names for use in C comments (NFC) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Feb 25, 2023
1 parent 046451f commit adc9ecc
Show file tree
Hide file tree
Showing 2 changed files with 901 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,22 @@ constexpr char CWriter::MangleField(ModuleFieldType type) {
return 'a' + static_cast<char>(type);
}

// remove risky characters for pasting into a C-style comment
static std::string SanitizeForComment(std::string_view str) {
std::string result;

for (const uint8_t ch : str) {
// escape control chars, DEL, >7-bit chars, trigraphs, and end of comment
if (ch < ' ' || ch > '~' || ch == '?' || ch == '/') {
result += "\\" + StringPrintf("%02X", ch);
} else {
result += ch;
}
}

return result;
}

// static
std::string CWriter::MangleMultivalueTypes(const TypeVector& types) {
assert(types.size() >= 2);
Expand Down Expand Up @@ -1460,8 +1476,8 @@ void CWriter::BeginInstance() {
continue;
}

Write("/* import: '", import->module_name, "' '", import->field_name,
"' */", Newline());
Write("/* import: '", SanitizeForComment(import->module_name), "' '",
SanitizeForComment(import->field_name), "' */", Newline());

switch (import->kind()) {
case ExternalKind::Global:
Expand Down Expand Up @@ -1499,17 +1515,17 @@ void CWriter::WriteImports() {

for (const Import* import : unique_imports_) {
if (import->kind() == ExternalKind::Func) {
Write("/* import: '", import->module_name, "' '", import->field_name,
"' */", Newline());
Write("/* import: '", SanitizeForComment(import->module_name), "' '",
SanitizeForComment(import->field_name), "' */", Newline());
const Func& func = cast<FuncImport>(import)->func;
WriteImportFuncDeclaration(
func.decl, import->module_name,
ExportName(import->module_name, import->field_name));
Write(";");
Write(Newline());
} else if (import->kind() == ExternalKind::Tag) {
Write("/* import: '", import->module_name, "' '", import->field_name,
"' */", Newline());
Write("/* import: '", SanitizeForComment(import->module_name), "' '",
SanitizeForComment(import->field_name), "' */", Newline());
Write("extern const wasm_rt_tag_t ",
ExportName(import->module_name, import->field_name), ";",
Newline());
Expand Down Expand Up @@ -1924,7 +1940,8 @@ void CWriter::WriteExports(CWriterPhase kind) {
return;

for (const Export* export_ : module_->exports) {
Write(Newline(), "/* export: '", export_->name, "' */", Newline());
Write(Newline(), "/* export: '", SanitizeForComment(export_->name), "' */",
Newline());

const std::string mangled_name = ExportName(export_->name);
std::string internal_name;
Expand Down
Loading

0 comments on commit adc9ecc

Please sign in to comment.