Skip to main content
Trevor Jones
Senior
January 16, 2020
Question

sprintf causes hard fault.

  • January 16, 2020
  • 21 replies
  • 5488 views

I use "sprintf (string,"comment"); everywhere

now for some reason, I am getting a hardfault. in an innocuous print.

length += sprintf(String + length, "Received Packet from ");

length is only 15.

String is declared as

char String[256];

I guess it is using malloc,

How do we check the allocations are being cleared ?

I never use malloc anywhere, so its not me...

    This topic has been closed for replies.

    21 replies

    Trevor Jones
    Senior
    January 16, 2020

    Thanks for your support,

    this code starts from the cube and managed in Visual Studio.

    not sure about how it comes together and why they do it like that, but generally very good results. used the cube/VS on F0 F7 H7 on many boards.

    I pound sprintf usually, without issue. to CDC and U1 for debug

    I have commented out that section and still sprintf works everywhere else..

    it just seems a little flaky to me. the code is not on a boundary length is well inside the boundary, not sure what is falling over.

    other than this issue, all the ST processors seem very stable.

    anyhow, commented it out... everything else is working well.

    Do you think I should be implementing a _sbrk ? function how does malloc unload ?

    berendi
    Principal
    January 17, 2020

    Keep in mind that malloc is broken or FatFs is misconfigured. You can expect arbitrary regions of memory or hardware registers changed anytime. Just remember it the next time you are facing problems with harmlessly looking code.

    If there is no _sbrk, programs using newlib malloc explicitly or implicitly (throgh sprintf) could not be compiled at all. Either there is _sbrk() hidden somewhere, or it isn't newlib.

    Copying simple strings with sprintf() is a serious waste of cycles, you might want to replace it with strcpy() or strcat()

    #include <string.h>
     
     char String[256];
     strcpy(String, "Received Packet from ");
     if (have_LCD)
     strcat(String, "LCD unit ");
     if(have_Facex)
     strcat(String, "Facex unit ");

    this will of course not eliminate the problem, just postpone it.

    Trevor Jones
    Senior
    January 17, 2020

    Yes, I can see that  serious waste of cycles,

    I made my own string copy today, its a trivial exercise without boundaries just looking for a null, but its always there.

    I should add a limit to that.

    In Visual Studio, when you make the project, it asks if you want to include the newlib,

    I select the newlib with floating point support in printf. probably pre-compiled and why I cannot step into sprintf.

    Yes, the issue re-appeared again today in anther innocuous sprintf... hence I made a new one...

    nprintf... while (*String) copy bytes; then add the null

    but the original question still applies, how can we check the malloc allocation level and if it has a memory leak.

    generally, I dont use FatFS at all in a day.

    I only use it when an SDCard is installed and I use @Community member​'s  flash burning and reading code for the external QSPI which runs without FatFS

    that seems very stable, but once the images are saved in Flash from the SDCard,

    FatFS remains unused for the next month.. (although it is initialised at startup.)

    so I don't think its FatFS.

    Trevor Jones
    Senior
    January 17, 2020

    will check for VisualStudio's malloc implementation,

    would like to work a function to check stack/heap levels / leaks and Freeing the same memory multiple times.