mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
slice::from_raw_parts must not be called on null pointer
This commit is contained in:
parent
76f11a4c10
commit
700e5fff8b
3 changed files with 28 additions and 15 deletions
|
@ -33,6 +33,16 @@ pub enum PsycPacketFlag {
|
||||||
PSYC_PACKET_NO_LENGTH = 2,
|
PSYC_PACKET_NO_LENGTH = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub enum PsycPacketId {
|
||||||
|
PSYC_PACKET_ID_CONTEXT = 0,
|
||||||
|
PSYC_PACKET_ID_SOURCE = 1,
|
||||||
|
PSYC_PACKET_ID_TARGET = 2,
|
||||||
|
PSYC_PACKET_ID_COUNTER = 3,
|
||||||
|
PSYC_PACKET_ID_FRAGMENT = 4,
|
||||||
|
PSYC_PACKET_ID_ELEMS = 5,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum PsycOperator {
|
pub enum PsycOperator {
|
||||||
|
@ -51,16 +61,6 @@ pub enum PsycStateOp {
|
||||||
PSYC_STATE_RESYNC = '?' as _,
|
PSYC_STATE_RESYNC = '?' as _,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
pub enum PsycPacketId {
|
|
||||||
PSYC_PACKET_ID_CONTEXT = 0,
|
|
||||||
PSYC_PACKET_ID_SOURCE = 1,
|
|
||||||
PSYC_PACKET_ID_TARGET = 2,
|
|
||||||
PSYC_PACKET_ID_COUNTER = 3,
|
|
||||||
PSYC_PACKET_ID_FRAGMENT = 4,
|
|
||||||
PSYC_PACKET_ID_ELEMS = 5,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct PsycElem {
|
pub struct PsycElem {
|
||||||
pub elem_type: PsycString,
|
pub elem_type: PsycString,
|
||||||
|
|
|
@ -300,21 +300,24 @@ impl PsycListParser {
|
||||||
|
|
||||||
PsycParseListRC::PSYC_PARSE_LIST_ELEM_START => {
|
PsycParseListRC::PSYC_PARSE_LIST_ELEM_START => {
|
||||||
let result = PsycListParserResult::ListElementStart {
|
let result = PsycListParserResult::ListElementStart {
|
||||||
value_part: util::cstring_to_slice(element.data, element.length)
|
value_part: util::cstring_to_slice(element.data,
|
||||||
|
element.length)
|
||||||
};
|
};
|
||||||
return Ok(result)
|
return Ok(result)
|
||||||
},
|
},
|
||||||
|
|
||||||
PsycParseListRC::PSYC_PARSE_LIST_ELEM_CONT => {
|
PsycParseListRC::PSYC_PARSE_LIST_ELEM_CONT => {
|
||||||
let result = PsycListParserResult::ListElementCont {
|
let result = PsycListParserResult::ListElementCont {
|
||||||
value_part: util::cstring_to_slice(element.data, element.length)
|
value_part: util::cstring_to_slice(element.data,
|
||||||
|
element.length)
|
||||||
};
|
};
|
||||||
return Ok(result)
|
return Ok(result)
|
||||||
},
|
},
|
||||||
|
|
||||||
PsycParseListRC::PSYC_PARSE_LIST_ELEM_END => {
|
PsycParseListRC::PSYC_PARSE_LIST_ELEM_END => {
|
||||||
let result = PsycListParserResult::ListElementEnd {
|
let result = PsycListParserResult::ListElementEnd {
|
||||||
value_part: util::cstring_to_slice(element.data, element.length)
|
value_part: util::cstring_to_slice(element.data,
|
||||||
|
element.length)
|
||||||
};
|
};
|
||||||
return Ok(result)
|
return Ok(result)
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,13 +1,23 @@
|
||||||
use packet_types::{RawPsycList, PsycRenderRC};
|
use packet_types::{RawPsycList, PsycRenderRC};
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn psyc_render_list(list: *const RawPsycList, buffer: *mut c_char, buflen: usize) -> PsycRenderRC;
|
fn psyc_render_list(list: *const RawPsycList, buffer: *mut c_char, buflen: usize) -> PsycRenderRC;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn cstring_to_slice<'a>(cstring: *const c_char, length: usize) -> &'a[u8] {
|
pub unsafe fn cstring_to_slice<'a>(cstring: *const c_char, length: usize)
|
||||||
slice::from_raw_parts(cstring as *const u8, length)
|
-> &'a[u8] {
|
||||||
|
match cstring {
|
||||||
|
p if p.is_null() => &[],
|
||||||
|
_ => slice::from_raw_parts(cstring as *const u8, length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn cstring_to_str<'a>(cstring: *const c_char, length: usize)
|
||||||
|
-> &'a str {
|
||||||
|
str::from_utf8_unchecked(cstring_to_slice(cstring, length))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn render_list(list: &RawPsycList) -> Vec<u8> {
|
pub unsafe fn render_list(list: &RawPsycList) -> Vec<u8> {
|
||||||
|
|
Loading…
Reference in a new issue