Skip to content

Commit 099fa05

Browse files
authored
[FIX] 134 Codes in XDS and General Tests (#1708)
* Made pointers valid in Unit Tests of Decoder * fix: test_do_cb * Copilot Suggestions * Suggestions about Redundancy * Suggestions about Redundancy
1 parent e663eca commit 099fa05

File tree

2 files changed

+87
-79
lines changed

2 files changed

+87
-79
lines changed

src/rust/src/decoder/mod.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,24 @@ pub struct Dtvcc<'a> {
4444
impl<'a> Dtvcc<'a> {
4545
/// Create a new dtvcc context
4646
pub fn new(ctx: &'a mut dtvcc_ctx) -> Self {
47-
let report = if !ctx.report.is_null() {
48-
unsafe { Box::new(*(ctx.report)) }
49-
} else {
50-
Box::new(ccx_decoder_dtvcc_report::default())
51-
};
52-
let encoder = if !ctx.encoder.is_null() {
53-
unsafe { Box::new(*(ctx.encoder as *mut encoder_ctx)) }
54-
} else {
55-
Box::new(encoder_ctx::default())
56-
};
57-
let timing = if !ctx.timing.is_null() {
58-
unsafe { Box::new(*(ctx.timing)) }
59-
} else {
60-
Box::new(ccx_common_timing_ctx::default())
61-
};
47+
let report = unsafe { &mut *ctx.report };
48+
let encoder = unsafe { &mut *(ctx.encoder as *mut encoder_ctx) };
49+
let timing = unsafe { &mut *ctx.timing };
50+
6251
Self {
6352
is_active: is_true(ctx.is_active),
6453
active_services_count: ctx.active_services_count as u8,
6554
services_active: ctx.services_active.to_vec(),
6655
report_enabled: is_true(ctx.report_enabled),
67-
report: Box::leak(report),
56+
report,
6857
decoders: ctx.decoders.iter_mut().collect(),
6958
packet: ctx.current_packet.to_vec(),
7059
packet_length: ctx.current_packet_length as u8,
7160
is_header_parsed: is_true(ctx.is_current_packet_header_parsed),
7261
last_sequence: ctx.last_sequence,
73-
encoder: Box::leak(encoder),
62+
encoder,
7463
no_rollup: is_true(ctx.no_rollup),
75-
timing: Box::leak(timing),
64+
timing,
7665
}
7766
}
7867
/// Process cc data and add it to the dtvcc packet
@@ -247,13 +236,27 @@ impl PartialEq for dtvcc_symbol {
247236
}
248237

249238
#[cfg(test)]
250-
mod test {
239+
pub mod test {
251240
use lib_ccxr::util::log::{set_logger, CCExtractorLogger, DebugMessageMask, OutputTarget};
252241

253242
use crate::utils::get_zero_allocated_obj;
254243

255244
use super::*;
256245

246+
pub fn initialize_dtvcc_ctx() -> Box<dtvcc_ctx> {
247+
let mut ctx = get_zero_allocated_obj::<dtvcc_ctx>();
248+
249+
// Initialize the required pointers to avoid null pointer dereference
250+
let report = Box::new(ccx_decoder_dtvcc_report::default());
251+
ctx.report = Box::into_raw(report);
252+
253+
let encoder = Box::new(encoder_ctx::default());
254+
ctx.encoder = Box::into_raw(encoder) as *mut _ as *mut std::os::raw::c_void;
255+
256+
let timing = Box::new(ccx_common_timing_ctx::default());
257+
ctx.timing = Box::into_raw(timing);
258+
ctx
259+
}
257260
#[test]
258261
fn test_process_cc_data() {
259262
set_logger(CCExtractorLogger::new(
@@ -262,7 +265,9 @@ mod test {
262265
false,
263266
))
264267
.ok();
265-
let mut dtvcc_ctx = get_zero_allocated_obj::<dtvcc_ctx>();
268+
269+
let mut dtvcc_ctx = initialize_dtvcc_ctx();
270+
266271
let mut decoder = Dtvcc::new(&mut dtvcc_ctx);
267272

268273
// Case 1: cc_type = 2
@@ -311,7 +316,9 @@ mod test {
311316
false,
312317
))
313318
.ok();
314-
let mut dtvcc_ctx = get_zero_allocated_obj::<dtvcc_ctx>();
319+
320+
let mut dtvcc_ctx = initialize_dtvcc_ctx();
321+
315322
let mut decoder = Dtvcc::new(&mut dtvcc_ctx);
316323

317324
// Case 1: Without providing last_sequence

src/rust/src/lib.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::os::windows::io::{FromRawHandle, RawHandle};
2727

2828
use args::Args;
2929
use bindings::*;
30-
use cfg_if::cfg_if;
30+
use cfg_if::cfg_if;
3131
use clap::{error::ErrorKind, Parser};
3232
use common::{copy_from_rust, CType, CType2};
3333
use decoder::Dtvcc;
@@ -43,62 +43,62 @@ use std::{
4343
os::raw::{c_char, c_double, c_int, c_long, c_uint},
4444
};
4545

46-
// Mock data for rust unit tests
47-
cfg_if! {
48-
if #[cfg(test)] {
49-
static mut cb_708: c_int = 0;
50-
static mut cb_field1: c_int = 0;
51-
static mut cb_field2: c_int = 0;
52-
static mut current_fps: c_double = 30.0;
53-
static mut usercolor_rgb: [c_int; 8] = [0; 8];
54-
static mut FILEBUFFERSIZE: c_int = 0;
55-
static mut MPEG_CLOCK_FREQ: c_int = 90000;
56-
57-
static mut frames_since_ref_time: c_int = 0;
58-
static mut total_frames_count: c_uint = 0;
59-
static mut fts_at_gop_start: c_long = 0;
60-
static mut gop_rollover: c_int = 0;
61-
static mut pts_big_change: c_uint = 0;
62-
63-
static mut tlt_config: ccx_s_teletext_config = unsafe { std::mem::zeroed() };
64-
static mut ccx_options: ccx_s_options = unsafe { std::mem::zeroed() };
65-
static mut gop_time: gop_time_code = unsafe { std::mem::zeroed() };
66-
static mut first_gop_time: gop_time_code = unsafe { std::mem::zeroed() };
67-
static mut ccx_common_timing_settings: ccx_common_timing_settings_t = unsafe { std::mem::zeroed() };
68-
static mut capitalization_list: word_list = unsafe { std::mem::zeroed() };
69-
static mut profane: word_list = unsafe { std::mem::zeroed() };
70-
71-
unsafe extern "C" fn version(_location: *const c_char) {}
72-
unsafe extern "C" fn set_binary_mode() {}
73-
}
74-
}
75-
76-
// External C symbols (only when not testing)
77-
#[cfg(not(test))]
78-
extern "C" {
79-
static mut cb_708: c_int;
80-
static mut cb_field1: c_int;
81-
static mut cb_field2: c_int;
82-
static mut current_fps: c_double;
83-
static mut usercolor_rgb: [c_int; 8];
84-
static mut FILEBUFFERSIZE: c_int;
85-
static mut MPEG_CLOCK_FREQ: c_int;
86-
static mut tlt_config: ccx_s_teletext_config;
87-
static mut ccx_options: ccx_s_options;
88-
static mut frames_since_ref_time: c_int;
89-
static mut total_frames_count: c_uint;
90-
static mut gop_time: gop_time_code;
91-
static mut first_gop_time: gop_time_code;
92-
static mut fts_at_gop_start: c_long;
93-
static mut gop_rollover: c_int;
94-
static mut ccx_common_timing_settings: ccx_common_timing_settings_t;
95-
static mut capitalization_list: word_list;
96-
static mut profane: word_list;
97-
static mut pts_big_change: c_uint;
98-
99-
fn version(location: *const c_char);
100-
fn set_binary_mode();
101-
}
46+
// Mock data for rust unit tests
47+
cfg_if! {
48+
if #[cfg(test)] {
49+
static mut cb_708: c_int = 0;
50+
static mut cb_field1: c_int = 0;
51+
static mut cb_field2: c_int = 0;
52+
static mut current_fps: c_double = 30.0;
53+
static mut usercolor_rgb: [c_int; 8] = [0; 8];
54+
static mut FILEBUFFERSIZE: c_int = 0;
55+
static mut MPEG_CLOCK_FREQ: c_int = 90000;
56+
57+
static mut frames_since_ref_time: c_int = 0;
58+
static mut total_frames_count: c_uint = 0;
59+
static mut fts_at_gop_start: c_long = 0;
60+
static mut gop_rollover: c_int = 0;
61+
static mut pts_big_change: c_uint = 0;
62+
63+
static mut tlt_config: ccx_s_teletext_config = unsafe { std::mem::zeroed() };
64+
static mut ccx_options: ccx_s_options = unsafe { std::mem::zeroed() };
65+
static mut gop_time: gop_time_code = unsafe { std::mem::zeroed() };
66+
static mut first_gop_time: gop_time_code = unsafe { std::mem::zeroed() };
67+
static mut ccx_common_timing_settings: ccx_common_timing_settings_t = unsafe { std::mem::zeroed() };
68+
static mut capitalization_list: word_list = unsafe { std::mem::zeroed() };
69+
static mut profane: word_list = unsafe { std::mem::zeroed() };
70+
71+
unsafe extern "C" fn version(_location: *const c_char) {}
72+
unsafe extern "C" fn set_binary_mode() {}
73+
}
74+
}
75+
76+
// External C symbols (only when not testing)
77+
#[cfg(not(test))]
78+
extern "C" {
79+
static mut cb_708: c_int;
80+
static mut cb_field1: c_int;
81+
static mut cb_field2: c_int;
82+
static mut current_fps: c_double;
83+
static mut usercolor_rgb: [c_int; 8];
84+
static mut FILEBUFFERSIZE: c_int;
85+
static mut MPEG_CLOCK_FREQ: c_int;
86+
static mut tlt_config: ccx_s_teletext_config;
87+
static mut ccx_options: ccx_s_options;
88+
static mut frames_since_ref_time: c_int;
89+
static mut total_frames_count: c_uint;
90+
static mut gop_time: gop_time_code;
91+
static mut first_gop_time: gop_time_code;
92+
static mut fts_at_gop_start: c_long;
93+
static mut gop_rollover: c_int;
94+
static mut ccx_common_timing_settings: ccx_common_timing_settings_t;
95+
static mut capitalization_list: word_list;
96+
static mut profane: word_list;
97+
static mut pts_big_change: c_uint;
98+
99+
fn version(location: *const c_char);
100+
fn set_binary_mode();
101+
}
102102

103103
/// Initialize env logger with custom format, using stdout as target
104104
#[no_mangle]
@@ -352,7 +352,8 @@ mod test {
352352

353353
#[test]
354354
fn test_do_cb() {
355-
let mut dtvcc_ctx = utils::get_zero_allocated_obj::<dtvcc_ctx>();
355+
let mut dtvcc_ctx = crate::decoder::test::initialize_dtvcc_ctx();
356+
356357
let mut dtvcc = Dtvcc::new(&mut dtvcc_ctx);
357358

358359
let mut decoder_ctx = lib_cc_decode::default();

0 commit comments

Comments
 (0)