Clean up error checking in fuzzer and bins

This commit is contained in:
David Tolnay 2022-07-23 12:47:09 -07:00
parent 093886f2d2
commit a69723b2d8
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
5 changed files with 9 additions and 9 deletions

View file

@ -21,7 +21,7 @@ unsafe fn fuzz_target(mut data: &[u8]) {
let mut document = MaybeUninit::<yaml_document_t>::uninit(); let mut document = MaybeUninit::<yaml_document_t>::uninit();
let document = document.as_mut_ptr(); let document = document.as_mut_ptr();
while yaml_parser_load(parser, document) != 0 { while yaml_parser_load(parser, document).ok {
let done = yaml_document_get_root_node(document).is_null(); let done = yaml_document_get_root_node(document).is_null();
yaml_document_delete(document); yaml_document_delete(document);
if done { if done {

View file

@ -21,7 +21,7 @@ unsafe fn fuzz_target(mut data: &[u8]) {
let mut event = MaybeUninit::<yaml_event_t>::uninit(); let mut event = MaybeUninit::<yaml_event_t>::uninit();
let event = event.as_mut_ptr(); let event = event.as_mut_ptr();
while yaml_parser_parse(parser, event) != 0 { while yaml_parser_parse(parser, event).ok {
let type_ = (*event).type_; let type_ = (*event).type_;
yaml_event_delete(event); yaml_event_delete(event);
if type_ == YAML_STREAM_END_EVENT { if type_ == YAML_STREAM_END_EVENT {

View file

@ -21,7 +21,7 @@ unsafe fn fuzz_target(mut data: &[u8]) {
let mut token = MaybeUninit::<yaml_token_t>::uninit(); let mut token = MaybeUninit::<yaml_token_t>::uninit();
let token = token.as_mut_ptr(); let token = token.as_mut_ptr();
while yaml_parser_scan(parser, token) != 0 { while yaml_parser_scan(parser, token).ok {
let type_ = (*token).type_; let type_ = (*token).type_;
yaml_token_delete(token); yaml_token_delete(token);
if type_ == YAML_STREAM_END_TOKEN { if type_ == YAML_STREAM_END_TOKEN {

View file

@ -47,7 +47,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let mut emitter = MaybeUninit::<yaml_emitter_t>::uninit(); let mut emitter = MaybeUninit::<yaml_emitter_t>::uninit();
let emitter = emitter.as_mut_ptr(); let emitter = emitter.as_mut_ptr();
if yaml_emitter_initialize(emitter) == 0 { if !yaml_emitter_initialize(emitter).ok {
return Err("Could not initalize the emitter object".into()); return Err("Could not initalize the emitter object".into());
} }
@ -75,7 +75,7 @@ pub(crate) unsafe fn unsafe_main(
let mut anchor = [0u8; 256]; let mut anchor = [0u8; 256];
let mut tag = [0u8; 256]; let mut tag = [0u8; 256];
let ok = if line.starts_with(b"+STR") { let result = if line.starts_with(b"+STR") {
yaml_stream_start_event_initialize(event, YAML_UTF8_ENCODING) yaml_stream_start_event_initialize(event, YAML_UTF8_ENCODING)
} else if line.starts_with(b"-STR") { } else if line.starts_with(b"-STR") {
yaml_stream_end_event_initialize(event) yaml_stream_end_event_initialize(event)
@ -133,10 +133,10 @@ pub(crate) unsafe fn unsafe_main(
break Err(format!("Unknown event: '{}'", CStr::from_ptr(line)).into()); break Err(format!("Unknown event: '{}'", CStr::from_ptr(line)).into());
}; };
if ok == 0 { if !result.ok {
break Err("Memory error: Not enough memory for creating an event".into()); break Err("Memory error: Not enough memory for creating an event".into());
} }
if yaml_emitter_emit(emitter, event) == 0 { if !yaml_emitter_emit(emitter, event).ok {
break Err(match (*emitter).error { break Err(match (*emitter).error {
YAML_MEMORY_ERROR => "Memory error: Not enough memory for emitting".into(), YAML_MEMORY_ERROR => "Memory error: Not enough memory for emitting".into(),
YAML_WRITER_ERROR => { YAML_WRITER_ERROR => {

View file

@ -40,7 +40,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit(); let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr(); let parser = parser.as_mut_ptr();
if yaml_parser_initialize(parser) == 0 { if !yaml_parser_initialize(parser).ok {
return Err("Could not initialize the parser object".into()); return Err("Could not initialize the parser object".into());
} }
@ -66,7 +66,7 @@ pub(crate) unsafe fn unsafe_main(
let mut event = MaybeUninit::<yaml_event_t>::uninit(); let mut event = MaybeUninit::<yaml_event_t>::uninit();
let event = event.as_mut_ptr(); let event = event.as_mut_ptr();
loop { loop {
if yaml_parser_parse(parser, event) == 0 { if !yaml_parser_parse(parser, event).ok {
let mut error = format!("Parse error: {}", CStr::from_ptr((*parser).problem)); let mut error = format!("Parse error: {}", CStr::from_ptr((*parser).problem));
if (*parser).problem_mark.line != 0 || (*parser).problem_mark.column != 0 { if (*parser).problem_mark.line != 0 || (*parser).problem_mark.column != 0 {
let _ = write!( let _ = write!(