From 39c13287c53bd81d151116b6af434498cf3da0be Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Thu, 5 Oct 2023 16:01:32 +0200 Subject: live: hw_inventory: fix empty memory bank bug When a client's hardware presents an empty memory bank and invalid call to _bytes_to_human is performed because None is passed as a parameter. size = _bytes_to_human(obj.get('size', None)) Fix this by checking if 'size' is present in the JSON output from lshw. If size is present then map the bytes to a human readable string using _bytes_to_human, if no size is present then use 'Empty slot' to indicate that the memory bank is not being used. --- src/utils/hw_inventory.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/utils') diff --git a/src/utils/hw_inventory.py b/src/utils/hw_inventory.py index 2f38ae2..e7cb463 100644 --- a/src/utils/hw_inventory.py +++ b/src/utils/hw_inventory.py @@ -64,7 +64,7 @@ class HardwareInventory(): def _bytes_to_human(size): suffixes = ['B', 'MiB', 'GiB', 'TiB'] if type(size) is not int: - raise TypeError('Invalid type') + raise TypeError(f'Invalid type in _bytes_to_human, got: {size} {type(size)}') for exponent, suffix in enumerate(suffixes, start=1): conv = size / (1024**exponent) if conv < 1024: @@ -106,11 +106,14 @@ def _process_core_cpu(inventory, obj): def _process_core_mem_bank(inventory, obj): slot = obj.get('slot', 'Unknown slot') - size = _bytes_to_human(obj.get('size', None)) - if size: - mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), size, f'({slot})']) - mem_elem = HardwareElement(HardwareType.MEMORY, mem) - inventory.add_element(mem_elem) + if 'size' in obj: + size = obj['size'] + human_size = _bytes_to_human(size) + mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), human_size, f'({slot})']) + else: + mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), 'Empty slot', f'({slot})']) + mem_elem = HardwareElement(HardwareType.MEMORY, mem) + inventory.add_element(mem_elem) def _process_core_mem(inventory, obj): -- cgit v1.2.3-18-g5258