bios_table: pldm_bios_table_string_entry_encode_check(): Handle overflow
Allow assertions to be disabled in get_bios_string_handle() by returning
PLDM_ERROR_INVALID_DATA if the next handle will will cause the allocator
state to overflow.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Id36c2f48d9fe62cd6dc10a530d4fc5174ed349b4
diff --git a/src/bios_table.c b/src/bios_table.c
index 2fe75b2..dd7b09e 100644
--- a/src/bios_table.c
+++ b/src/bios_table.c
@@ -36,12 +36,16 @@
}
}
-static uint16_t get_bios_string_handle(void)
+static int get_bios_string_handle(uint16_t *val)
{
static uint16_t handle = 0;
assert(handle != UINT16_MAX);
+ if (handle == UINT16_MAX) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
- return handle++;
+ *val = handle++;
+ return PLDM_SUCCESS;
}
LIBPLDM_ABI_STABLE
@@ -64,7 +68,12 @@
size_t length = pldm_bios_table_string_entry_encode_length(str_length);
BUFFER_SIZE_EXPECT(entry_length, length);
struct pldm_bios_string_table_entry *string_entry = entry;
- string_entry->string_handle = htole16(get_bios_string_handle());
+ uint16_t handle;
+ int rc = get_bios_string_handle(&handle);
+ if (rc != PLDM_SUCCESS) {
+ return rc;
+ }
+ string_entry->string_handle = htole16(handle);
string_entry->string_length = htole16(str_length);
memcpy(string_entry->name, str, str_length);
return PLDM_SUCCESS;