Sanitize device fields and prevent crash with numeric hostnames

This patch improves the resilience of the guess_icon function by sanitizing mac, vendor, and name fields to avoid crashes caused by unexpected data types (e.g., numeric hostnames).

Specifically:

mac is now cast to a string before being uppercased, with a newly added fallback to "00:00:00:00:00:00" if empty or invalid.

vendor is sanitized to a string before lowercasing, still defaulting to "unknown".

name is cast to a string before lowercasing, still falling back to "(unknown)" when empty.

This change not only resolves the error caused by numeric-only hostnames (which triggered an AttributeError due to calling .lower() on an int), but also proactively prevents similar crashes from malformed or unexpected input in the future.

References: Fixes issue #1088 and also let's me sleep a little easier tonight.
This commit is contained in:
Ashtin
2025-06-20 00:46:24 -04:00
parent cecfe60bac
commit 2a4198c2c8

View File

@@ -674,9 +674,9 @@ def guess_icon(vendor, mac, ip, name, default):
mylog('debug', [f"[guess_icon] Guessing icon for (vendor|mac|ip|name): ('{vendor}'|'{mac}'|{ip}|{name})"])
result = default
mac = mac.upper()
vendor = vendor.lower() if vendor else "unknown"
name = name.lower() if name else "(unknown)"
mac = str(mac).upper() if mac else "00:00:00:00:00:00"
vendor = str(vendor).lower() if vendor else "unknown"
name = str(name).lower() if name else "(unknown)"
# Guess icon based on vendor
if any(brand in vendor for brand in {"samsung", "motorola"}):