Skip to content

fix(dvb_subtitle_decoder): add NULL checks after malloc calls#1794

Merged
cfsmp3 merged 2 commits intomasterfrom
fix/dvb-subtitle-memory-safety
Dec 12, 2025
Merged

fix(dvb_subtitle_decoder): add NULL checks after malloc calls#1794
cfsmp3 merged 2 commits intomasterfrom
fix/dvb-subtitle-memory-safety

Conversation

@cfsmp3
Copy link
Copy Markdown
Contributor

@cfsmp3 cfsmp3 commented Dec 12, 2025

Summary

This PR adds missing NULL checks for 9 malloc() calls in the DVB subtitle decoder (src/lib_ccx/dvb_subtitle_decoder.c) that could cause crashes or undefined behavior if memory allocation fails.

Changes

All checks use fatal(EXIT_NOT_ENOUGH_MEMORY, ...) to terminate gracefully with an appropriate error message, consistent with the approach used in matroska.c and other parts of the codebase.

Affected Functions and Allocations

Function Allocation Line
dvbsub_init_decoder() DVBSubContext 424
dvbsub_parse_clut_segment() DVBSubCLUT 1146
dvbsub_parse_region_segment() DVBSubRegion 1254
dvbsub_parse_region_segment() region->pbuf 1287
dvbsub_parse_region_segment() DVBSubObject 1332
dvbsub_parse_region_segment() DVBSubObjectDisplay 1347
dvbsub_parse_page_segment() DVBSubRegionDisplay 1438
write_dvb_sub() cc_bitmap (rect) 1553
write_dvb_sub() rect->data1 1636
write_dvb_sub() rect->data0 1656
dvbsub_handle_display_segment() private_data 1768

Additional Fix

This also fixes a potential memory leak in write_dvb_sub() where rect and rect->data1 would be leaked if the rect->data0 allocation failed (previously returned -1 without cleanup, now terminates via fatal()).

Context

This is part of a systematic effort to improve memory safety across the CCExtractor codebase. The DVB subtitle decoder was identified as having 28 memory-related function calls, making it a high-priority target for review.

Test plan

  • Code compiles without errors
  • Run with DVB subtitle streams to verify normal operation
  • Memory allocation failures will now produce clear error messages instead of crashes

🤖 Generated with Claude Code

cfsmp3 and others added 2 commits December 12, 2025 20:01
This commit addresses multiple memory safety issues in the Matroska
parser identified through static analysis (cppcheck).

## Null pointer dereference after malloc (15 fixes)

Added null checks after all malloc/calloc calls to prevent crashes
when memory allocation fails:

- read_byte_block(): line 28
- read_bytes_signed(): line 38
- generate_timestamp_ass_ssa(): line 267
- parse_segment_cluster_block_group_block(): lines 306, 361
- parse_segment_cluster_block_group_block_additions(): line 405
- parse_segment_cluster_block_group(): line 476
- parse_segment_track_entry(): lines 958, 973
- parse_private_codec_data(): line 1019
- generate_filename_from_track(): line 1167
- ass_ssa_sentence_erase_read_order(): line 1191
- save_sub_track(): lines 1264, 1271, 1303, 1310
- matroska_loop(): lines 1496, 1505

## Buffer overflow fixes (3 fixes)

- generate_timestamp_ass_ssa(): Increased buffer from 15 to 32 bytes,
  changed sprintf to snprintf. GCC warned output could be 11-23 bytes.
- save_sub_track(): Increased number[] buffer from 9 to 16 bytes,
  changed sprintf to snprintf.
- generate_filename_from_track(): Now calculates required buffer size
  dynamically instead of using fixed 200 bytes.

## Memory leak fixes (7 fixes)

- parse_ebml(): Fixed leak of read_vint_block_string() return value
- parse_segment_info(): Fixed 4 leaks of read_vint_block_string()
  returns (filename, title, muxing_app, writing_app)
- parse_segment_track_entry(): Added free(lang) before reassignment
- save_sub_track(): Fixed leak where text pointer was advanced,
  losing original allocation

## Realloc error handling (3 fixes)

Fixed realloc calls to use temporary variable, preventing loss of
original pointer if realloc fails:

- parse_segment_cluster_block_group_block(): line 366
- parse_segment_cluster_block_group(): line 475
- parse_segment_track_entry(): line 973

## Use-after-free fix (1 fix)

- matroska_loop(): Saved avc_track_number and dec_sub.got_output
  before calling matroska_free_all(), then used saved values

## Missing free fixes (2 fixes)

- free_sub_track(): Added free(track->sentences) for the array itself
- matroska_free_all(): Added free(mkv_ctx->sub_tracks) for the array

## Other improvements

- Initialized sub_track->sentences to NULL in parse_segment_track_entry()
  to ensure safe NULL check in free_sub_track()

All changes use EXIT_NOT_ENOUGH_MEMORY (exit code 500) for
out-of-memory conditions, consistent with the rest of the codebase.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add missing NULL checks for 9 malloc() calls in the DVB subtitle decoder
that could cause crashes or undefined behavior if memory allocation fails.

All checks use fatal(EXIT_NOT_ENOUGH_MEMORY, ...) to terminate gracefully
with an appropriate error message, consistent with the approach used in
matroska.c and other parts of the codebase.

Affected functions and allocations:
- dvbsub_init_decoder(): DVBSubContext allocation
- dvbsub_parse_clut_segment(): DVBSubCLUT allocation
- dvbsub_parse_region_segment(): DVBSubRegion, pbuf, DVBSubObject,
  and DVBSubObjectDisplay allocations
- dvbsub_parse_page_segment(): DVBSubRegionDisplay allocation
- write_dvb_sub(): cc_bitmap (rect), data1, and data0 allocations
- dvbsub_handle_display_segment(): private_data allocation

This also fixes a potential memory leak in write_dvb_sub() where rect
and rect->data1 would be leaked if the rect->data0 allocation failed
(previously returned -1 without cleanup, now terminates via fatal()).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@cfsmp3 cfsmp3 merged commit 810c869 into master Dec 12, 2025
26 of 30 checks passed
@cfsmp3 cfsmp3 deleted the fix/dvb-subtitle-memory-safety branch December 12, 2025 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant