{"record":{"id":"739ffedfb30bf413","repo":"tonhowtf/omniget","slug":"express-o-regular-inv-lida","errorCode":null,"errorMessage":"expressão regular inválida: {}","messagePattern":"expressão regular inválida: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/src/platforms/bilibili/danmaku/filter.rs","lineNumber":56,"sourceCode":"\n/// O `mode` do Bilibili: 1-3 rolam da direita para a esquerda, 4 fica preso no\n/// rodapé, 5 no topo. O resto (6 reverso, 7 posicionado, 8 script) é raro e\n/// nem chega a ser desenhado no ASS, mas continua valendo no XML e no JSON.\npub fn kind_of(mode: i32) -> &'static str {\n    match mode {\n        1..=3 => KIND_SCROLL,\n        4 => KIND_BOTTOM,\n        5 => KIND_TOP,\n        _ => KIND_OTHER,\n    }\n}\n\npub fn apply(elems: &[DanmakuElem], f: &DanmakuFilter) -> anyhow::Result<Vec<DanmakuElem>> {\n    let pattern = f.regex.trim();\n    let re = if pattern.is_empty() {\n        None\n    } else {\n        Some(Regex::new(pattern).map_err(|e| anyhow!(\"expressão regular inválida: {}\", e))?)\n    };\n    let needle = f.keyword.trim().to_lowercase();\n    let kinds: Vec<&str> = f\n        .kinds\n        .iter()\n        .map(|s| s.as_str())\n        .filter(|s| !s.is_empty())\n        .collect();\n\n    let mut kept: Vec<DanmakuElem> = Vec::with_capacity(elems.len());\n    for e in elems {\n        if !kinds.is_empty() && !kinds.contains(&kind_of(e.mode)) {\n            continue;\n        }\n        let matched = match re.as_ref() {\n            Some(r) => r.is_match(&e.content),\n            None => {\n                if needle.is_empty() {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/src/platforms/bilibili/danmaku/filter.rs#L38-L74","documentation":"`DanmakuFilter::apply` compiles the user-supplied `f.regex` string with the `regex` crate; when the pattern is syntactically invalid the compile error is wrapped in `anyhow!(\"expressão regular inválida: {}\")` and propagated. Empty patterns are skipped (no filtering), so only genuinely malformed regexes trigger this.","triggerScenarios":"Calling `apply` with a DanmakuFilter whose `regex` field contains a pattern the regex crate rejects — unbalanced parentheses, invalid escapes like `\\d` inside character classes with flags, unescaped `(`, `[a-`, `*+`, or patterns exceeding size/complexity limits (e.g. huge repetition counts like `a{1000000}`).","commonSituations":"Users typing free-form regex into a danmaku filter UI without validation; porting PCRE/JS regex syntax (lookaheads `(?=...)`, backreferences `\\1`) that the Rust regex crate does not support; copy-pasting patterns with smart quotes.","solutions":["Validate the regex with `Regex::new(pattern)` before saving the filter and reject invalid input in the UI.","Remove regex constructs unsupported by the Rust regex crate (lookarounds, backreferences) or switch to `fancy-regex` if they are needed.","Trim/sanitize user input; verify no accidental characters outside the intended pattern.","Return a structured error so the frontend can point at the offending pattern field."],"exampleFix":"// before\nlet f = DanmakuFilter { regex: \"(?<=spam)\\d+\".into(), ..Default::default() };\napply(&elems, &f)?; // panics back with invalid-regex error\n// after\nif regex::Regex::new(&f.regex).is_err() { return Err(anyhow!(\"padrão não suportado\")); }\napply(&elems, &f)?;","handlingStrategy":"validation","validationCode":"fn is_valid_regex(pattern: &str) -> bool {\n    regex::Regex::new(pattern.trim()).is_ok()\n}","typeGuard":null,"tryCatchPattern":"match DanmakuFilter::apply(&elems, &filter) {\n    Err(e) if e.to_string().starts_with(\"expressão regular inválida\") => show_regex_error(&filter.regex),\n    Err(e) => return Err(e),\n    Ok(filtered) => filtered,\n}","preventionTips":["Validate the regex at input time in the UI before saving the filter.","Stick to syntax supported by the Rust regex crate (no lookarounds/backreferences).","Trim user input and strip smart quotes.","Keep patterns small; avoid huge bounded repetitions that exceed compile limits."],"tags":["regex","validation","rust"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}