Fix malloc errors!
This commit is contained in:
27
flake.nix
27
flake.nix
@@ -3,16 +3,21 @@
|
|||||||
nixpkgs.url = "github:nixos/nixpkgs";
|
nixpkgs.url = "github:nixos/nixpkgs";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs }: let
|
outputs = {
|
||||||
|
self,
|
||||||
|
nixpkgs,
|
||||||
|
}: let
|
||||||
inherit (nixpkgs) lib;
|
inherit (nixpkgs) lib;
|
||||||
systems = ["x86_64-linux"];
|
systems = ["x86_64-linux"];
|
||||||
eachSystem = f: lib.genAttrs systems (system: f {
|
eachSystem = f:
|
||||||
|
lib.genAttrs systems (system:
|
||||||
|
f {
|
||||||
inherit system;
|
inherit system;
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
ownPkgs = self.packages.${system};
|
ownPkgs = self.packages.${system};
|
||||||
});
|
});
|
||||||
in {
|
in {
|
||||||
packages = eachSystem ({ pkgs, ... }: {
|
packages = eachSystem ({pkgs, ...}: {
|
||||||
default = pkgs.rustPlatform.buildRustPackage (final: {
|
default = pkgs.rustPlatform.buildRustPackage (final: {
|
||||||
pname = "zathura-typst";
|
pname = "zathura-typst";
|
||||||
version = "0.1.0";
|
version = "0.1.0";
|
||||||
@@ -48,10 +53,22 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
devShells = eachSystem ({ pkgs, ownPkgs, ... }: {
|
devShells = eachSystem ({
|
||||||
|
pkgs,
|
||||||
|
ownPkgs,
|
||||||
|
...
|
||||||
|
}: {
|
||||||
default = pkgs.mkShell {
|
default = pkgs.mkShell {
|
||||||
inputsFrom = [ownPkgs.default];
|
inputsFrom = [ownPkgs.default];
|
||||||
packages = [pkgs.rust-analyzer pkgs.clippy pkgs.rustfmt pkgs.gdb];
|
packages = [
|
||||||
|
pkgs.rust-analyzer
|
||||||
|
pkgs.clippy
|
||||||
|
pkgs.rustfmt
|
||||||
|
pkgs.gdb
|
||||||
|
(pkgs.zathuraPkgs.zathura_core.overrideAttrs {
|
||||||
|
mesonBuildType = "debug";
|
||||||
|
})
|
||||||
|
];
|
||||||
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
|
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
# From: https://github.com/NixOS/nixpkgs/blob/1fab95f5190d087e66a3502481e34e15d62090aa/pkgs/applications/networking/browsers/firefox/common.nix#L247-L253
|
# From: https://github.com/NixOS/nixpkgs/blob/1fab95f5190d087e66a3502481e34e15d62090aa/pkgs/applications/networking/browsers/firefox/common.nix#L247-L253
|
||||||
|
|||||||
42
src/lib.rs
42
src/lib.rs
@@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
ffi::{CStr, CString, OsStr, c_void},
|
ffi::{CStr, OsStr, c_void},
|
||||||
fs,
|
fs,
|
||||||
os::unix::ffi::OsStrExt,
|
os::unix::ffi::OsStrExt,
|
||||||
path::Path,
|
path::Path,
|
||||||
@@ -41,6 +41,17 @@ mod typst;
|
|||||||
mod zathura;
|
mod zathura;
|
||||||
use typst::TypstDocument;
|
use typst::TypstDocument;
|
||||||
|
|
||||||
|
unsafe extern "C" {
|
||||||
|
safe fn calloc(n: usize, s: usize) -> *mut c_void;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn alloc_c_string(bytes: &[u8]) -> *mut i8 {
|
||||||
|
let cstr = calloc(bytes.len() + 1, 1);
|
||||||
|
unsafe { slice::from_raw_parts_mut(cstr as *mut u8, bytes.len()) }
|
||||||
|
.copy_from_slice(bytes);
|
||||||
|
cstr as *mut i8
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub static zathura_plugin_6_7: zathura::zathura_plugin_definition_s =
|
pub static zathura_plugin_6_7: zathura::zathura_plugin_definition_s =
|
||||||
@@ -151,15 +162,7 @@ unsafe extern "C" fn document_get_information(
|
|||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|(_, st)| !st.is_empty())
|
.filter(|(_, st)| !st.is_empty())
|
||||||
.map(|(ty, st)| {
|
.map(|(ty, st)| (ty, alloc_c_string(st.as_bytes())))
|
||||||
unsafe extern "C" {
|
|
||||||
safe fn calloc(n: usize, s: usize) -> *mut c_void;
|
|
||||||
}
|
|
||||||
let st_m = calloc(st.len() + 1, 1);
|
|
||||||
unsafe { slice::from_raw_parts_mut(st_m as *mut u8, st.len()) }
|
|
||||||
.copy_from_slice(st.as_bytes());
|
|
||||||
(ty, st_m as *const i8)
|
|
||||||
})
|
|
||||||
.for_each(|(ty, st)| {
|
.for_each(|(ty, st)| {
|
||||||
unsafe { lst.append_allocated(zathura_document_information_entry_new(ty, st)) };
|
unsafe { lst.append_allocated(zathura_document_information_entry_new(ty, st)) };
|
||||||
});
|
});
|
||||||
@@ -447,13 +450,7 @@ unsafe extern "C" fn page_links_get(
|
|||||||
zathura_link_target_s {
|
zathura_link_target_s {
|
||||||
destination_type:
|
destination_type:
|
||||||
zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_UNKNOWN,
|
zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_UNKNOWN,
|
||||||
value: doc
|
value: alloc_c_string(url.as_bytes()),
|
||||||
.cstring_cache
|
|
||||||
.entry(url.to_string())
|
|
||||||
.or_insert_with(|| {
|
|
||||||
CString::new(url.as_bytes()).expect("URL shouldn't contain NUL")
|
|
||||||
})
|
|
||||||
.as_ptr() as *mut i8,
|
|
||||||
page_number: 0,
|
page_number: 0,
|
||||||
left: -1.,
|
left: -1.,
|
||||||
right: -1.,
|
right: -1.,
|
||||||
@@ -506,13 +503,10 @@ unsafe extern "C" fn page_links_get(
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn page_get_label(
|
unsafe extern "C" fn page_get_label(
|
||||||
zpage: *mut zathura_page_t,
|
_zpage: *mut zathura_page_t,
|
||||||
page: *mut c_void,
|
page: *mut c_void,
|
||||||
out: *mut *mut i8,
|
out: *mut *mut i8,
|
||||||
) -> ZathuraResult {
|
) -> ZathuraResult {
|
||||||
let doc = unsafe {
|
|
||||||
&mut *(zathura_document_get_data(zathura_page_get_document(zpage)) as *mut TypstDocument)
|
|
||||||
};
|
|
||||||
let page = unsafe { &*(page as *mut Page) };
|
let page = unsafe { &*(page as *mut Page) };
|
||||||
let numbering = match page.numbering.clone() {
|
let numbering = match page.numbering.clone() {
|
||||||
Some(Numbering::Pattern(numbering)) => numbering,
|
Some(Numbering::Pattern(numbering)) => numbering,
|
||||||
@@ -520,11 +514,7 @@ unsafe extern "C" fn page_get_label(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let label = numbering.apply(&[page.number]);
|
let label = numbering.apply(&[page.number]);
|
||||||
let label = doc
|
let label = alloc_c_string(label.as_bytes());
|
||||||
.cstring_cache
|
|
||||||
.entry(label.to_string())
|
|
||||||
.or_insert_with_key(|k| CString::new(k.to_owned()).expect("It's a string"))
|
|
||||||
.as_ptr();
|
|
||||||
|
|
||||||
unsafe { out.write(label as _) };
|
unsafe { out.write(label as _) };
|
||||||
ZathuraResult::OK
|
ZathuraResult::OK
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{hash_map::Entry, HashMap}, ffi::CString, hash::Hash, io::{self, ErrorKind}, iter, path::{Path, PathBuf}, slice, sync::{LazyLock, Mutex}
|
collections::{hash_map::Entry, HashMap}, hash::Hash, io::{self, ErrorKind}, iter, path::{Path, PathBuf}, slice, sync::{LazyLock, Mutex}
|
||||||
};
|
};
|
||||||
|
|
||||||
use codespan_reporting::{
|
use codespan_reporting::{
|
||||||
@@ -131,7 +131,6 @@ impl World {
|
|||||||
pub struct TypstDocument {
|
pub struct TypstDocument {
|
||||||
pub doc: typst::layout::PagedDocument,
|
pub doc: typst::layout::PagedDocument,
|
||||||
pub warnings: Vec<SourceDiagnostic>,
|
pub warnings: Vec<SourceDiagnostic>,
|
||||||
pub cstring_cache: HashMap<String, CString>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
@@ -147,7 +146,6 @@ fn compile(path: &Path) -> Result<TypstDocument, (Option<World>, Vec<SourceDiagn
|
|||||||
Ok(doc) => Ok(TypstDocument {
|
Ok(doc) => Ok(TypstDocument {
|
||||||
doc,
|
doc,
|
||||||
warnings: warnings.to_vec(),
|
warnings: warnings.to_vec(),
|
||||||
cstring_cache: Default::default(),
|
|
||||||
}),
|
}),
|
||||||
Err(mut errors) => {
|
Err(mut errors) => {
|
||||||
errors.extend(warnings);
|
errors.extend(warnings);
|
||||||
@@ -279,7 +277,6 @@ fn diagnostics(world: &World, errors: impl IntoIterator<Item = SourceDiagnostic>
|
|||||||
TypstDocument {
|
TypstDocument {
|
||||||
doc: output.expect("Should be valid syntax"),
|
doc: output.expect("Should be valid syntax"),
|
||||||
warnings: vec![],
|
warnings: vec![],
|
||||||
cstring_cache: Default::default()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user