Is sprintf to unsafe to use?

This is a discussion on Is sprintf to unsafe to use? within the C Programming forums, part of the General Programming Boards category; And should the non-standard snprintf always be used?...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Is sprintf to unsafe to use?

    And should the non-standard snprintf always be used?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Is sprintf unsafe? Yes, it can be. It is if you don't supply a large enough buffer to hold the data you format.
    I always recommend the use of safer more non-portable functions due to security issues.
    But you should at least write a wrapper for the non-standard function and wrap it around the standard function to make it compile it compilers that does not support the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sprintf() is unsafe if you do not KNOW that the size of the input is safe. For example, if you are printing strings that you know from other places are no longer than 99 chars into a 120 char array, where the formatting itself adds no more than 20 characters then you are safe.

    The difficulty, of course, is to judge whether the format and input is within range or not. You may for example want to format a floating point value, and you expect it to fit in %6.6f, but for some reason the value is greater than 999999, so the resulting string is longer than the format expects [and bear in mind that negative numbers take up one extra space, so a negative, large enough, number would also produce the same type of problem].

    sprintf certainly has the ability to overflow.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Almost anything can be unsafe if you don't use it properly.

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, 05:33 PM
  2. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  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, 09:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21