Thread: sprintf

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    sprintf

    Hi all...

    Just a quick question; What are the key issues with using sprintf; I'm guessing that it is array overflow? ...Just doing a little bit of revision for a job interview I have later this afternoon [for a graduate role] thanks!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yes, the main problem will always be printing a string into a char array or malloc'ed memory which can't hold the entire string you're printing.
    Better would be to use snprintf, where you have to specify the maximum number of characters to print to the string so you won't overflow.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Buffer overflow would be the primary problem. Some libraries support snprintf() or vsnprintf() as alternatives.

    And of course, it's not just strings that could cause a problem, consider:
    Code:
    float f;
    char str[15];  // Big enough for 8.4... (not!)
    
    f = 100000.0;
    
    f *= 1000.0;
    
    sprintf(str, "%8.4f\n", f);  // This will overflow...
    --
    Mats

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Quote Originally Posted by matsp View Post
    Buffer overflow would be the primary problem. Some libraries support snprintf() or vsnprintf() as alternatives.

    And of course, it's not just strings that could cause a problem, consider:
    Code:
    float f;
    char str[15];  // Big enough for 8.4... (not!)
    
    f = 100000.0;
    
    f *= 1000.0;
    
    sprintf(str, "%8.4f\n", f);  // This will overflow...
    --
    Mats
    Would that not only overflow if the array was set to less that 12 chars in length? ...I'm assuming with str set to a length of 15 it would not overflow considering you are wanting to print:

    XXXXXXXX.XXXX which is less than 15 chars in length! ...Or am I not thinking about this correct?
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobthebullet990 View Post
    Would that not only overflow if the array was set to less that 12 chars in length? ...I'm assuming with str set to a length of 15 it would not overflow??
    "12345678.1234" is 13 characters with the dot, 14 with the '\0' which is ALWAYS added.

    But you're right, it would fit (only just) because I multiply with 1000 - if it was -1000 or 10000 it would not fit.

    It's too early in the morning still... ;-)

    --
    Mats

  6. #6
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Quote Originally Posted by matsp View Post
    "12345678.1234" is 13 characters with the dot, 14 with the '\0' which is ALWAYS added.

    But you're right, it would fit (only just) because I multiply with 1000 - if it was -1000 or 10000 it would not fit.

    It's too early in the morning still... ;-)

    --
    Mats
    Ah yes! ...I forgot about the \0 at the end! well pointed out there!!!! ...I was taking the . into consideration there - so the length would have been 13 + the '\0' char!

    Thankyou for putting it into a practical form of thinking! helped a lot! ...Now If a question about overflow comes up regarding strings; as one subject they said to read up on was strings, I shud be fine!!!!! LOL!

    Thanks again!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  7. #7
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Ah yes! ...Is there any advantages to using sprintf? ...other than its super easy to use!!!!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobthebullet990 View Post
    Ah yes! ...Is there any advantages to using sprintf? ...other than its super easy to use!!!!
    Well, the CLEAR advantage is of course that it already exists and is easy to use. If the library that comes with the compiler, or someone makes up a snprintf, it's only one more argument, and it's now safe, which is better.

    However, writing your own snprintf may not be trivial (unless there's a vnsprintf of course, in which case it's a simple case of three or so lines, not counting blank lines piossibly added for readability).

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM