]> git.99rst.org Git - openwrt-packages.git/commitdiff
alfred: handle unreadable files in bat-hosts.lua
authorJosef Schlehofer <redacted>
Thu, 6 Aug 2026 10:42:19 +0000 (12:42 +0200)
committerJosef Schlehofer <redacted>
Tue, 18 Aug 2026 06:39:34 +0000 (08:39 +0200)
get_interface_address() calls io.open() and dereferences the result
without checking it. /sys/class/net does not contain interfaces
only: with the bonding module loaded it also holds the plain file
bonding_masters, which the `ls -1` in get_interfaces_names() returns
and which has no address below it. io.open() then returns nil and
the script dies with "attempt to index a nil value", so no bat-hosts
data is published at all on such a node.

Return nil when the file cannot be opened and skip those entries in
the caller, which would otherwise index the interface table with
nil. get_hostname() gets the same guard.

Reported-by: openwrt-ai[bot]
Signed-off-by: Josef Schlehofer <redacted>
Co-authored-by: Claude Fable 5 <redacted>
net/alfred/files/bat-hosts.lua

index f9fe586876befff7799813ffa290f30d93e1bc90..c9b5619395d648264258b3b9d144ac0fd4efd0b3 100644 (file)
@@ -4,6 +4,7 @@ local type_id = 64 -- bat-hosts
 
 function get_hostname()
   local hostfile = io.open("/proc/sys/kernel/hostname", "r")
+  if not hostfile then return nil end
   local ret_string = hostfile:read()
   hostfile:close()
   return ret_string
@@ -20,7 +21,10 @@ function get_interfaces_names()
 end
 
 function get_interface_address(name)
+  -- /sys/class/net also contains plain files, e.g. bonding_masters
+  -- once the bonding module is loaded, which have no address below them
   local addressfile = io.open("/sys/class/net/"..name.."/address", "r")
+  if not addressfile then return nil end
   local ret_string = addressfile:read()
   addressfile:close()
   return ret_string
@@ -37,7 +41,7 @@ local function generate_bat_hosts()
 
   for n, i in ipairs(get_interfaces_names()) do
     local address = get_interface_address(i)
-    if not ifaces[address] then ifaces[address] = i end
+    if address and not ifaces[address] then ifaces[address] = i end
   end
 
   for mac, iname in pairs(ifaces) do
git clone https://git.99rst.org/PROJECT