From 272d255dfbd73d377c5d52fa97252ac6c25b5479 Mon Sep 17 00:00:00 2001 From: vaibhavsrv Date: Wed, 23 Sep 2026 09:18:16 +0530 Subject: [PATCH] fix(amd-topo): guard non-numeric byte counts in amd_memory_type --- .github/workflows/test-linux.yml | 1 + ods/installers/lib/amd-topo.sh | 2 + ods/tests/test_amd_topo_non_numeric_bytes.py | 40 ++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 ods/tests/test_amd_topo_non_numeric_bytes.py diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml index e106bd5df4..99953fdfaa 100644 --- a/.github/workflows/test-linux.yml +++ b/.github/workflows/test-linux.yml @@ -227,6 +227,7 @@ jobs: run: | python3 tests/test_compatibility_compose_list.py python3 tests/test_detection_darwin_cpu_cores.py + python3 tests/test_amd_topo_non_numeric_bytes.py bash scripts/check-compatibility.sh bash scripts/check-release-claims.sh diff --git a/ods/installers/lib/amd-topo.sh b/ods/installers/lib/amd-topo.sh index b075631068..7003625c94 100755 --- a/ods/installers/lib/amd-topo.sh +++ b/ods/installers/lib/amd-topo.sh @@ -47,6 +47,8 @@ _amd_sort_card_dirs() { amd_memory_type() { local vram_bytes="${1:-0}" local gtt_bytes="${2:-0}" + [[ "$vram_bytes" =~ ^[0-9]+$ ]] || vram_bytes=0 + [[ "$gtt_bytes" =~ ^[0-9]+$ ]] || gtt_bytes=0 local gtt_gb_int=$(( gtt_bytes / 1073741824 )) local vram_gb_int=$(( vram_bytes / 1073741824 )) diff --git a/ods/tests/test_amd_topo_non_numeric_bytes.py b/ods/tests/test_amd_topo_non_numeric_bytes.py new file mode 100644 index 0000000000..93cee049db --- /dev/null +++ b/ods/tests/test_amd_topo_non_numeric_bytes.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Regression test: verify amd_memory_type handles non-numeric and empty byte counts.""" +import subprocess +import unittest +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parent.parent +SCRIPT = REPO_ROOT / "installers" / "lib" / "amd-topo.sh" + + +class AmdTopoNonNumericBytesTests(unittest.TestCase): + def _run_amd_memory_type(self, vram, gtt): + cmd = f'. "{SCRIPT}" && amd_memory_type "{vram}" "{gtt}"' + return subprocess.run(["bash", "-euo", "pipefail", "-c", cmd], capture_output=True, text=True) + + def test_non_numeric_vram_and_gtt_fallback(self): + res = self._run_amd_memory_type("invalid", "N/A") + self.assertEqual(res.returncode, 0, f"Expected returncode 0, got {res.returncode}: {res.stderr}") + self.assertEqual(res.stdout.strip(), "discrete") + + def test_empty_strings_fallback(self): + res = self._run_amd_memory_type("", "") + self.assertEqual(res.returncode, 0, f"Expected returncode 0, got {res.returncode}: {res.stderr}") + self.assertEqual(res.stdout.strip(), "discrete") + + def test_valid_discrete_memory(self): + # 8GB VRAM (8589934592), 4GB GTT (4294967296) + res = self._run_amd_memory_type("8589934592", "4294967296") + self.assertEqual(res.returncode, 0) + self.assertEqual(res.stdout.strip(), "discrete") + + def test_valid_unified_memory(self): + # 1GB VRAM (1073741824), 32GB GTT (34359738368) + res = self._run_amd_memory_type("1073741824", "34359738368") + self.assertEqual(res.returncode, 0) + self.assertEqual(res.stdout.strip(), "unified") + + +if __name__ == "__main__": + unittest.main()