putta ks
putta ks

Reputation: 165

Vsnprintf crash for NULL on my embedded target

I am trying to use vsnprintf for formatting the log data on embedded board(Its arm based board).

below is the code for my log print

#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);
}

if my data is NULL for string formatting, my program crashes at vsnprintf below is sample for case.

char *dData = NULL;

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

where as on linux(my PC) this properly prints "The Obtained data is null" but on my device it crashes.

any help is appreciated

Upvotes: 0

Views: 1083

Answers (1)

Alexey Frunze
Alexey Frunze

Reputation: 62086

The C standard from 1999 says:

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.

This is the case. There's no surprise that the embedded C library chooses not to detect all possible error cases in order to save memory.

Upvotes: 4

Related Questions