LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2013, 01:06 PM   #1
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Rep: Reputation: 81
Will vprintf == vsprintf using the same parameters?


vprintf and vsprintf return the amount of characters written.

I know that you should not use vsprintf due to the risk of buffer overflow, but if vprintf is guaranteed to return the same amount of characters as vsprintf will write (using exactly the same parameters), then it should be safe, right? Like this:

Code:
int printed = vprintf(<params>);
char * buffer = malloc(sizeof(* buffer) * (printed + 1));
vsprintf(buffer, <params>); /* == printed and thus overflow is not possible? */
Assuming params are the same.

I suspect this might not always work since (v)printf writes to stdout while (v)sprintf writes to a buffer.
 
Old 09-16-2013, 01:25 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Use [v]asprintf instead.
 
Old 09-16-2013, 01:25 PM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
What's the point? Just use vsnprintf and you don't have to worry about any edge cases.

Quote:
Originally Posted by NevemTeve View Post
Use [v]asprintf instead.
It's worth remembering it's a GNU extension though.

Last edited by mina86; 09-16-2013 at 01:27 PM.
 
1 members found this post helpful.
Old 09-16-2013, 03:02 PM   #4
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Original Poster
Rep: Reputation: 81
Quote:
Originally Posted by mina86 View Post
What's the point? Just use vsnprintf and you don't have to worry about any edge cases.
It's written in C89.
 
Old 09-17-2013, 10:34 AM   #5
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Original Poster
Rep: Reputation: 81
I dusted off the standard and I think it settles it:

Quote:
The fprintf function returns the number of characters transmitted, or a negative value if an output error occured.
Quote:
The sprintf function is equivalent to fprintf, except that the argument s specifies an array into which the generated output is to be written, rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned sum.
Quote:
The sprintf function returns the number of characters written in the array, not counting the terminating null character.
"Transmitted" must mean the amount of characters that the program attempts to write to stdout, regardless if stdout successfully outputs all characters, as opposed to "written", which is used to describe sprintf.

(I should have checked this before asking but cppreference.com says "returns number of characters written" for both.)

Obviously not a good general solution for sprintf but in my case I have to write the same thing to stdout and a buffer, so this works perfectly for me. I think.
 
Old 09-18-2013, 12:49 PM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by Soderlund View Post
It's written in C89.
I would question if there is any valid reason for sticking to C89, but cannot comment without knowing the project of course.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vsprintf causing segmentation fault knobby67 Programming 5 01-08-2011 11:11 AM
vsprintf and va_list - string length? Ephracis Programming 2 02-21-2008 10:09 AM
Parameters nagrcm Programming 1 09-14-2007 11:19 AM
Kernel alternative for vsprintf bhuvanmital Linux - Software 3 02-26-2006 09:23 PM
using ld parameters artur Programming 4 02-13-2004 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration