Skip to content

Commit 135d7c3

Browse files
committed
Pad with null bytes in GetEscapedStringLiteral
1 parent fe79931 commit 135d7c3

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,7 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
16851685
}
16861686

16871687
std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
1688-
bool add_null_char) const {
1688+
uint64_t pad_nulls) const {
16891689
auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts());
16901690
assert(str_expr);
16911691
auto raw = str_expr->getString();
@@ -1694,7 +1694,7 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
16941694
for (unsigned char c : raw) {
16951695
out += GetEscapedCharLiteral(static_cast<char>(c));
16961696
}
1697-
if (add_null_char) {
1697+
for (uint64_t i = 0; i < pad_nulls; ++i) {
16981698
out += "\\0";
16991699
}
17001700
out.push_back('"');
@@ -1703,12 +1703,17 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17031703

17041704
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
17051705
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
1706-
// b"" has type &static [u8; N]. For translating char str[] =
1707-
// "string_literal"; we need an initializer of type [u8; N]. Dereferencing
1708-
// the &static [u8; N] achieves this.
17091706
StrCat(token::kStar);
1707+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
1708+
uint64_t target = arr_ty->getSize().getZExtValue();
1709+
uint64_t pad = target > expr->getString().size()
1710+
? target - expr->getString().size()
1711+
: 0;
1712+
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, pad)));
1713+
return false;
1714+
}
17101715
}
1711-
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, true)));
1716+
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1)));
17121717
return false;
17131718
}
17141719

cpp2rust/converter/converter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
231231
std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const;
232232

233233
std::string GetEscapedStringLiteral(clang::Expr *expr,
234-
bool add_null_char = false) const;
234+
uint64_t pad_nulls = 0) const;
235235
virtual bool VisitStringLiteral(clang::StringLiteral *expr);
236236

237237
virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,15 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
945945

946946
bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
947947
if (!curr_init_type_.empty() && curr_init_type_.top()->isArrayType()) {
948-
// b"" has type &static [u8; N]. For translating char str[] =
949-
// "string_literal"; we need an initializer of type Box<[u8]>
948+
uint64_t pad = 1;
949+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.top())) {
950+
uint64_t target = arr_ty->getSize().getZExtValue();
951+
pad = target > expr->getString().size()
952+
? target - expr->getString().size()
953+
: 0;
954+
}
950955
StrCat(std::format("Box::<[u8]>::from(b{}.as_slice())",
951-
GetEscapedStringLiteral(expr, true)));
956+
GetEscapedStringLiteral(expr, pad)));
952957
return false;
953958
}
954959
StrCat(GetEscapedStringLiteral(expr));

0 commit comments

Comments
 (0)