File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2542,6 +2542,16 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) {
25422542 return false ;
25432543 }
25442544
2545+ // char* fields in libc structs are *mut i8. We represent char* as *mut u8. Do
2546+ // the i8 -> u8 conversion here.
2547+ if (IsCharPointerFieldFromLibc (member)) {
2548+ StrCat (std::format (" ({} as {})" , str,
2549+ member->getType ()->getPointeeType ().isConstQualified ()
2550+ ? " *const u8"
2551+ : " *mut u8" ));
2552+ return false ;
2553+ }
2554+
25452555 StrCat (str);
25462556 return false ;
25472557}
Original file line number Diff line number Diff line change @@ -129,6 +129,16 @@ bool IsInMainFile(const clang::Decl *decl) {
129129 return src_mgr.isInMainFile (src_mgr.getExpansionLoc (loc));
130130}
131131
132+ bool IsCharPointerFieldFromLibc (const clang::ValueDecl *decl) {
133+ auto field = clang::dyn_cast<clang::FieldDecl>(decl);
134+ if (!field || !field->getType ()->isPointerType () ||
135+ !field->getType ()->getPointeeType ()->isCharType ()) {
136+ return false ;
137+ }
138+ return field->getASTContext ().getSourceManager ().isInSystemHeader (
139+ field->getParent ()->getLocation ());
140+ }
141+
132142bool IsUserDefinedDecl (const clang::Decl *decl) {
133143 const auto &ctx = decl->getASTContext ();
134144 const auto &src_mgr = ctx.getSourceManager ();
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ bool IsComparisonWithNullOp(const clang::BinaryOperator *expr);
3939
4040bool IsInMainFile (const clang::Decl *decl);
4141
42+ bool IsCharPointerFieldFromLibc (const clang::ValueDecl *decl);
43+
4244bool IsUserDefinedDecl (const clang::Decl *decl);
4345
4446bool RefersToUserDefinedDecl (const clang::Expr *expr);
Original file line number Diff line number Diff line change 1+ // no-compile: refcount
2+ #include <pwd.h>
3+ #include <unistd.h>
4+
5+ int main (void ) {
6+ struct passwd * pw = getpwuid (geteuid ());
7+ if (!pw ) {
8+ return 0 ;
9+ }
10+ char * home = pw -> pw_dir ;
11+ return home == 0 ;
12+ }
Original file line number Diff line number Diff line change 1+ extern crate libc;
2+ use libc:: * ;
3+ extern crate libcc2rs;
4+ use libcc2rs:: * ;
5+ use std:: collections:: BTreeMap ;
6+ use std:: io:: { Read , Seek , Write } ;
7+ use std:: os:: fd:: { AsFd , FromRawFd , IntoRawFd } ;
8+ use std:: rc:: Rc ;
9+ pub fn main ( ) {
10+ unsafe {
11+ std:: process:: exit ( main_0 ( ) as i32 ) ;
12+ }
13+ }
14+ unsafe fn main_0 ( ) -> i32 {
15+ let mut pw: * mut passwd = libc:: getpwuid ( libc:: geteuid ( ) ) ;
16+ if !!( pw) . is_null ( ) {
17+ return 0 ;
18+ }
19+ let mut home: * mut u8 = ( ( * pw) . pw_dir as * mut u8 ) ;
20+ return ( ( ( home) . is_null ( ) ) as i32 ) ;
21+ }
You can’t perform that action at this time.
0 commit comments