Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent nvim_buf_get_name frees buf->b_ffname #89

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions crates/nvim-api/src/ffi/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use nvim_types::{
Array,
BufHandle,
Dictionary,
Error,
Integer,
LuaRef,
NonOwning,
Object,
String,
Array, BufHandle, Dictionary, Error, Integer, LuaRef, NonOwning, Object,
Str, String,
};

use crate::opts::{
KeyDict_get_commands,
KeyDict_keymap,
KeyDict_user_command,
KeyDict_get_commands, KeyDict_keymap, KeyDict_user_command,
};

extern "C" {
Expand Down Expand Up @@ -118,8 +109,7 @@ extern "C" {
) -> Array;

// https://github.com/neovim/neovim/blob/master/src/nvim/api/buffer.c#L1086
pub(crate) fn nvim_buf_get_name(buf: BufHandle, err: *mut Error)
-> String;
pub(crate) fn nvim_buf_get_name(buf: BufHandle, err: *mut Error) -> Str;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a breaking change. Str might need to implement some traits. Maybe by duplicating some code from string.rs
The main different between Str and String is the former doesn't implement custom Drop to drop ffi memory.


// https://github.com/neovim/neovim/blob/master/src/nvim/api/buffer.c#L876
pub(crate) fn nvim_buf_get_offset(
Expand Down
2 changes: 2 additions & 0 deletions crates/nvim-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ mod non_owning;
mod object;
#[cfg(feature = "serde")]
pub mod serde;
mod str;
mod string;

pub use crate::str::Str;
pub use array::{Array, ArrayIterator};
pub use dictionary::{DictIterator, Dictionary, KeyValuePair};
pub use error::Error;
Expand Down
28 changes: 28 additions & 0 deletions crates/nvim-types/src/str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{
ffi::{c_char, OsStr},
os::unix::prelude::OsStrExt,
path::PathBuf,
slice,
};

#[repr(C)]
pub struct Str {
pub(crate) data: *mut c_char,
pub(crate) size: usize,
}

impl Str {
fn as_bytes(&self) -> &[u8] {
if self.data.is_null() {
&[]
} else {
unsafe { slice::from_raw_parts(self.data as *const u8, self.size) }
}
}
}

impl From<Str> for PathBuf {
fn from(s: Str) -> Self {
OsStr::from_bytes(s.as_bytes()).to_owned().into()
}
}