diff --git a/platforms/common/board_identity.c b/platforms/common/board_identity.c index 6ee0f59a59..483717b599 100644 --- a/platforms/common/board_identity.c +++ b/platforms/common/board_identity.c @@ -40,6 +40,12 @@ #include #include #include + +/** + * For special cases, specific boards may need to override the UUID, instead of using the generic + * PX4 GUID (gettable via 'board_get_px4_guid_formated' function). In that case we define the cascaded + * UUID function getters to incorporate the overridden UUID into the GUID. + */ #if defined(BOARD_OVERRIDE_UUID) || defined(BOARD_OVERRIDE_MFGUID) || defined(BOARD_OVERRIDE_PX4_GUID) static const uint16_t soc_arch_id = PX4_SOC_ARCH_ID; static const char board_uuid[17] = BOARD_OVERRIDE_UUID; @@ -72,11 +78,11 @@ int board_get_uuid32_formated(char *format_buffer, int size, int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } } diff --git a/platforms/common/include/px4_platform_common/board_common.h b/platforms/common/include/px4_platform_common/board_common.h index 051870395f..43d3412dd3 100644 --- a/platforms/common/include/px4_platform_common/board_common.h +++ b/platforms/common/include/px4_platform_common/board_common.h @@ -946,7 +946,7 @@ int board_get_mfguid_formated(char *format_buffer, int size); // DEPRICATED use int board_get_px4_guid(px4_guid_t guid); /************************************************************************************ - * Name: board_get_mfguid_formated + * Name: board_get_px4_guid_formated * * Description: * All boards either provide a way to retrieve a formatted string of the diff --git a/platforms/nuttx/src/px4/nxp/imxrt/version/board_identity.c b/platforms/nuttx/src/px4/nxp/imxrt/version/board_identity.c index 522c7400b7..6df0b18f35 100644 --- a/platforms/nuttx/src/px4/nxp/imxrt/version/board_identity.c +++ b/platforms/nuttx/src/px4/nxp/imxrt/version/board_identity.c @@ -100,15 +100,14 @@ int board_get_uuid32_formated(char *format_buffer, int size, { uuid_uint32_t uuid; board_get_uuid32(uuid); - int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { - offset += snprintf(&format_buffer[offset], size - ((i * 2 * sizeof(uint32_t)) + 1), format, uuid[i]); + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { + offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } } diff --git a/platforms/nuttx/src/px4/nxp/kinetis/version/board_identity.c b/platforms/nuttx/src/px4/nxp/kinetis/version/board_identity.c index 45d265d47b..275746cb44 100644 --- a/platforms/nuttx/src/px4/nxp/kinetis/version/board_identity.c +++ b/platforms/nuttx/src/px4/nxp/kinetis/version/board_identity.c @@ -71,15 +71,14 @@ int board_get_uuid32_formated(char *format_buffer, int size, { uuid_uint32_t uuid; board_get_uuid32(uuid); - int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { - offset += snprintf(&format_buffer[offset], size - ((i * 2 * sizeof(uint32_t)) + 1), format, uuid[i]); + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { + offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } } diff --git a/platforms/nuttx/src/px4/nxp/s32k1xx/version/board_identity.c b/platforms/nuttx/src/px4/nxp/s32k1xx/version/board_identity.c index df6c5a4a0c..cd7a4ede83 100644 --- a/platforms/nuttx/src/px4/nxp/s32k1xx/version/board_identity.c +++ b/platforms/nuttx/src/px4/nxp/s32k1xx/version/board_identity.c @@ -71,15 +71,14 @@ int board_get_uuid32_formated(char *format_buffer, int size, { uuid_uint32_t uuid; board_get_uuid32(uuid); - int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { - offset += snprintf(&format_buffer[offset], size - ((i * 2 * sizeof(uint32_t)) + 1), format, uuid[i]); + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { + offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } } diff --git a/platforms/nuttx/src/px4/rpi/rpi_common/version/board_identity.c b/platforms/nuttx/src/px4/rpi/rpi_common/version/board_identity.c index 677c3eb8a1..5e6852ff32 100644 --- a/platforms/nuttx/src/px4/rpi/rpi_common/version/board_identity.c +++ b/platforms/nuttx/src/px4/rpi/rpi_common/version/board_identity.c @@ -65,11 +65,11 @@ int board_get_uuid32_formated(char *format_buffer, int size, int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } } diff --git a/platforms/nuttx/src/px4/stm/stm32_common/version/board_identity.c b/platforms/nuttx/src/px4/stm/stm32_common/version/board_identity.c index 83d4390f5f..da2d5f1746 100644 --- a/platforms/nuttx/src/px4/stm/stm32_common/version/board_identity.c +++ b/platforms/nuttx/src/px4/stm/stm32_common/version/board_identity.c @@ -89,11 +89,11 @@ int board_get_uuid32_formated(char *format_buffer, int size, int offset = 0; int sep_size = seperator ? strlen(seperator) : 0; - for (unsigned i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { + for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) { offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]); - if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) { - strcat(&format_buffer[offset], seperator); + if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) { + strncat(&format_buffer[offset], seperator, size - offset); offset += sep_size; } }