Fix up guid
GUIDs have some cases where they might not be representable as a string,
due to an overflow. To handle this previously, the existing
implementation just allocated extra space.
To ensure that we are always publishing correct guids, break this
function down into an add_guid method that we can call anytime we add a
guid to json. This function can use the appropriate guid string length,
and if we go over, we can make sure that we don't publish the string at
all, by handling the appropriate error codes.
Change-Id: I98239b7d5ba7567cea1b016579d7566e292b6e81
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/cper-utils.c b/cper-utils.c
index 11fdbcb..c37796d 100644
--- a/cper-utils.c
+++ b/cper-utils.c
@@ -384,12 +384,18 @@
}
//Helper function to convert an EDK EFI GUID into a string for intermediate use.
-void guid_to_string(char *out, EFI_GUID *guid)
+int guid_to_string(char *out, size_t out_len, EFI_GUID *guid)
{
- sprintf(out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
- guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
- guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
- guid->Data4[5], guid->Data4[6], guid->Data4[7]);
+ size_t len = snprintf(
+ out, out_len,
+ "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", guid->Data1,
+ guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1],
+ guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5],
+ guid->Data4[6], guid->Data4[7]);
+ if (len != out_len) {
+ return -1;
+ }
+ return len;
}
//Helper function to convert a string into an EDK EFI GUID.
@@ -446,3 +452,15 @@
json_object_new_string_len(str, fru_text_len));
}
}
+
+void add_guid(json_object *ir, const char *field_name, EFI_GUID *guid)
+{
+ char platform_string[GUID_STRING_LENGTH + 1];
+ if (!guid_to_string(platform_string, sizeof(platform_string), guid)) {
+ return;
+ }
+ json_object_object_add(
+ ir, field_name,
+ json_object_new_string_len(platform_string,
+ sizeof(platform_string) - 1));
+}