1
0
Fork 0
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:
lurchi 2016-09-07 15:14:35 +02:00
parent 76f11a4c10
commit 700e5fff8b
3 changed files with 28 additions and 15 deletions

View file

@ -33,6 +33,16 @@ pub enum PsycPacketFlag {
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)]
#[repr(C)]
pub enum PsycOperator {
@ -51,16 +61,6 @@ pub enum PsycStateOp {
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)]
pub struct PsycElem {
pub elem_type: PsycString,

View file

@ -300,21 +300,24 @@ impl PsycListParser {
PsycParseListRC::PSYC_PARSE_LIST_ELEM_START => {
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)
},
PsycParseListRC::PSYC_PARSE_LIST_ELEM_CONT => {
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)
},
PsycParseListRC::PSYC_PARSE_LIST_ELEM_END => {
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)
},

View file

@ -1,13 +1,23 @@
use packet_types::{RawPsycList, PsycRenderRC};
use std::slice;
use std::os::raw::c_char;
use std::str;
extern "C" {
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] {
slice::from_raw_parts(cstring as *const u8, length)
pub unsafe fn cstring_to_slice<'a>(cstring: *const c_char, length: usize)
-> &'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> {