From ec6cc5c35509a76b73b9fbb3261c4af1aa758af5 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Wed, 24 Jul 2019 23:37:56 +0300 Subject: [PATCH] Allocate dc_kml_t in a rusty way --- src/dc_location.rs | 12 +++++++----- src/dc_mimeparser.rs | 28 ++++++++++++++++------------ src/dc_receive_imf.rs | 19 +++++++++++-------- tests/stress.rs | 41 +++++++++++++++++++---------------------- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/dc_location.rs b/src/dc_location.rs index 6c61eca6a..26b9a7854 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -481,8 +481,8 @@ pub unsafe fn dc_kml_parse( context: &Context, content: *const libc::c_char, content_bytes: size_t, -) -> *mut dc_kml_t { - let mut kml: *mut dc_kml_t = calloc(1, ::std::mem::size_of::()) as *mut dc_kml_t; +) -> dc_kml_t { + let mut kml = dc_kml_t::new(); let mut content_nullterminated: *mut libc::c_char = 0 as *mut libc::c_char; let mut saxparser: dc_saxparser_t = dc_saxparser_t { starttag_cb: None, @@ -499,8 +499,11 @@ pub unsafe fn dc_kml_parse( } else { content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int); if !content_nullterminated.is_null() { - (*kml).locations = dc_array_new_typed(1, 100 as size_t); - dc_saxparser_init(&mut saxparser, kml as *mut libc::c_void); + kml.locations = dc_array_new_typed(1, 100 as size_t); + dc_saxparser_init( + &mut saxparser, + &mut kml as *mut dc_kml_t as *mut libc::c_void, + ); dc_saxparser_set_tag_handler( &mut saxparser, Some(kml_starttag_cb), @@ -641,7 +644,6 @@ pub unsafe fn dc_kml_unref(kml: *mut dc_kml_t) { } dc_array_unref((*kml).locations); free((*kml).addr as *mut libc::c_void); - free(kml as *mut libc::c_void); } pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mut dc_job_t) { diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index 5f2da89a8..d19278fff 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -56,8 +56,8 @@ pub struct dc_mimeparser_t<'a> { pub context: &'a Context, pub reports: *mut carray, pub is_system_message: libc::c_int, - pub location_kml: *mut dc_kml_t, - pub message_kml: *mut dc_kml_t, + pub location_kml: Option, + pub message_kml: Option, } // deprecated @@ -83,8 +83,8 @@ pub unsafe fn dc_mimeparser_new(context: &Context) -> dc_mimeparser_t { context, reports: carray_new(16i32 as libc::c_uint), is_system_message: 0, - location_kml: std::ptr::null_mut(), - message_kml: std::ptr::null_mut(), + location_kml: None, + message_kml: None, } } @@ -134,11 +134,15 @@ pub unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) { (*mimeparser).decrypting_failed = 0i32; dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper); - dc_kml_unref((*mimeparser).location_kml); - (*mimeparser).location_kml = 0 as *mut dc_kml_t; + if let Some(location_kml) = (*mimeparser).location_kml.as_mut() { + dc_kml_unref(location_kml as *mut dc_kml_t); + } + (*mimeparser).location_kml = None; - dc_kml_unref((*mimeparser).message_kml); - (*mimeparser).message_kml = 0 as *mut dc_kml_t; + if let Some(message_kml) = (*mimeparser).message_kml.as_mut() { + dc_kml_unref(message_kml as *mut dc_kml_t); + } + (*mimeparser).message_kml = None; } unsafe fn dc_mimepart_unref(mut mimepart: *mut dc_mimepart_t) { @@ -1392,11 +1396,11 @@ unsafe fn dc_mimeparser_add_single_part_if_known( 4, ) == 0i32 { - (*mimeparser).location_kml = dc_kml_parse( + (*mimeparser).location_kml = Some(dc_kml_parse( (*mimeparser).context, decoded_data, decoded_data_bytes, - ); + )); current_block = 8795901732489102124; } else if strncmp( desired_filename, @@ -1411,11 +1415,11 @@ unsafe fn dc_mimeparser_add_single_part_if_known( 4, ) == 0i32 { - (*mimeparser).message_kml = dc_kml_parse( + (*mimeparser).message_kml = Some(dc_kml_parse( (*mimeparser).context, decoded_data, decoded_data_bytes, - ); + )); current_block = 8795901732489102124; } else { dc_replace_bad_utf8_chars(desired_filename); diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index f59aaaf08..d3775c957 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -454,7 +454,7 @@ pub unsafe fn dc_receive_imf( } let part = carray_get(mime_parser.parts, i as libc::c_uint) as *mut dc_mimepart_t; if !(0 != (*part).is_meta) { - if !mime_parser.location_kml.is_null() + if !mime_parser.location_kml.is_none() && icnt == 1 && !(*part).msg.is_null() && (strcmp( @@ -772,18 +772,18 @@ pub unsafe fn dc_receive_imf( i = i.wrapping_add(1) } } - if !mime_parser.message_kml.is_null() && chat_id > 9 as libc::c_uint { + if !mime_parser.message_kml.is_none() && chat_id > 9 as libc::c_uint { let mut location_id_written = false; let mut send_event = false; - if !mime_parser.message_kml.is_null() + if !mime_parser.message_kml.is_none() && chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint { let newest_location_id: uint32_t = dc_save_locations( context, chat_id, from_id, - (*mime_parser.message_kml).locations, + mime_parser.message_kml.unwrap().locations, 1, ); if 0 != newest_location_id && 0 == hidden { @@ -793,20 +793,23 @@ pub unsafe fn dc_receive_imf( } } - if !mime_parser.location_kml.is_null() + if !mime_parser.location_kml.is_none() && chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint { let contact = dc_get_contact(context, from_id); - if !(*mime_parser.location_kml).addr.is_null() + if !mime_parser.location_kml.as_ref().unwrap().addr.is_null() && !contact.is_null() && !(*contact).addr.is_null() - && strcasecmp((*contact).addr, (*mime_parser.location_kml).addr) == 0 + && strcasecmp( + (*contact).addr, + mime_parser.location_kml.as_ref().unwrap().addr, + ) == 0 { let newest_location_id = dc_save_locations( context, chat_id, from_id, - (*mime_parser.location_kml).locations, + mime_parser.location_kml.as_ref().unwrap().locations, 0, ); if newest_location_id != 0 && hidden == 0 && !location_id_written { diff --git a/tests/stress.rs b/tests/stress.rs index 099486c9e..89beddf4b 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -837,34 +837,31 @@ fn test_dc_kml_parse() { b"\n\n\n2019-03-06T21:09:57Z9.423110,53.790302\n\n \n\t2018-12-13T22:11:12Z\t 19.423110 \t , \n 63.790302\n \n\n\x00" as *const u8 as *const libc::c_char; - let kml: *mut dc_kml_t = dc_kml_parse(&context.ctx, xml, strlen(xml)); + let mut kml = dc_kml_parse(&context.ctx, xml, strlen(xml)); - assert!(!(*kml).addr.is_null()); - assert_eq!( - as_str((*kml).addr as *const libc::c_char), - "user@example.org", - ); + assert!(!kml.addr.is_null()); + assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); - assert_eq!(dc_array_get_cnt((*kml).locations), 2); + assert_eq!(dc_array_get_cnt(kml.locations), 2); - assert!(dc_array_get_latitude((*kml).locations, 0) > 53.6f64); - assert!(dc_array_get_latitude((*kml).locations, 0) < 53.8f64); - assert!(dc_array_get_longitude((*kml).locations, 0) > 9.3f64); - assert!(dc_array_get_longitude((*kml).locations, 0) < 9.5f64); - assert!(dc_array_get_accuracy((*kml).locations, 0) > 31.9f64); - assert!(dc_array_get_accuracy((*kml).locations, 0) < 32.1f64); - assert_eq!(dc_array_get_timestamp((*kml).locations, 0), 1551906597); + assert!(dc_array_get_latitude(kml.locations, 0) > 53.6f64); + assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64); + assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64); + assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64); + assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64); + assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64); + assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597); - assert!(dc_array_get_latitude((*kml).locations, 1) > 63.6f64); - assert!(dc_array_get_latitude((*kml).locations, 1) < 63.8f64); - assert!(dc_array_get_longitude((*kml).locations, 1) > 19.3f64); - assert!(dc_array_get_longitude((*kml).locations, 1) < 19.5f64); - assert!(dc_array_get_accuracy((*kml).locations, 1) > 2.4f64); - assert!(dc_array_get_accuracy((*kml).locations, 1) < 2.6f64); + assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64); + assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64); + assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64); + assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64); + assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64); + assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64); - assert_eq!(dc_array_get_timestamp((*kml).locations, 1), 1544739072); + assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072); - dc_kml_unref(kml); + dc_kml_unref(&mut kml); } }