[string] add compile-time check for printf style arg consistency (#8277)

This commit adds `OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK` macro in
`toolchain.h`. It specifies that a function or method takes `printf`
style args. It asks the compiler to check the args for consistency
with the passed-in format string.

The commit uses the new macro in `String::Append()` and its uses are
updated to ensure consistency between format string and the args.
This commit is contained in:
Abtin Keshavarzian
2022-10-21 16:26:21 -07:00
committed by GitHub
parent acf44c45cd
commit c3bb95d5f4
8 changed files with 55 additions and 13 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (252)
#define OPENTHREAD_API_VERSION (253)
/**
* @addtogroup api-instance
+27
View File
@@ -110,6 +110,24 @@ extern "C" {
*
*/
/**
* @def OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK
*
* This macro specifies that a function or method takes `printf` style arguments and should be type-checked against
* a format string.
*
* This macro must be added after the function/method declaration. For example:
*
* `void MyPrintf(void *aObject, const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(2, 3);`
*
* The two argument index values indicate format string and first argument to check against it. They start at index 1
* for the first parameter in a function and at index 2 for the first parameter in a method.
*
* @param[in] aFmtIndex The argument index of the format string.
* @param[in] aStartIndex The argument index of the first argument to check against the format string.
*
*/
// =========== TOOLCHAIN SELECTION : START ===========
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) || defined(__TI_ARM__)
@@ -122,6 +140,9 @@ extern "C" {
#define OT_TOOL_PACKED_END __attribute__((packed))
#define OT_TOOL_WEAK __attribute__((weak))
#define OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(aFmtIndex, aStartIndex) \
__attribute__((format(printf, aFmtIndex, aStartIndex)))
#elif defined(__ICCARM__) || defined(__ICC8051__)
// http://supp.iar.com/FilesPublic/UPDINFO/004916/arm/doc/EWARM_DevelopmentGuide.ENU.pdf
@@ -133,6 +154,8 @@ extern "C" {
#define OT_TOOL_PACKED_END
#define OT_TOOL_WEAK __weak
#define OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(aFmtIndex, aStartIndex)
#elif defined(__SDCC)
// Structures are packed by default in sdcc, as it primarily targets 8-bit MCUs.
@@ -142,6 +165,8 @@ extern "C" {
#define OT_TOOL_PACKED_END
#define OT_TOOL_WEAK
#define OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(aFmtIndex, aStartIndex)
#else
#error "Error: No valid Toolchain specified"
@@ -153,6 +178,8 @@ extern "C" {
#define OT_TOOL_PACKED_END
#define OT_TOOL_WEAK
#define OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(aFmtIndex, aStartIndex)
#endif
// =========== TOOLCHAIN SELECTION : END ===========