ESP_LOGD crashes when a %s parameter is NULL

ataweg
Posts: 1
Joined: Sat Jan 20, 2018 1:42 pm

ESP_LOGD crashes when a %s parameter is NULL

Postby ataweg » Sat Jan 20, 2018 2:00 pm

The following code snipplet results in a program crash:

Code: Select all

 char *string = NULL;
 ESP_LOGD( TAG, "*** crash here %s", string );

Since the ESP_LOG * functions are often used for debugging, it is more than annoying when these functions themselves crash, because the parameters are not well defined.

JimmyPedersen
Posts: 13
Joined: Sun Nov 15, 2015 4:14 am

Re: ESP_LOGD crashes when a %s parameter is NULL

Postby JimmyPedersen » Sat Jan 20, 2018 9:58 pm

ataweg wrote:The following code snipplet results in a program crash:

Code: Select all

 char *string = NULL;
 ESP_LOGD( TAG, "*** crash here %s", string );

Since the ESP_LOG * functions are often used for debugging, it is more than annoying when these functions themselves crash, because the parameters are not well defined.


I would hazard to guess that the the esp_log_write function (used by the ESP_LOG macro) uses the standard vsnprintf and unless they change that any other solution would be non-trivial.
While the NULL string issue is irritating, it has bitten me more than once, it seems to be the standard for printf-like funtions to act like this (How ever annoying).

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.


To wrap it up, it's to save space.


Maybe you can use something like:

Code: Select all

#define SS(s) (s!=NULL?s:"")   // Safe strings. Check string for NULL

or if you prefer

Code: Select all

#define SS(s) (s!=NULL?s:"<NULL>")   // Safe strings. Check string for NULL


and then wrap strings in ESP_LOGD outputs like so:

Code: Select all

 char *string = NULL;
 ESP_LOGD( TAG, "*** crash here %s", SS(string) );

Who is online

Users browsing this forum: No registered users and 1 guest