From b27b4665ac076873d6c0e27a6009d8b10fb5d264 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sun, 5 Dec 2021 20:25:29 -0800 Subject: [PATCH 1/3] test: fix incorrect uses of i8 instead of c_char Fixes build on aarch64, ppc64le and other platforms that have c_char defined as u8. --- cli/src/tests/highlight_test.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cli/src/tests/highlight_test.rs b/cli/src/tests/highlight_test.rs index 6b09d64c..e0b356d2 100644 --- a/cli/src/tests/highlight_test.rs +++ b/cli/src/tests/highlight_test.rs @@ -1,6 +1,7 @@ use super::helpers::fixtures::{get_highlight_config, get_language, get_language_queries_path}; use lazy_static::lazy_static; use std::ffi::CString; +use std::os::raw::c_char; use std::sync::atomic::{AtomicUsize, Ordering}; use std::{fs, ptr, slice, str}; use tree_sitter_highlight::{ @@ -489,15 +490,15 @@ fn test_highlighting_via_c_api() { ]; let highlight_names = highlights .iter() - .map(|h| h["class=".len()..].as_ptr() as *const i8) + .map(|h| h["class=".len()..].as_ptr() as *const c_char) .collect::>(); let highlight_attrs = highlights .iter() - .map(|h| h.as_bytes().as_ptr() as *const i8) + .map(|h| h.as_bytes().as_ptr() as *const c_char) .collect::>(); let highlighter = c::ts_highlighter_new( - &highlight_names[0] as *const *const i8, - &highlight_attrs[0] as *const *const i8, + &highlight_names[0] as *const *const c_char, + &highlight_attrs[0] as *const *const c_char, highlights.len() as u32, ); @@ -515,9 +516,9 @@ fn test_highlighting_via_c_api() { js_scope.as_ptr(), js_injection_regex.as_ptr(), language, - highlights_query.as_ptr() as *const i8, - injections_query.as_ptr() as *const i8, - locals_query.as_ptr() as *const i8, + highlights_query.as_ptr() as *const c_char, + injections_query.as_ptr() as *const c_char, + locals_query.as_ptr() as *const c_char, highlights_query.len() as u32, injections_query.len() as u32, locals_query.len() as u32, @@ -534,8 +535,8 @@ fn test_highlighting_via_c_api() { html_scope.as_ptr(), html_injection_regex.as_ptr(), language, - highlights_query.as_ptr() as *const i8, - injections_query.as_ptr() as *const i8, + highlights_query.as_ptr() as *const c_char, + injections_query.as_ptr() as *const c_char, ptr::null(), highlights_query.len() as u32, injections_query.len() as u32, From cdf2ecd176fe9ac8876092dfd385875d6e280139 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sun, 5 Dec 2021 20:29:31 -0800 Subject: [PATCH 2/3] tags: fix incorrect uses of i8 instead of c_char Fixes build on aarch64, ppc64le and other platforms that have c_char defined as u8. --- tags/src/c_lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tags/src/c_lib.rs b/tags/src/c_lib.rs index 8f689a9a..088cc7bc 100644 --- a/tags/src/c_lib.rs +++ b/tags/src/c_lib.rs @@ -215,9 +215,9 @@ pub extern "C" fn ts_tags_buffer_tags_len(this: *const TSTagsBuffer) -> u32 { } #[no_mangle] -pub extern "C" fn ts_tags_buffer_docs(this: *const TSTagsBuffer) -> *const i8 { +pub extern "C" fn ts_tags_buffer_docs(this: *const TSTagsBuffer) -> *const c_char { let buffer = unwrap_ptr(this); - buffer.docs.as_ptr() as *const i8 + buffer.docs.as_ptr() as *const c_char } #[no_mangle] @@ -237,7 +237,7 @@ pub extern "C" fn ts_tagger_syntax_kinds_for_scope_name( this: *mut TSTagger, scope_name: *const c_char, len: *mut u32, -) -> *const *const i8 { +) -> *const *const c_char { let tagger = unwrap_mut_ptr(this); let scope_name = unsafe { unwrap(CStr::from_ptr(scope_name).to_str()) }; let len = unwrap_mut_ptr(len); @@ -245,7 +245,7 @@ pub extern "C" fn ts_tagger_syntax_kinds_for_scope_name( *len = 0; if let Some(config) = tagger.languages.get(scope_name) { *len = config.c_syntax_type_names.len() as u32; - return config.c_syntax_type_names.as_ptr() as *const *const i8; + return config.c_syntax_type_names.as_ptr() as *const *const c_char; } std::ptr::null() } From fe33599f4695c4ab442e1e5f8682cfda11321308 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Tue, 22 Mar 2022 22:47:21 -0700 Subject: [PATCH 3/3] lib: fix incorrect int ptr cast on big-endian architectures `*usize` -> `*u32` conversion on 64-bit big-endian machine takes high halfword of the value. As a consequence, any result returned via `count` is unexpectedly shifted left: u32 = 00 00 00 01 // 1 usize = 00 00 00 01 00 00 00 00 // 4294967296 Fixes following test failure: ``` $ cargo test -- tests::corpus_test <...> running 13 tests memory allocation of 206158430208 bytes failed error: test failed, to rerun pass '--lib' ``` --- lib/binding_rust/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index e88a411c..1df9e7ac 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -705,14 +705,14 @@ impl Tree { /// after calling one of the [Parser::parse] functions. Call it on the old tree that /// was passed to parse, and pass the new tree that was returned from `parse`. pub fn changed_ranges(&self, other: &Tree) -> impl ExactSizeIterator { - let mut count = 0; + let mut count = 0u32; unsafe { let ptr = ffi::ts_tree_get_changed_ranges( self.0.as_ptr(), other.0.as_ptr(), - &mut count as *mut _ as *mut u32, + &mut count as *mut u32, ); - util::CBufferIter::new(ptr, count).map(|r| r.into()) + util::CBufferIter::new(ptr, count as usize).map(|r| r.into()) } } }