NVMeBasicContext: Fix response vector size
73030639a5ba ("Enable cppcoreguidelines-avoid-goto checks") switches
from passing the backing store of the response vector to ::write() to
using an iterator over the response vector. This exposed the flaw that
the vector was never properly sized to accommodate the presence of the
response in the backing buffer[1], though from a memory-safety
perspective the backing buffer _was_ appropriately sized from the
reserve() call.
Resize the vector both before and after to ensure that a complete
response can be captured safely while also returning a vector that is
sized in accordance with the response.
It's likely the buffer management strategy could be improved here by
instead using std::array and std::span[2].
[1] https://github.com/openbmc/dbus-sensors/issues/18
[2] https://discord.com/channels/775381525260664832/867820390406422538/959228526811312169
Fixes: #18
Fixes: 73030639a5ba ("Enable cppcoreguidelines-avoid-goto checks")
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ic2015a776389635765972084de75e33e7ea23d53
diff --git a/src/NVMeBasicContext.cpp b/src/NVMeBasicContext.cpp
index 08333c5..5708ad8 100644
--- a/src/NVMeBasicContext.cpp
+++ b/src/NVMeBasicContext.cpp
@@ -77,7 +77,7 @@
return -errno;
}
- resp.reserve(UINT8_MAX + 1);
+ resp.resize(UINT8_MAX + 1);
/* Issue the NVMe MI basic command */
size = i2c_smbus_read_block_data(fileHandle.handle(), cmd, resp.data());
@@ -96,6 +96,8 @@
return -EBADMSG;
}
+ resp.resize(size);
+
return size;
}