Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getline - sort of

Reply
Thread Tools

getline - sort of

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-17-2008
Richard Heathfield <(E-Mail Removed)> writes:
> Keith Thompson said:
>
>> Richard Heathfield <(E-Mail Removed)> writes:
>>> CBFalconer said:
>>>
>>> <snip>
>>>
>>>> Don't use sscanf, it is not suitable for interactive work.
>>>
>>> Why not?

>>
>> For one thing, its behavior on numeric input overflow is undefined.

>
> But you can use field width specifiers to prevent this. See, for instance,
> the C89 example of fscanf (which is obviously analogous to sscanf):
>
> ++++++++++++++++++++++
> fscanf(stdin, "%2d%f%*d %[0123456789]", &i, &x, name);
>
> with input:
>
> 56789 0123 56a72
>
> will assign to i the value 56
> ++++++++++++++++++++++


True, but it's not a very flexible way to handle it. If INT_MAX is
32767, for example, then you can use "%4d" to safely limit input
values to 9999, but if you want to handle values from 10000 to 32767
you can't avoid UB if the user enters something from 32768 to 99999.

It would have been really nice if the standard had required sane
behavior.

Note that the example invokes UB if the input for "%f" overflows.

> Not that I'm rootin' for *scanf or anything. But of the *scanf family,
> sscanf is certainly the least useless, and it seems to me that it makes no
> odds whether the string on which it works comes from an "interactive"
> source or not.


Agreed, numeric overflow reading from a file isn't much less of a
problem than numeric overflow reading from a keyboard. You *might*
have better control over file input, but a robust program should
handle bad input even if you're sure it can't happen.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-17-2008
Keith Thompson said:

[sscanf]

> If INT_MAX is
> 32767, for example, then you can use "%4d" to safely limit input
> values to 9999, but if you want to handle values from 10000 to 32767
> you can't avoid UB if the user enters something from 32768 to 99999.


Yes you can, by capturing it as a long int. (But your argument scales to
long int, of course.)

> It would have been really nice if the standard had required sane
> behavior.


Aye. For example, <TYPE>_MIN, 0, or <TYPE>_MAX as appropriate in the
object, and the halting of the conversion process.

> Note that the example invokes UB if the input for "%f" overflows.


Typical. (They should have written %3f.)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
The Colourised Bewitched -- sort of OK....... sort of! anthony DVD Video 26 06-28-2005 04:39 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
What is faster? C++ vector sort or sort in database JerryJ C++ 11 04-28-2004 10:23 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments