Skip to content

Commit e7d120f

Browse files
committed
Pad with null bytes in GetEscapedStringLiteral
1 parent ce23495 commit e7d120f

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
@@ -1689,7 +1689,7 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
16891689
}
16901690

16911691
std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
1692-
bool add_null_char) const {
1692+
uint64_t pad_nulls) const {
16931693
auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts());
16941694
assert(str_expr);
16951695
auto raw = str_expr->getString();
@@ -1698,7 +1698,7 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
16981698
for (unsigned char c : raw) {
16991699
out += GetEscapedCharLiteral(static_cast<char>(c));
17001700
}
1701-
if (add_null_char) {
1701+
for (uint64_t i = 0; i < pad_nulls; ++i) {
17021702
out += "\\0";
17031703
}
17041704
out.push_back('"');
@@ -1707,12 +1707,17 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17071707

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

cpp2rust/converter/converter.h

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

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

239239
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
@@ -947,10 +947,15 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
947947

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

0 commit comments

Comments
 (0)