Products > Programming

GCC: How to get a vsprintf from sprintf?

(1/5) > >>

peter-h:
I have a "printf" library which I have successfully used to replace the source-less ST Cube supplied stuff (which uses the heap, etc) but upon analysing .map file modules I have discovered that I have 1 place where a vsprintf is being called so I need to create that.

A bit of context is here
https://www.eevblog.com/forum/programming/best-thread-safe-printf-and-why-does-printf-need-the-heap-for-f-etc/

and the library I have installed is
https://github.com/MaJerle/lwprintf
which unfortunately doesn't have it.

It also doesn't have

svfprintf
svfiprintf
fiprintf
vfiprintf

but nothing in my project is calling any of those AFAICT, and I struggle to find out what exactly what most of them do. However vsprintf is being used.

EDIT: I found I do have vsnprintf but not vsprintf. I know that in embedded work one should never use the latter. So maybe this is a better way to go to get vsprintf.

SiliconWizard:

--- Quote from: peter-h on July 29, 2022, 11:23:43 am ---EDIT: I found I do have vsnprintf but not vsprintf. I know that in embedded work one should never use the latter. So maybe this is a better way to go to get vsprintf.

--- End quote ---

Not just in "embedded work". One should avoid sprintf() as well, and any function that's supposed to fill a buffer and that doesn't take the max size as a parameter in general.

Then I'm not sure what you mean by "So maybe this is a better way to go to get vsprintf". Use vsnprintf () instead (and make sure to pass the right max size depending on context). That will require refactoring, but it's definitely eons better.

peter-h:
Sure; the thing is I am putting together something which will be programmed by others, and they may want it.

TheCalligrapher:

--- Quote from: peter-h on July 29, 2022, 11:23:43 am ---EDIT: I found I do have vsnprintf but not vsprintf. I know that in embedded work one should never use the latter. So maybe this is a better way to go to get vsprintf.

--- End quote ---

What is so special about embedded to claim that "one should never use the latter" in "embedded work" specifically?

If you hold such a radical stance towards `vsprintf`, it should probably apply to any kind of work, shouldn't it?

peter-h:
I would think this can be done with a macro but googling didn't come up with one.

vsprintf from vsnprintf. You have to work out the value for the parameter; not obvious.

But vsprintf from sprintf is just a substitution of the parameters, no?

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version