Like Tree4Likes

Validation design

This is a discussion on Validation design within the C Programming forums, part of the General Programming Boards category; Originally Posted by Salem However, it seems to be almost completely useless. It only seems to generate ERANGE if the ...

  1. #16
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Salem View Post
    However, it seems to be almost completely useless.

    It only seems to generate ERANGE if the number input exceeds the limits of long long int, regardless of the width of the datatype being converted at the moment.
    Interesting. When I run your code I get:
    Code:
    $ ./foo
    2147483647
    9223372036854775807
    Enter number: 2147483648
    Number out of range, try again: Numerical result out of range
    Enter number: 9223372036854775807
    Number out of range, try again: Numerical result out of range
    $ /lib/i386-linux-gnu/libc.so.6
    GNU C Library (Ubuntu EGLIBC 2.13-20ubuntu5.2) stable release version 2.13, by Roland McGrath et al.
    Quote Originally Posted by Salem View Post
    If you have to read your data into the longest data type, then manually check the range to see if will fit in a smaller int, then you may as well use strtol() from the outset. It's the same PITA either way.
    My first thought was using strtol() but because it's returning a long I took another look at the sscanf() man page to check if it does any error reporting and that's how I found out about the ERANGE.

    Bye, Andreas

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Salem likes this.

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Ah, I wonder if it's down to machine word size.
    Code:
    $ /lib/x86_64-linux-gnu/libc.so.6
    GNU C Library (Ubuntu EGLIBC 2.13-20ubuntu5.2) stable release version 2.13, by Roland McGrath et al.
    Here is something out of the glibc vfscanf,c file
    Code:
    	  /* Convert the number.  */
    	  ADDW (L_('\0'));
    	  if (need_longlong && (flags & LONGDBL))
    	    {
    	      if (flags & NUMBER_SIGNED)
    		num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
    	      else
    		num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
    	    }
    	  else
    	    {
    	      if (flags & NUMBER_SIGNED)
    		num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
    	      else
    		num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
    	    }
    It seems to use the widest available strto... function to perform the initial conversion.

    What follows next are just a bunch of dumb casts to throw away the msb data, ignoring the actual data type ranges.
    Things like this:
    Code:
    		    *ARG (short int *) = (short int) num.l;
    AndiPersti likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Quote Originally Posted by AndiPersti View Post
    Mmm, "Status: RESOLVED INVALID", from 2008
    In other words, not a bug, and nothing to fix.

    It might just squeak in within the letter of the law, but it's certainly unhelpful with respect to the intent of the manual page enhancement.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validation
    By ulti-killer in forum C Programming
    Replies: 5
    Last Post: 11-15-2012, 12:16 PM
  2. validation in c
    By thangarasu1988 in forum C Programming
    Replies: 4
    Last Post: 01-05-2012, 01:42 PM
  3. validation
    By Ideswa in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2006, 12:01 PM
  4. need help in validation
    By dholman in forum C Programming
    Replies: 2
    Last Post: 01-08-2004, 08:27 AM
  5. Help with validation
    By boontune in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2003, 08:44 AM

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