common: trim null bytes from filename in readNotifyAsync()
readNotifyAsync() uses inotify_event to monitor filesystem changes.
The name field in inotify_event contains a null-terminated name, and
may include additional null bytes to pad the structure for alignment as
mentioned in [1] inotify(7).
If the padded name (including extra null bytes) is passed directly to
processUpdate(), the comparison against the expected filename will
fail, even when the visible parts of the names match.
This change ensures the filename is trimmed at the first null byte
before being passed to processUpdate(), preventing false mismatches
and ensuring correct processing.
Tested on Yosemite5, and confirmed that the BIOS version is retrieved
correctly when host0_bios_version.txt is created.
[1] https://www.man7.org/linux/man-pages/man7/inotify.7.html
Change-Id: Ic0ccd7f4e163eb650baa65045c9af1e7be01964b
Signed-off-by: Kevin Tung <kevin.tung.openbmc@gmail.com>
diff --git a/common/include/NotifyWatch.hpp b/common/include/NotifyWatch.hpp
index 8b5c795..4a22ef3 100644
--- a/common/include/NotifyWatch.hpp
+++ b/common/include/NotifyWatch.hpp
@@ -101,8 +101,10 @@
buffer.data() + offset + offsetof(inotify_event, name)),
len[0]};
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
- co_await static_cast<Instance*>(this)->processUpdate(
- std::string(name.begin(), name.end()));
+ auto fileName = std::string(
+ name.data(),
+ std::find(name.data(), name.data() + name.size(), '\0'));
+ co_await static_cast<Instance*>(this)->processUpdate(fileName);
}
offset += offsetof(inotify_event, name) + len[0];
}