Skip to content

Commit 721a3dc

Browse files
author
Dhanush Varma
committed
fix: crash when reading from stdin with no data
Two bugs: (1) general_loop.c dereferences NULL dec_ctx in the live_stream progress code when no data was processed. (2) Rust switch_to_next_file calls demux_ctx.open() for stdin, triggering null pointer dereference via ptr::null(). Fix: add NULL check for dec_ctx in general_loop.c, and return early from switch_to_next_file for stdin/network/tcp sources instead of calling demux_ctx.open().
1 parent 9f250b1 commit 721a3dc

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/lib_ccx/general_loop.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ int general_loop(struct lib_ccx_ctx *ctx)
15071507
if (!data_node)
15081508
continue;
15091509
}
1510-
if (ctx->live_stream)
1510+
if (ctx->live_stream && dec_ctx)
15111511
{
15121512
LLONG t = get_fts(dec_ctx->timing, dec_ctx->current_field);
15131513
if (!t && ctx->demux_ctx->global_timestamp_inited)
@@ -1574,7 +1574,9 @@ int general_loop(struct lib_ccx_ctx *ctx)
15741574
}
15751575

15761576
// void segment_output_file(struct lib_ccx_ctx *ctx, struct lib_cc_decode *dec_ctx);
1577-
segment_output_file(ctx, dec_ctx);
1577+
// dec_ctx is NULL only when no data was processed (e.g. empty stdin)
1578+
if (dec_ctx)
1579+
segment_output_file(ctx, dec_ctx);
15781580

15791581
if (ccx_options.send_to_srv)
15801582
net_check_conn();

src/rust/src/file_functions/file.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,25 @@ pub unsafe fn switch_to_next_file(
152152
bytes_in_buffer: i64,
153153
ccx_options: &mut Options,
154154
) -> i32 {
155-
let mut ret = 0;
156-
157155
// 1. Initially reset condition
158156
let mut demux_ctx = copy_demuxer_from_c_to_rust(ctx.demux_ctx);
159157
if ctx.current_file == -1 || !ccx_options.binary_concat {
160158
demux_ctx.reset();
161159
}
162160

163161
// 2. Handle special input sources
164-
#[allow(deref_nullptr)]
165162
match ccx_options.input_source {
166163
DataSource::Stdin | DataSource::Network | DataSource::Tcp => {
167-
demux_ctx.open(*ptr::null(), ccx_options);
168-
return match ret {
169-
r if r < 0 => 0,
170-
r if r > 0 => r,
171-
_ => 1,
172-
};
164+
// stdin/network/tcp don't use file-based switching.
165+
// Return success immediately — fd is configured in the main open() path.
166+
return 1;
173167
}
174168
_ => {}
175169
}
176170

171+
#[allow(unused_assignments)]
172+
let mut ret = 0;
173+
177174
// 3. Close current file handling
178175

179176
if demux_ctx.is_open() {

0 commit comments

Comments
 (0)