Skip to content

Commit

Permalink
fix: warnings in libjsonnet.so
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed Mar 17, 2024
1 parent 5c3244a commit a7b20f9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 55 deletions.
7 changes: 7 additions & 0 deletions bindings/jsonnet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# NOTE: This library may panic, and it is only safe to panic in FFI contexts when library is built in panic="abort" mode,
# which is set for release builds of this library.
# FIXME: Move this warning somewhere else, or remove panics from this library (It is not always possible, in some cases
# there is nothing to report the error, in those cases use `abort()`)
# NOTE: This library assumes the allocator is libc malloc or alternative, which does track allocation size for user,
# see TODO in `jsonnet_realloc`.

[package]
name = "libjsonnet"
description = "Rust implementation of libjsonnet.so"
Expand Down
2 changes: 1 addition & 1 deletion bindings/jsonnet/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub unsafe extern "C" fn jsonnet_import_callback(
/// `path` should be a NUL-terminated string
#[no_mangle]
pub unsafe extern "C" fn jsonnet_jpath_add(vm: &VM, path: *const c_char) {
let cstr = CStr::from_ptr(path);
let cstr = unsafe { CStr::from_ptr(path) };
let path = PathBuf::from(cstr.to_str().unwrap());
let any_resolver = vm.state.import_resolver();
let resolver = any_resolver
Expand Down
28 changes: 15 additions & 13 deletions bindings/jsonnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,20 @@ pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *
if sz == 0 {
return std::ptr::null_mut();
}
return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());
return unsafe {
std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())
};
}
// TODO: Somehow store size of allocation, because its real size is probally not 16 :D
// OR (Alternative way of fixing this TODO)
// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,
// TODO: so it should work in normal cases. Maybe force allocator for this library?
let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();
if sz == 0 {
std::alloc::dealloc(buf, old_layout);
unsafe { std::alloc::dealloc(buf, old_layout) };
return std::ptr::null_mut();
}
std::alloc::realloc(buf, old_layout, sz)
unsafe { std::alloc::realloc(buf, old_layout, sz) }
}

