Skip to content

fix: memory leaks and invalid CSS in WebVTT encoder#2164

Open
Varadraj75 wants to merge 2 commits intoCCExtractor:masterfrom
Varadraj75:fix/webvtt-memory-leaks-and-css
Open

fix: memory leaks and invalid CSS in WebVTT encoder#2164
Varadraj75 wants to merge 2 commits intoCCExtractor:masterfrom
Varadraj75:fix/webvtt-memory-leaks-and-css

Conversation

@Varadraj75
Copy link
Contributor

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.
  • I have mentioned this change in the changelog.

My familiarity with the project is as follows (check one):

  • I have never used CCExtractor.
  • I have used CCExtractor just a couple of times.
  • I absolutely love CCExtractor, but have not contributed previously.
  • I am an active contributor to CCExtractor.

Summary

Three bugs fixed in src/lib_ccx/ccx_encoders_webvtt.c, reported in #2154.

Fix 1 — Remove 6 unnecessary strdup() memory leaks

Inside write_cc_buffer_as_webvtt(), string literals "<i>", "<u>", "<c.",
"</c>", "</u>", "</i>" were heap-allocated with strdup() and passed to
write_wrapped() but never freed. Since write_wrapped() takes a void* and
doesn't require heap memory, the strdup() calls were completely unnecessary.

This ran in a per-character, per-row, per-subtitle inner loop — leaking thousands
of tiny allocations per styled broadcast hour.

Fix 2 — Invalid CSS rgba(0, 256, 0, 0.5)rgba(0, 255, 0, 0.5)

CSS color channels are 0–255. The value 256 is out of range and generates
technically invalid CSS that strict parsers and validators reject.

Fix 3 — Missing free(unescaped) on write-error path

In write_stringz_as_webvtt(), the error path after a failed encoded_crlf
write freed el but not unescaped, unlike the adjacent error path above it
which correctly freed both. Added the missing free(unescaped) before return -1.

Testing

All existing CI tests pass. Built and verified locally on macOS.

Fixes #2154

- Remove 6 unnecessary strdup() calls on string literals in
  write_cc_buffer_as_webvtt() — literals are passed directly to
  write_wrapped() which takes void*, no heap allocation needed.
  This runs in a per-character inner loop and leaked on every
  styled subtitle in a broadcast.
- Fix invalid CSS: rgba(0, 256, 0, 0.5) -> rgba(0, 255, 0, 0.5)
  CSS color channels are 0-255; 256 is out of range.
- Fix missing free(unescaped) on write-error path in
  write_stringz_as_webvtt() — matched the existing pattern on
  the adjacent error path which correctly freed both el and unescaped.

Fixes CCExtractor#2154
Copilot AI review requested due to automatic review settings March 2, 2026 10:57
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes multiple correctness and memory-safety issues in the WebVTT encoder path (src/lib_ccx/ccx_encoders_webvtt.c) reported in #2154, improving long-running stability and standards compliance for generated WebVTT/CSS output.

Changes:

  • Replace unnecessary strdup() usage in the WebVTT styling write loop to eliminate repeated per-caption memory leaks.
  • Fix invalid inline CSS color channel value (rgba(0, 256, 0, 0.5)rgba(0, 255, 0, 0.5)).
  • Ensure unescaped is freed on the encoded_crlf write-error path in write_stringz_as_webvtt().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/lib_ccx/ccx_encoders_webvtt.c Removes inner-loop leaks, corrects CSS output, and fixes an error-path leak in WebVTT writing.
docs/CHANGES.TXT Adds a changelog entry describing the WebVTT fixes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit f377be9...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 7/7
DVD 3/3
DVR-MS 2/2
General 27/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 86/86
Teletext 21/21
WTV 13/13
XDS 34/34

Congratulations: Merging this PR would fix the following tests:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65..., Last passed: Never
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b..., Last passed: Never
  • ccextractor --out=spupng c83f765c66..., Last passed: Never
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never

All tests passed completely.

Check the result page for more info.

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on windows. Below is a summary of the test results, when compared to test for commit f377be9...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 6/7
DVD 3/3
DVR-MS 2/2
General 25/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 80/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --out=spupng c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

@Varadraj75
Copy link
Contributor Author

The Windows CI failures are pre-existing and unrelated to this PR.

All failing tests show "Last passed: Never" in the CI summary, confirming
they were already broken on master before this change. The failures show
Empty path name is not legal — a Windows test runner path issue, not
a CCExtractor code issue.

Linux CI passes all 237 tests cleanly. This PR only modifies
ccx_encoders_webvtt.c and docs/CHANGES.TXT.

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.

[BUG] Memory leaks and invalid CSS in WebVTT encoder (ccx_encoders_webvtt.c)

3 participants