Skip to content

Commit c8a86c7

Browse files
authored
Use correct expr kind when converting pointer offset (#236)
1 parent f0072b2 commit c8a86c7

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,7 @@ void Converter::ConvertPointerOffset(clang::Expr *base, clang::Expr *idx,
36803680
PushParen neg_paren(*this, !is_addition);
36813681
{
36823682
PushParen inner(*this);
3683+
PushExprKind push(*this, ExprKind::RValue);
36833684
Convert(idx);
36843685
}
36853686
StrCat(keyword::kAs, "isize");

tests/unit/out/unsafe/string_h.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ pub unsafe fn test_strlen_5() {
211211
as i32)
212212
!= 0)
213213
);
214+
let buf: [libc::c_char; 8] = std::mem::transmute(*b"one\0two\0");
215+
let mut first: *const libc::c_char = buf.as_ptr();
216+
let mut second: *const libc::c_char =
217+
(&buf[((libc::strlen(first)).wrapping_add(1_usize))] as *const libc::c_char);
218+
assert!(
219+
((((libc::strcmp(second, (c"two".as_ptr().cast_mut()).cast_const())) == (0)) as i32) != 0)
220+
);
214221
}
215222
pub unsafe fn test_strcmp_6() {
216223
assert!(

tests/unit/string_h.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ static void test_strlen(void) {
5050
assert(strlen("") == 0);
5151
assert(strlen("hello") == 5);
5252
assert(strlen("hello world") == 11);
53+
const char buf[] = "one\0two";
54+
const char *first = buf;
55+
const char *second = &buf[strlen(first) + 1];
56+
assert(strcmp(second, "two") == 0);
5357
}
5458

5559
static void test_strcmp(void) {

0 commit comments

Comments
 (0)