/// Clean up a JSON subtree.
Expand Down Expand Up @@ -192,7 +194,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_file(
filename: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = parse_path(CStr::from_ptr(filename));
let filename = unsafe { parse_path(CStr::from_ptr(filename)) };
match vm
.state
.import(filename)
Expand Down Expand Up @@ -226,8 +228,8 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet(
snippet: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
let filename = unsafe { CStr::from_ptr(filename) };
let snippet = unsafe { CStr::from_ptr(snippet) };
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
Expand Down Expand Up @@ -275,7 +277,7 @@ fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {
out.push(0);
let v = out.as_ptr();
std::mem::forget(out);
v as *const c_char
v.cast::<c_char>()
}

/// # Safety
Expand All @@ -285,7 +287,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_file_multi(
filename: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = parse_path(CStr::from_ptr(filename));
let filename = unsafe { parse_path(CStr::from_ptr(filename)) };
match vm
.state
.import(filename)
Expand Down Expand Up @@ -313,8 +315,8 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(
snippet: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
let filename = unsafe { CStr::from_ptr(filename) };
let snippet = unsafe { CStr::from_ptr(snippet) };
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
Expand Down Expand Up @@ -367,7 +369,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_file_stream(
filename: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = parse_path(CStr::from_ptr(filename));
let filename = unsafe { parse_path(CStr::from_ptr(filename)) };
match vm
.state
.import(filename)
Expand Down Expand Up @@ -395,8 +397,8 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(
snippet: *const c_char,
error: &mut c_int,
) -> *const c_char {
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
let filename = unsafe { CStr::from_ptr(filename) };
let snippet = unsafe { CStr::from_ptr(snippet) };
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
Expand Down
18 changes: 10 additions & 8 deletions bindings/jsonnet/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::VM;
/// failure, which will appear in Jsonnet as an error. The `argv` pointer is an array whose size
/// matches the array of parameters supplied when the native callback was originally registered.
///
/// - `ctx` User pointer, given in jsonnet_native_callback.
/// - `ctx` User pointer, given in `jsonnet_native_callback`.
/// - `argv` Array of arguments from Jsonnet code.
/// - `param` success Set this byref param to 1 to indicate success and 0 for failure.
/// Returns the content of the imported file, or an error message.
Expand Down Expand Up @@ -69,17 +69,19 @@ pub unsafe extern "C" fn jsonnet_native_callback(
ctx: *const c_void,
mut raw_params: *const *const c_char,
) {
let name = CStr::from_ptr(name).to_str().expect("name is not utf-8");
let name = unsafe { CStr::from_ptr(name).to_str().expect("name is not utf-8") };
let mut params = Vec::new();
loop {
if (*raw_params).is_null() {
if (unsafe { *raw_params }).is_null() {
break;
}
let param = CStr::from_ptr(*raw_params)
.to_str()
.expect("param name is not utf-8");
let param = unsafe {
CStr::from_ptr(*raw_params)
.to_str()
.expect("param name is not utf-8")
};
params.push(param.into());
raw_params = raw_params.offset(1);
raw_params = unsafe { raw_params.offset(1) };
}

let any_resolver = vm.state.context_initializer();
Expand All @@ -91,5 +93,5 @@ pub unsafe extern "C" fn jsonnet_native_callback(
name,
#[allow(deprecated)]
NativeCallback::new(params, JsonnetNativeCallbackHandler { ctx, cb }),
)
);
}
2 changes: 1 addition & 1 deletion bindings/jsonnet/src/val_make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::VM;
/// `v` should be a NUL-terminated string
#[no_mangle]
pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {
let val = CStr::from_ptr(val);
let val = unsafe { CStr::from_ptr(val) };
let val = val.to_str().expect("string is not utf-8");
Box::into_raw(Box::new(Val::string(val)))
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/jsonnet/src/val_modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub unsafe extern "C" fn jsonnet_json_object_append(
) {
match obj {
Val::Obj(old) => old
.extend_field(CStr::from_ptr(name).to_str().unwrap().into())
.extend_field(unsafe { CStr::from_ptr(name).to_str().unwrap().into() })
.value(val.clone()),
_ => panic!("should receive object"),
}
Expand Down
16 changes: 8 additions & 8 deletions bindings/jsonnet/src/vars_tlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::VM;
/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *const c_char) {
let name = CStr::from_ptr(name);
let value = CStr::from_ptr(value);
let name = unsafe { CStr::from_ptr(name) };
let value = unsafe { CStr::from_ptr(value) };

let any_initializer = vm.state.context_initializer();
any_initializer
Expand All @@ -39,8 +39,8 @@ pub unsafe extern "C" fn jsonnet_ext_var(vm: &VM, name: *const c_char, value: *c
/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *const c_char) {
let name = CStr::from_ptr(name);
let code = CStr::from_ptr(code);
let name = unsafe { CStr::from_ptr(name) };
let code = unsafe { CStr::from_ptr(code) };

let any_initializer = vm.state.context_initializer();
any_initializer
Expand All @@ -63,8 +63,8 @@ pub unsafe extern "C" fn jsonnet_ext_code(vm: &VM, name: *const c_char, code: *c
/// `name`, `value` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value: *const c_char) {
let name = CStr::from_ptr(name);
let value = CStr::from_ptr(value);
let name = unsafe { CStr::from_ptr(name) };
let value = unsafe { CStr::from_ptr(value) };
vm.tla_args.insert(
name.to_str().expect("name is not utf-8").into(),
TlaArg::String(value.to_str().expect("value is not utf-8").into()),
Expand All @@ -80,8 +80,8 @@ pub unsafe extern "C" fn jsonnet_tla_var(vm: &mut VM, name: *const c_char, value
/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code: *const c_char) {
let name = CStr::from_ptr(name);
let code = CStr::from_ptr(code);
let name = unsafe { CStr::from_ptr(name) };
let code = unsafe { CStr::from_ptr(code) };

let name: IStr = name.to_str().expect("name is not utf-8").into();
let code: IStr = code.to_str().expect("code is not utf-8").into();
Expand Down
4 changes: 4 additions & 0 deletions cmds/jrsonnet-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ impl Printable for ArgsDesc {
format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);
p!(out, if("between args", multi_line, nl));
}
if end_comments.should_start_with_newline {
p!(out, nl);
}
format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);
p!(out, if("end args", multi_line, <i info(end)) str(")"));
}
}
Expand Down
23 changes: 0 additions & 23 deletions crates/jrsonnet-rowan-parser/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{cell::Cell, fmt, rc::Rc};

use miette::{LabeledSpan, SourceOffset, SourceSpan};
use rowan::{GreenNode, TextRange};

use crate::{
Expand Down Expand Up @@ -53,28 +52,6 @@ pub struct LocatedSyntaxError {
pub range: TextRange,
}

impl From<LocatedSyntaxError> for LabeledSpan {
fn from(val: LocatedSyntaxError) -> Self {
let span = SourceSpan::new(
SourceOffset::from(usize::from(val.range.start())),
usize::from(val.range.end() - val.range.start()),
);
dbg!(&val);
match val.error {
SyntaxError::Unexpected { expected, found } => LabeledSpan::new_with_span(
Some(format!("expected {expected}, found {found:?}")),
span,
),
SyntaxError::Missing { expected } => {
LabeledSpan::new_with_span(Some(format!("missing {expected}")), span)
}
SyntaxError::Custom { error } | SyntaxError::Hint { error } => {
LabeledSpan::new_with_span(Some(error), span)
}
}
}
}

impl Parser {
pub fn new(kinds: Vec<SyntaxKind>) -> Self {
Self {
Expand Down

0 comments on commit a7b20f9

Please sign in to comment.