- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想打印出 libc::getcwd
的结果.我的问题是创建 getcwd
需要 i8
( c_char
) 缓冲区,而 String::from_utf8
需要 u8
缓冲。我开始于:
static BUF_BYTES: usize = 4096;
fn main() {
unsafe {
let mut buf: Vec<i8> = Vec::with_capacity(BUF_BYTES as usize);
libc::getcwd(buf.as_mut_ptr(), buf.len());
let s = String::from_utf8(buf).expect("Found invalid UTF-8");
println!("result: {}", s);
}
}
产生错误的地方:
14:32 error: mismatched types:
expected `std::vec::Vec<u8>`,
found `std::vec::Vec<i8>` [E0308]
感谢评论,我更改了 buf
进入 Vec<u8>
并将其转换为 c_char
getcwd
中的缓冲区调用:
let mut buf: Vec<u8> = Vec::with_capacity(BUF_BYTES as usize);
libc::getcwd(buf.as_mut_ptr() as *mut c_char, buf.len());
这可以编译,但是现在,当打印字符串时它是空的(长度:0)
我发现 getcwd
返回 NULL(libc::getcwd(...).is_null()
为真),通过外部 crate 读取最后一个错误 errno
(为什么这是一个独立于 libc 的箱子?)揭示了 getcwd
失败并显示“无效参数”。问题的根源似乎是 buf.len()
返回 0。
最佳答案
在大多数情况下,您应该只使用env::current_dir
.这会为您正确处理所有特定于平台的问题,例如评论中提到的“其他”编码。
C 字符串有点糟糕。 getcwd
填充一定长度的缓冲区,但不告诉您它在哪里结束;您必须手动找到终止 NUL
字节。
extern crate libc;
static BUF_BYTES: usize = 4096;
fn main() {
let buf = unsafe {
let mut buf = Vec::with_capacity(BUF_BYTES);
let res = libc::getcwd(buf.as_mut_ptr() as *mut i8, buf.capacity());
if res.is_null() {
panic!("Not long enough");
}
let mut len = 0;
while *buf.as_mut_ptr().offset(len as isize) != 0 { len += 1 }
buf.set_len(len);
buf
};
let s = String::from_utf8(buf).expect("Found invalid UTF-8");
println!("result: {}", s);
}
seems that buf.len() returns 0
是的,长度为零,因为没有人告诉 vector 添加了数据。矢量由三部分组成 - 指向数据的指针、长度和容量。
容量是有多少内存可用,大小是使用了多少。将向量视为存储数据的 blob 时,您需要使用容量。然后,您需要通知向量使用了多少字节,以便 String::from_utf8
知道结束位置。
您会注意到,我将 unsafe
的范围更改为仅包括真正不安全的方面以及使该代码真正安全的代码。
事实上,您可以只复制 the implementation of env::current_dir
for Unix-like systems .它可以更好地处理失败情况并使用正确的类型(路径不是 字符串)。当然,调用 env::current_dir
更简单。 ^_^
fyi: I ended up with this
extern crate libc;
use std::ffi::CStr;
use std::io;
use std::str;
static BUF_BYTES: usize = 4096;
fn main() {
let buf = unsafe {
let mut buf = Vec::with_capacity(BUF_BYTES);
let ptr = buf.as_mut_ptr() as *mut libc::c_char;
if libc::getcwd(ptr, buf.capacity()).is_null() {
panic!(io::Error::last_os_error());
}
CStr::from_ptr(ptr).to_bytes()
};
println!("result: {}", str::from_utf8(buf).unwrap());
}
这是不安全,会导致崩溃(在最好的情况下)或静默内存损坏或更糟。
当一个 block 结束时,其中的任何变量都将被删除。在这种情况下,unsafe
block 创建 buf
,获取一个指向它的指针,用该指针创建一个 CStr
,然后 释放Vec
,使指针无效。然后它返回包含来自该 block 的无效引用的 CStr
。
像这样更好:
extern crate libc;
use std::ffi::{CStr, CString};
use std::io;
use std::str;
static BUF_BYTES: usize = 4096;
fn main() {
let buf = unsafe {
// Allocate some space to store the result
let mut buf = Vec::with_capacity(BUF_BYTES);
// Call the function, panicking if it fails
let ptr = buf.as_mut_ptr() as *mut libc::c_char;
if libc::getcwd(ptr, buf.capacity()).is_null() {
panic!(io::Error::last_os_error());
}
// Find the first NUL and inform the vector of that
let s = CStr::from_ptr(ptr);
buf.set_len(s.to_bytes().len());
// Transfer ownership of the Vec to a CString, ensuring there are no interior NULs
CString::new(buf)
};
let s = buf.expect("Not a C string").into_string().expect("Not UTF-8");
println!("result: {}", s);
}
I wonder why this has actually worked
可能是因为在您尝试访问内存之前没有任何更改。在高度多线程的环境中,我可能会发现更多问题。
why is it possible to have two mutable references to the vector? First as
mut buf
and then asptr = buf.as_mut_ptr()
. The ownership has not moved, has it? Otherwise, why is it possible to callbuf.capacity()
您实际上没有两个引用。 buf
拥有该值,然后您将获得一个可变指针。指针没有编译器保护,这是需要 unsafe
block 的部分原因
关于rust - 将 libc::getcwd 的输出转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196386/
我看到这两个项目非常相关,但它们之间有什么区别?官方网页并没有说明太多。 我知道ABI(Application Binary Interface)用于在不同平台之间提供低级二进制接口(interfac
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
现代 glibc 二进制文件在 Linux 中称为 libc.so.6。为什么这里用“6”? libc.so.1 或 libc.so.8 也可以是好名字恕我直言。 维基百科在 http://en.wi
我在我的 Linux Mint 14 Nadia 中安装了 Matlab(a uname -a 显示:Linux Ideapad-Z570 3.5.0-17-generic#28-Ubuntu SMP
我有一个使用 eventfd 和 timerfd 内核系统调用的应用程序。为此,您需要一个现代内核和一个支持它们的 libc,至少是 2.8。 我目前的情况是,我的系统具有正确的内核,但是 2.7.1
我有一个用 musl libc 编译的共享库 $ readelf -d ./libinterop_d.so Dynamic section at offset 0x8ecb0 contains 22
我的问题是,是否总是需要 libc.a,即使我希望链接到 libc.so。我搜索了互联网,它给出了 3 个可能的答案(冲突) 1. c program will require to link to
我安装了多个版本的 libc,我如何在编译时选择要链接的对象? 现在我正在编译 g++ prog.cpp 最佳答案 您的程序将链接到 libc。当您在 gdb 中运行时,libc-dbg 将用于读取符
我在 Ubuntu 16.04 64 位上使用 gcc 5.4.0。当我编译程序时: gcc -o prog prog.c GCC 会自动链接到 C 标准库,因此我不必专门这样做。 我如何查看 gcc
我正在为我的系统安全类编写返回 libc 攻击。一、漏洞代码: //vuln.c #include #include int loadconfig(void){ char buf[1024];
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在尝试查找信息,但我没有在 clang 网站上看到它。我想尝试在 Windows 上使用它,但我不知道它是有自己的 libc 还是使用 MS 损坏的 libc? 另一个问题:如果我用 clang
我正在使用 clang 的规定通过插件和 clang 的 LibTooling 开发插件和工具。我能够做以下事情: 从 svn(Linux 和 OSX),通过运行配置脚本(不使用 cmake)按照入门
尝试运行时出现以下错误。 lib/libc.so.6:找不到(test.so)所需的版本“GLIBC_2.7” 要解决这个问题,在编译时静态链接到 libc-2.11.a 是否安全,这样在运行时就不会
首先,我在谷歌上搜索了很多,但没有找到与我的案例相关的任何东西,我有一个ELF可执行文件,我试图在我的Ubuntu WSL中运行它,我已经更改了权限(chmod+x文件),当我运行它时,显示以下错误。
GNU page说: Your program can arrange to run its own cleanup functions if normal termination happens.
我正在尝试在我的路由器上交叉编译 MIPSEL。我在汇编中找到了一些东西,但现在我开始尝试编译基本的 C 代码。 目前只有简单的 hello world c 代码,并使用 mipsel-linux-g
libc 中预期的字符编码是什么?例如,gethostname(char name, size_t namelen);以 char 作为参数。名称参数是否应该以 utf8(保持 ascii 完整)或纯
题 如果我包括 time.h ,更改“源类型参数”并重新包含该 header ,难道不应该添加这些新定义吗?我知道这是由于包括 guard 而发生的。我的问题是:这是 libc 中的错误吗?难道它不能
我正在学习Ubuntu上的动态链接器。我想导入我需要的libc函数,但是它链接了while libc.so文件。我不知道该怎么做,所以我来这里寻求帮助。 最佳答案 如果您将程序链接到动态库(例如 li
我是一名优秀的程序员,十分优秀!