Thread: secure use of scanf

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    secure use of scanf

    I've heard how scanf can pose security risks.
    What would be the correct and safe way to use it, so that those risks would be avoided?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It is generally a better idea to use fgets + sscanf. But one place to be especially careful is with string input -- always specify a maximum length. I believe there are a few other general "avoid" directives, but I can't think of them at the moment. And make sure that you've got a compiler or linter that can help you not screw up directives with their corresponding arguments.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Not use it at all would be my suggestion.

    The only truly safe operation is %c, which you may as well use fgetc()

    %s can limit the length with say "%10s", but there's no easy way to relate that to the size of the buffer (like you can with fgets). In addition, the 10 represents the number of characters to be stored (not the length of the buffer).

    All the numeric conversions fail to detect numeric overflow or underflow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    I'm interested in %d and %f.

    Using fgets and sscanf seems a little tedious.
    Can anyone give an example of how would I read a number from stdin securely (with use of scanf, fgets, sscanf or any other function) ?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this or this, maybe.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Using fgets and sscanf seems a little tedious.
    this would be the simplest that u can use to get an integer value

    Code:
    fgets(str, sizeof str, stdin)
    
    sscanf(str,,"%d",&num)'
    but Daves solution would be best for the this.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM