Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sscanf correctly and safely

Tags:

c

scanf

First of all, other questions about usage of sscanf do not answer my question because the common answer is to not use sscanf at all and use fgets or getch instead, which is impossible in my case.

The problem is my C professor wants me to use scanf in a program. It's a requirement. However the program also must handle all the incorrect input.

The program must read an array of integers. It doesn't matter in what format the integers for the array are supplied. To make the task easier, the program might first read the size of the array and then the integers each in a new line.

The program must handle the inputs like these (and report errors appropriately):

  1. 999999999999999...9 (numbers larger than integer)
  2. 12a3 (don't read this as an integer 12)
  3. a...z (strings)
  4. 11 aa 22 33\n all in one line (this might be handled by discarding everything after 11)
  5. inputs larger than the input array

There might be more incorrect cases, these are the only few I could think of.

If the erroneous input is supplied, the program must ask the user to input again until the correct input is given, but the previous correct input must be kept (only incorrect input must be cleared from the input stream).

Everything must conform to C99 standard.

like image 629
evodevo Avatar asked Feb 26 '12 21:02

evodevo


People also ask

Is sscanf thread safe?

The string-based functions, such as sprintf() and sscanf() , do not depend on the stdio library. These functions are thread-safe.

Is sscanf unsafe?

The reason why sscanf might be considered bad is because it doesnt require you to specify maximum string width for string arguments, which could result in overflows if the input read from the source string is longer. so the precise answer is: it is safe if you specify widths properly in the format string otherwise not.

How fast is sscanf?

Using fscanf the speed would top out at around 100-150MB/s. Only after I've switched to fread the data was actually read at the maximum speed of the drive - over 400MB/s. I guess it's due to parsing fscan is doing.


2 Answers

The scanf family of function cannot be used safely, especially when dealing with integers. The first case you mentioned is particularly troublesome. The standard says this:

If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

Plain and simple. You might think of %5d tricks and such but you'll find they're not reliable. Or maybe someone will think of errno. The scanf functions aren't required to set errno.

Follow this fun little page: they end up ditching scanf altogether.


So go back to your C professor and ask them: how exactly does C99 mandate that sscanf will report errors ?

like image 142
cnicutar Avatar answered Sep 19 '22 05:09

cnicutar


Well, let sscanf accept all inputs as %s (i.e. strings) and then program analyze them

like image 24
mikithskegg Avatar answered Sep 22 '22 05:09

mikithskegg