does snprintf guarantee null termination?


 
Thread Tools Search this Thread
Top Forums Programming does snprintf guarantee null termination?
# 1  
Old 05-15-2008
does snprintf guarantee null termination?

Hi All,

I was reading the man page of snprintf function and it saids that snprintf adds a null terminator at the end of the string, but I remember once someone told me that snprintf doesn't guarantee the insertion of a null terminator character.

What do you think? Does anyone have experience on it??

Thanks!!
# 2  
Old 05-15-2008
According to POSIX.1-2001:

"The snprintf() function shall be equivalent to sprintf(), with the addition of the n argument which states the size of the buffer referred to by s. If n is zero, nothing shall be written and s may be a null pointer. Otherwise, output bytes beyond the n-1st shall be discarded instead of being written to the array, and a null byte is written at the end of the bytes actually written into the array."
# 3  
Old 05-16-2008
What the OP is on about:

from the same C-99 standard (what POSIX uses)
Quote:
The snprintf function returns the number of characters that would have been written
had n been sufficiently large, not counting the terminating null character, or a neg ative
value if an encoding error occurred. Thus, the null-terminated output has been
completely written if and only if the returned value is nonnegative and less than n.
So you have to check the return code of snprintf to know if it worked correctly.
You should check return codes no matter how stupid it seems. If you use gcc then
Code:
gcc -Wall <filename.c>

or use lint on your code. The only acceptable compile is a completely clean one.
# 4  
Old 05-16-2008
Thanks!

Hi,

I also was reading that in another not UNIX operating systems (i.e. window$) sprintf doesn't guarantee the insertion of a null terminator, hence my confusion!


Thank you very much!
# 5  
Old 05-16-2008
I would rely on the manpage of snprintf() on your system instead of on a certain someone. You can write a small code snippet to verify if snprintf() provides that functionality.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

A question about scsi termination

http://ep.yimg.com/ay/iercomputer/ibm-39j5022-ultra320-scsi-adapter-dual-channel-pci-x-fc5736-3.gif I have bought this controller. Simple and fast question: I will put on this controller a external LTO tape,which is terminated with a terminator. I have to put another terminator on PCI-controller... (1 Reply)
Discussion started by: Linusolaradm1
1 Replies

2. Shell Programming and Scripting

Problem with ssh termination...

hi all, i have a situation where i run ssh command from a unix machine to execute few scripts on 2 other unix machines. my problem is, the scripts that i run will start few commands on the 2 servers and will quit....i am able to quit from the script but i have to give ctrl+c (on the... (10 Replies)
Discussion started by: niteesh_!7
10 Replies

3. Shell Programming and Scripting

guarantee to start before restart...

Hi All, is there a way or script that i can check my AIX 5.3 OS will restart before i made restart? is there a script that can check all the startup files are ok before restarting. it is because i was stuck last time when i restart my PC because some startup files were missing:o. (2 Replies)
Discussion started by: malcomex999
2 Replies

4. Shell Programming and Scripting

search sprintf and replace with snprintf

Hi, anyone knows the perl search-and-replace expression for strcpy (char * destination, const char * source ); to strncpy ( char * destination, const char * source, size_t num ); ? the first and second arguments are the same (destination and source), the difference being that strncpy... (1 Reply)
Discussion started by: ChaMeN
1 Replies

5. Shell Programming and Scripting

Perl script to search sprintf and replace with snprintf

Dear all, I am new to perl script and would need some help for my 1st script. I wrote a script to search sprintf(buf,"%s", sourcestring) and replace with snprintf(buf, sizeof(buf),"%s", sourcestring). As snprintf() requires an extra argument, so it is not a simple search-and-replace. I need to... (1 Reply)
Discussion started by: ChaMeN
1 Replies

6. Shell Programming and Scripting

random script termination

I'm writing a script to archive data. First, the files are all rsync'd to the archive directory via NFS mounts(I know not the most efficient, but the only choice in this situation), then I use md5sum to validate the transfers. During execution of the script, it will exit for no apparent reason. It... (6 Replies)
Discussion started by: mph
6 Replies

7. Solaris

problem with sprintf snprintf in Solaris

******************************** Following is not the real issue. The issue is with popen. Plz continue forward with the thread to get a better picture. ******************************** Hi, I am working on a customised ftp application. In it we have used sprintf to store a UNIX command... (7 Replies)
Discussion started by: diganta
7 Replies
Login or Register to Ask a Question