snprintf hardfaulting on buffer of zero length

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

snprintf hardfaulting on buffer of zero length

Jump to solution
1,720 Views
jaaphollander
Contributor II

Hi all,

Today I noticed that when using redlib's snprintf in the manner below results in an hardfault on my cortex m3 cpu.

length = snprintf(NULL, 0, "Test: %s", "test123");

I'd expect it to return the length of the string, but it didn't...

What am i doing wrong?

Kind Regards,

Jaap

1 Solution
1,476 Views
lpcxpresso_supp
NXP Employee
NXP Employee

You are correct, you should be able to pass NULL buffer into the snprintf function when specifying a total of 0 bytes to write into the buffer (in order to work out how big a buffer is actually needed).

But with the current version of Redlib supplied with MCUXpresso IDE v10.2 (and earlier), this is not implemented and is causing a hard fault. We'll investigate fixing for a future release. In the meantime, if you don't want to rework your code to avoid the issue, then you could consider switching from Redlib to NewlibNano.

Anyway, thank you for reporting this issue.

Regards,

MCUXpresso IDE Support

View solution in original post

4 Replies
1,477 Views
lpcxpresso_supp
NXP Employee
NXP Employee

You are correct, you should be able to pass NULL buffer into the snprintf function when specifying a total of 0 bytes to write into the buffer (in order to work out how big a buffer is actually needed).

But with the current version of Redlib supplied with MCUXpresso IDE v10.2 (and earlier), this is not implemented and is causing a hard fault. We'll investigate fixing for a future release. In the meantime, if you don't want to rework your code to avoid the issue, then you could consider switching from Redlib to NewlibNano.

Anyway, thank you for reporting this issue.

Regards,

MCUXpresso IDE Support

1,476 Views
jaaphollander
Contributor II

I managed to workaround the issue by wrapping the vsnprintf symbol.

I've included my workaround below, to help out anyone else that may have encounted the issue.

#ifdef __REDLIB__
#include <stdarg.h>
#include <stddef.h>

extern int __real_vsnprintf(char *buff, size_t limit, const char *fmt, va_list args);
 

//Fix Redlib unable to deal with buffers of size 0
int __wrap_vsnprintf(char *buff, size_t limit, const char *fmt, va_list args)
{
  int ret;
  char tmp[1];

  if(limit == 0)
  {
    ret = __real_vsnprintf(tmp, 1, fmt, args);
  }
  else
  {
    ret = __real_vsnprintf(buff, limit, fmt, args);
  }

  return ret;
}
#endif //__REDLIB__
0 Kudos
1,476 Views
jaaphollander
Contributor II

But NULL is a valid buffer of atleast 0 characters, right?

0 Kudos
1,476 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Jaap Hollander ,

In the function define :

int snprintf ( char * s, size_t n, const char * format, ... )

Parameters “s: Pointer to a buffer where the resulting C-string is stored.
 The buffer should have a size of at least n characters.
 While on your code ,it is NULL. Please have a look at introduction about snprintf() function :
http://www.cplusplus.com/reference/cstdio/snprintf/   
pastedImage_12.png

Hope it helps,



Have a great day,
TIC

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos