snprintf hardfaulting on buffer of zero length

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

snprintf hardfaulting on buffer of zero length

ソリューションへジャンプ
1,775件の閲覧回数
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 解決策
1,531件の閲覧回数
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

元の投稿で解決策を見る

4 返答(返信)
1,532件の閲覧回数
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,531件の閲覧回数
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 件の賞賛
1,531件の閲覧回数
jaaphollander
Contributor II

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

0 件の賞賛
1,531件の閲覧回数
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 件の賞賛