mirror of
https://github.com/HKUDS/CLI-Anything.git
synced 2026-08-28 23:27:04 +08:00
fix(obs-studio): reject NaN in validate_range (#396)
NaN bypassed min/max checks because NaN comparisons are always false, so invalid opacity/volume values could be stored. Reject non-finite floats before the range compare.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
"""validate_range must reject non-finite opacity-like values."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from cli_anything.obs_studio.utils.obs_utils import validate_range
|
||||
|
||||
|
||||
def test_nan_rejected() -> None:
|
||||
with pytest.raises(ValueError, match="finite"):
|
||||
validate_range(float("nan"), 0.0, 1.0, "Opacity")
|
||||
|
||||
|
||||
def test_inf_rejected() -> None:
|
||||
with pytest.raises(ValueError, match="finite"):
|
||||
validate_range(float("inf"), 0.0, 1.0, "Opacity")
|
||||
|
||||
|
||||
def test_in_range_ok() -> None:
|
||||
assert validate_range(0.5, 0.0, 1.0, "Opacity") == 0.5
|
||||
@@ -1,6 +1,7 @@
|
||||
"""OBS Studio CLI - JSON helpers and utilities."""
|
||||
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import copy
|
||||
from typing import Dict, Any, List, Optional
|
||||
@@ -27,6 +28,8 @@ def unique_name(name: str, items: List[Dict[str, Any]], key: str = "name") -> st
|
||||
def validate_range(value: float, min_val: float, max_val: float, name: str) -> float:
|
||||
"""Validate that a value is within range."""
|
||||
val = float(value)
|
||||
if not math.isfinite(val):
|
||||
raise ValueError(f"{name} must be a finite number, got {val}")
|
||||
if val < min_val or val > max_val:
|
||||
raise ValueError(f"{name} must be between {min_val} and {max_val}, got {val}")
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user