Skip to main content
JBonn
Associate III
September 6, 2022
Question

Is there a way to prevent sprintf from writing a terminating nul past the end of the destination

  • September 6, 2022
  • 11 replies
  • 6953 views

..

This topic has been closed for replies.

11 replies

Piranha
Principal III
September 6, 2022

Not only terminating null character, but it can write anything past the end. It's strongly recommended to not use it at all. Instead snprintf() must be used.

JBonn
JBonnAuthor
Associate III
September 7, 2022
Piranha<>, WOW! Thanks! Did not know snprintf existed. Also did not know that printf was so difficult to deal with. Jerry Bonner
Tesla DeLorean
Guru
September 6, 2022

No,

But sprintf() does return the length of the string added, but whatever buffering you have needs to be able to accommodate the C string properly.

The length would allow you to advance the pointer to the NUL, or memcpy() to a spot you can't tolerate the NUL

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JBonn
JBonnAuthor
Associate III
September 7, 2022
Tesla,
Thanks for your answer.
Please help me get around this problem.
Here is my current code.
char char One_LCD[1], Two_LCD[], Three_LCD[];
uint8_t Gain; // Gain is always a single digit value
sprintf (One_LCD,"%1d",Gain); // I think you told me that this pollutes Two_LCD
OLED_countDraw (1,1,66,One_LCD);
OLED_countDraw is a program I wrote for STM32L4 chips to draw characters on an OLED graphic display
The above code will draw a 6 by 8 character on line 1 of 4 lines starting at the 66th place on 128 place line.
It is my way of placing characters at specified places on a graphic display.
How do I keep the sprint code above from polluting the Two_LCD space?
Would the following code display the correct Gain and not pollute Three_LCD?
sprintf (Two_LCD,"%1d",Gain);
OLED_countDraw (1, 1, 66, Two_LCD);
Jerry Bonner
Tesla DeLorean
Guru
September 7, 2022

Why can't you make these strings wide enough to carry the data?

You don't show the prototype for OLED_countDraw, if it takes a char, pass a char

char char String[4];

sprintf (String,"%1d",Gain);

OLED_countDraw (1,1,66,String[0]); // First character

OLED_countDraw (1,1,66,String[1]); // Second character

If it expects the string, ie char *

OLED_countDraw (1,1,66,String); // Pass as string

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JBonn
JBonnAuthor
Associate III
September 7, 2022

Tesla, I do not understand your reaction. I said "WOW" because i liked your response and followed that with a "Thanks". I apologize to you if if you think I responded to your answer in a negative way. I meant my response to be a "Thank You".

Piranha
Principal III
September 7, 2022

That was meant to me... ;)

JBonn
JBonnAuthor
Associate III
September 7, 2022

Thanks to all the answers. Am looking at all of them. There is evidently more than one way to solve my problem. I am investigating to determine which approach to take. Thanks again to all who answered, including you, Tesla.