Hi,
this might be a strange question, but why is snprintf not guaranteed to nul
terminate a string?
i thought the whole point of those sn functions was to prevent these errors.
sure, they prevent buffer overwrites, but callers can still screw up bad if
they don't always make sure for themselves that the 0 is there before they
let someone else read the contents.
now you still have the problem that you have to make that check everywhere,
instead of not having to bother.
kind regards,
Bruno. 2 1625
"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP09.phx.gbl... this might be a strange question, but why is snprintf not guaranteed to nul terminate a string?
History, mostly.
As I see it, there are two ways to go when the string being written exceeds
the space avaialble to it.
1) truncate and null terminate
2) just truncate
C has always had the mindset that the programmer knows what he is doing or
should know. So, it chooses option 2.
i thought the whole point of those sn functions was to prevent these errors.
You may be thinking of the new "safe" string functions http://msdn.microsoft.com/library/de...re03102004.asp
sure, they prevent buffer overwrites, but callers can still screw up bad if they don't always make sure for themselves that the 0 is there before they let someone else read the contents.
Yup. The docs do mention in their security note that
snprintf(s, ...)
should be followed by
s[n - 1] = 0;
where n = the number of characters in s.
now you still have the problem that you have to make that check everywhere, instead of not having to bother.
Yup. Sadly, such is life. Since C "strings" are just arrays, and since
"strings" are passed by simple pointer, called functions have no
authoritative source for string length.
Regards,
Will
Bruno van Dooren wrote: Hi,
this might be a strange question, but why is snprintf not guaranteed to nul terminate a string? i thought the whole point of those sn functions was to prevent these errors.
sure, they prevent buffer overwrites, but callers can still screw up bad if they don't always make sure for themselves that the 0 is there before they let someone else read the contents.
now you still have the problem that you have to make that check everywhere, instead of not having to bother.
kind regards, Bruno.
Hi Bruno,
Take a look at the following link: http://msdn.microsoft.com/library/de...re03102004.asp
Ronald Laeremans
Visual C++ team This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chester |
last post by:
Hello all,
I am looking for an implementation of snprintf() that is targeted for
the embedded world, i.e. is optimized for memory size. I also don't
need to print anything larger than a float...
|
by: ajm |
last post by:
Hi,
I'm refactoring some HTTP client code I wrote a while back and in the
process I'm tightening up some of my string processing code (most of
which involves parsing, replacing, concatenating...
|
by: A. Farber |
last post by:
Hello,
I'm programming a web application and for that I'm trying
to extend the standard C string functions, so that they can
handle the application/x-www-form-urlencoded format
(which encodes...
|
by: dimka |
last post by:
Hello
Here is my situation: I have a char buf; that is used as a buffer
for a log output. Normally, I do:
rc = snprintf( buf, sizeof(buf), some_format, some_args );
This makes the...
|
by: matevzb |
last post by:
The C99 standard describes snprintf() in an awkward way, so it's hard
(for me) to assume what the result will be in some situations.
1. The "Description" section states:
"... Otherwise, output...
|
by: manjuks |
last post by:
Hi All,
I wants to know what is the difference between sprintf and snprintf?
Thanks,
Manjunath
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |