Skip to content

[FIX]: Replace Language::from_str().expect() with graceful fallback in copy_to_rust()#2238

Open
NexionisJake wants to merge 1 commit intoCCExtractor:masterfrom
NexionisJake:fix/dvblang-panic-unknown-language
Open

[FIX]: Replace Language::from_str().expect() with graceful fallback in copy_to_rust()#2238
NexionisJake wants to merge 1 commit intoCCExtractor:masterfrom
NexionisJake:fix/dvblang-panic-unknown-language

Conversation

@NexionisJake
Copy link
Copy Markdown
Contributor

In raising this pull request, I confirm the following :

Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample
    exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors
    guide
    .
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog. If it's just a bug fix, I have NOT added it to
    the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Repro instructions:

ccextractor input.ts --dvblang xyz
                                                                                                                         
With any unrecognized language code, CCExtractor panics immediately:                                                     
                                                                                                                         
thread 'main' panicked at 'Invalid language', src/rust/src/common.rs:427                                                 
                                                                                                                         
No subtitle extraction occurs and no useful error is shown to the user.                                                  
                                                                                                                         
---                                                                                                                      
Root cause      
                                                                                                                         
Language::from_str(&lang_str).expect("Invalid language") in copy_to_rust()
(src/rust/src/common.rs) uses strum's EnumString derive. For any string not                                              
present in the Language enum, from_str() returns Err and .expect() panics                                                
unconditionally — crashing the process instead of emitting an error message.                                             
                                                                                                                         
Fix                                                                                                                      
                                                                                                                         
Replaced .expect("Invalid language") with .unwrap_or_else(|_| { … }) that                                                
prints a descriptive warning to stderr and falls back to Language::default()
(Language::Und), allowing CCExtractor to continue processing.                                                            
                                                                                                                         
Before:                                                                                                                  
Language::from_str(&c_char_to_string((*ccx_s_options).dvblang))                                                          
    .expect("Invalid language")                                                                                          
// --dvblang xyz  →  thread 'main' panicked at 'Invalid language'                                                        
                                                                                                                         
After:                                                                                                                   
let lang_str = c_char_to_string((*ccx_s_options).dvblang);                                                               
Language::from_str(&lang_str).unwrap_or_else(|_| {                                                                       
    eprintln!("Warning: unrecognized language code '{lang_str}', defaulting to 'und'");                                  
    Language::default()                                                                
})                                                                                                                       
// --dvblang xyz  →  Warning: unrecognized language code 'xyz', defaulting to 'und'
                                                                                   
Only src/rust/src/common.rs is changed. cargo build and cargo clippy both
pass with zero new warnings.                                                                                             
                                                                                                                         
Fixes #2231                                                                                                              

Language::from_str().expect("Invalid language") panicked unconditionally
when the user passed any string not in the Language enum (e.g. --dvblang xyz),
producing a raw Rust panic instead of a useful error message.

Replace .expect() with .unwrap_or_else() that prints a human-readable warning:
  "Warning: unrecognized language code 'xyz', defaulting to 'und'"
and falls back to Language::default(), allowing CCExtractor to continue
processing rather than crashing.

Fixes CCExtractor#2231
@ccextractor-bot
Copy link
Copy Markdown
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 d56a6be...:
Report Name Tests Passed
Broken 9/13
CEA-708 1/14
DVB 3/7
DVD 3/3
DVR-MS 2/2
General 20/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 75/86
Teletext 20/21
WTV 12/13
XDS 21/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 8e8229b88b...
  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 132d7df7e9...
  • ccextractor --autoprogram --out=ttxt --latin1 99e5eaafdc...
  • ccextractor --autoprogram --out=srt --latin1 b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 7aad20907e...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --autoprogram --out=ttxt --latin1 01509e4d27...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --out=spupng c83f765c66...
  • ccextractor --no-codec dvbsub c83f765c66...
  • ccextractor --vides --out=srt c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --out=srt --latin1 f23a544ba8...
  • ccextractor --autoprogram --out=smptett --latin1 --ucla e274a73653...
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla e274a73653...
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla 85058ad37e...
  • ccextractor --autoprogram --out=srt --latin1 --ucla b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 27fab4dbb6...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds bbd5bb52fc...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds b992e0cccb...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds d0291cdcf6...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds c8dc039a88...
  • ccextractor --autoprogram --out=srt --latin1 --ucla 83b03036a2...
  • ccextractor --autoprogram --out=srt --latin1 --ucla 88cd42b89a...
  • ccextractor --autoprogram --out=srt --latin1 --output-field 2 --ucla 88cd42b89a...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 7f41299cc7...

NOTE: The following tests have been failing on the master branch as well as the PR:

Congratulations: Merging this PR would fix the following tests:

  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never

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.

@ccextractor-bot
Copy link
Copy Markdown
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 d56a6be...:
Report Name Tests Passed
Broken 9/13
CEA-708 1/14
DVB 4/7
DVD 3/3
DVR-MS 2/2
General 22/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 81/86
Teletext 20/21
WTV 13/13
XDS 31/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 8e8229b88b...
  • ccextractor --autoprogram --out=ttxt --latin1 132d7df7e9...
  • ccextractor --autoprogram --out=ttxt --latin1 99e5eaafdc...
  • ccextractor --autoprogram --out=srt --latin1 b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 7aad20907e...
  • ccextractor --autoprogram --out=ttxt --latin1 01509e4d27...
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla 85058ad37e...
  • ccextractor --autoprogram --out=srt --latin1 --ucla b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 7f41299cc7...

NOTE: The following tests have been failing on the master branch as well as the PR:

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

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.

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.

2 participants