Vsnprintf 在我的嵌入式目标上因 NULL 而崩溃

Vsnprintf crash for NULL on my embedded target

我正在尝试使用 vsnprintf 格式化嵌入式板(基于 arm 的板)上的日志数据。

下面是我的日志打印代码

#define Max_log_len 1024
char logBuf[Max_log_len+1] = { 0 };

printMessage(const char* Format,...)
{
    va_list logList;
    va_start(logList,  Format);
    vsnprintf(logBuf ,  Max_log_len,Format, logList);
    va_end(logList);
    sendMessageto(logBuf);
}

如果我的字符串格式化数据为 NULL,我的程序会在 vsnprintf 处崩溃 以下是案例示例。

char *dData = NULL;

printMessage("The Obtained data is [%s]",dData);

在 linux(我的电脑)上可以正确打印 "The Obtained data is null" 但在我的设备上它会崩溃。

感谢任何帮助

1999 年的 C 标准说:

7.1.4 Use of library functions

1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer,or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.

是这样的。嵌入式 C 库选择不检测所有可能的错误情况以节省内存也就不足为奇了。