bios_table: Relax pldm_bios_table_string_entry_decode_string_check()
Allow the string decoded by
pldm_bios_table_string_entry_decode_string_check() to be truncated, as
per the unchecked pldm_bios_table_string_entry_decode_string(). With
this change pldm_bios_table_string_entry_decode_string_check() provides
assert() safety but is otherwise equivalent.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ia872f1496c642efbb34ee3ae29455f4eaf574082
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e990245..b19d23e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,6 +40,8 @@
To disable `oem-ibm` in your development builds, pass `-Doem-ibm=disabled`
when invoking `meson setup`
+4. bios_table: Relax pldm_bios_table_string_entry_decode_string_check()
+
### Removed
1. libpldm: Remove the requester-api option
diff --git a/include/libpldm/bios_table.h b/include/libpldm/bios_table.h
index 09831f4..f56b066 100644
--- a/include/libpldm/bios_table.h
+++ b/include/libpldm/bios_table.h
@@ -144,9 +144,11 @@
* @param[out] buffer - Pointer to a buffer to store the string
* @param[in] size - Size of the buffer to store the string
* @return PLDM_SUCCESS on success, PLDM_ERROR_INVALID_DATA if entry or buffer are NULL, or
- * PLDM_ERROR_INVALID_LENGTH if size is insufficient for the decoded string. An
+ * PLDM_ERROR_INVALID_LENGTH if size is insufficient for NUL termination. An
* appropriate value for size can be determined using the expression
- * `pldm_bios_table_string_entry_decode_string_length(entry) + 1`.
+ * `pldm_bios_table_string_entry_decode_string_length(entry) + 1`. The provided size value
+ * may be smaller than the entry's string, in which case the string placed in buffer will
+ * be truncated (but still NUL terminated).
*/
int pldm_bios_table_string_entry_decode_string_check(
const struct pldm_bios_string_table_entry *entry, char *buffer,
diff --git a/src/bios_table.c b/src/bios_table.c
index 436f3e6..d6cf6f3 100644
--- a/src/bios_table.c
+++ b/src/bios_table.c
@@ -114,9 +114,9 @@
{
POINTER_CHECK(entry);
POINTER_CHECK(buffer);
- size_t length =
- pldm_bios_table_string_entry_decode_string_length(entry);
- BUFFER_SIZE_EXPECT(size, length + 1);
+ if (size == 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
pldm_bios_table_string_entry_decode_string(entry, buffer, size);
return PLDM_SUCCESS;
}
diff --git a/tests/libpldm_bios_table_test.cpp b/tests/libpldm_bios_table_test.cpp
index 4cefa30..c677b99 100644
--- a/tests/libpldm_bios_table_test.cpp
+++ b/tests/libpldm_bios_table_test.cpp
@@ -1006,9 +1006,10 @@
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0);
- rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(),
- buffer.size() - 1);
- EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+ /* Ensure equivalence with the unchecked API */
+ rc = pldm_bios_table_string_entry_decode_string_check(
+ entry, buffer.data(), 2 + 1 /* sizeof '\0' */);
+ EXPECT_EQ(rc, std::strcmp("Al", buffer.data()));
}
TEST(StringTable, IteratorTest)