Validating Data in C programming

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gurdz

    #1

    Validating Data in C programming

    Does anyone know how to perform data validation in C? I have searched
    google for every possible result, and I either end up with data
    validation for C++ or nothing at all. I have also searched websites
    such as planet-source-code.com, but my search has been in vain.

    Can someone please help me out. Or at least, point me in the right
    direction?

    Thank you very much. :)

  • MQ

    #2
    Re: Validating Data in C programming


    gurdz wrote:
    Does anyone know how to perform data validation in C? I have searched
    google for every possible result, and I either end up with data
    validation for C++ or nothing at all. I have also searched websites
    such as planet-source-code.com, but my search has been in vain.
    >
    Can someone please help me out. Or at least, point me in the right
    direction?
    >
    Thank you very much. :)
    Data validation is a generic programming technique that is language
    independant. It is just the process of checking that data input to
    your program is within the range of expected values, etc. If you are
    expecting a number between 1 and 10, check that it is a number between
    1 and 10 before using it. What is so hard about that? Unless I am not
    understanding your question...

    Comment

    • Flash Gordon

      #3
      Re: Validating Data in C programming

      gurdz wrote:
      Does anyone know how to perform data validation in C?
      Yes, you look at the data and see if it matches your validation rules.
      I have searched
      google for every possible result, and I either end up with data
      validation for C++ or nothing at all. I have also searched websites
      such as planet-source-code.com, but my search has been in vain.
      >
      Can someone please help me out. Or at least, point me in the right
      direction?
      It really depends on what the data you are trying to validate is, what
      the rules are, and where the data is coming from. There is no one size
      fits all solution.
      --
      Flash Gordon
      Still sigless on this computer.

      Comment

      • Richard Heathfield

        #4
        Re: Validating Data in C programming

        Flash Gordon said:

        <snip>
        >
        It really depends on what the data you are trying to validate is, what
        the rules are, and where the data is coming from. There is no one size
        fits all solution.
        Yes, there is.

        The following fragment is taken from the source code used by a major UK
        bank, the identity of which will remain anonymous (to protect the guilty).
        It was spotted during Y2K work in the late 1990s:

        if(d != 6)
        {
        d = 6;
        }

        Viola! All numbers are 6, even if they aren't. QED.

        How we laughed! We laughed and laughed and laughed, until we stopped.

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • lovecreatesbeauty

          #5
          Re: Validating Data in C programming


          Richard Heathfield wrote:
          The following fragment is taken from the source code used by a major UK
          bank, the identity of which will remain anonymous (to protect the guilty).
          It was spotted during Y2K work in the late 1990s:
          >
          if(d != 6)
          {
          d = 6;
          }
          >
          Viola! All numbers are 6, even if they aren't. QED.
          >
          How we laughed! We laughed and laughed and laughed, until we stopped.
          It is possible that there are occasions for using it.

          Comment

          • MQ

            #6
            Re: Validating Data in C programming


            Richard Heathfield wrote:
            The following fragment is taken from the source code used by a major UK
            bank, the identity of which will remain anonymous (to protect the guilty).
            It was spotted during Y2K work in the late 1990s:
            >
            if(d != 6)
            {
            d = 6;
            }
            >
            I don't think that it is any secret that banks have their own brand of
            logic, that seems to err in their favour

            Comment

            • Andrew Poelstra

              #7
              Re: Validating Data in C programming

              On 2006-08-05, lovecreatesbeau ty <lovecreatesbea uty@gmail.comwr ote:
              >
              Richard Heathfield wrote:
              >The following fragment is taken from the source code used by a major UK
              >bank, the identity of which will remain anonymous (to protect the guilty).
              >It was spotted during Y2K work in the late 1990s:
              >>
              >if(d != 6)
              >{
              > d = 6;
              >}
              >>
              >Viola! All numbers are 6, even if they aren't. QED.
              >>
              >How we laughed! We laughed and laughed and laughed, until we stopped.
              >
              It is possible that there are occasions for using it.
              >
              ....where typing `d = 6' or even just `6' wouldn't suffice?

              --
              Andrew Poelstra <http://www.wpsoftware. net/projects>
              To reach me by email, use `apoelstra' at the above domain.
              "Do BOTH ends of the cable need to be plugged in?" -Anon.

              Comment

              • Malcolm

                #8
                Re: Validating Data in C programming

                "Andrew Poelstra" <apoelstra@fals e.sitewrote
                On 2006-08-05, lovecreatesbeau ty <lovecreatesbea uty@gmail.comwr ote:
                >>
                >Richard Heathfield wrote:
                >>The following fragment is taken from the source code used by a major UK
                >>bank, the identity of which will remain anonymous (to protect the
                >>guilty).
                >>It was spotted during Y2K work in the late 1990s:
                >>>
                >>if(d != 6)
                >>{
                >> d = 6;
                >>}
                >>>
                >>Viola! All numbers are 6, even if they aren't. QED.
                >>>
                >>How we laughed! We laughed and laughed and laughed, until we stopped.
                >>
                >It is possible that there are occasions for using it.
                >>
                >
                ...where typing `d = 6' or even just `6' wouldn't suffice?
                >
                if(d != 6)
                {
                fprintf(stderr, "somethign fishy here\n");
                d = 6;
                }

                if(d != 6)
                {
                // fprintf(stderr, "somethign fishy here\n");
                d = 6;
                }

                Three months later, another compiler
                Warning, non-standard comment line 3456

                if((d != 6)
                {
                d = 6;
                }

                --

                freeware games to download.



                Comment

                • Al Balmer

                  #9
                  Re: Validating Data in C programming

                  On Sat, 05 Aug 2006 10:18:58 +0000, Richard Heathfield
                  <invalid@invali d.invalidwrote:
                  >Flash Gordon said:
                  >
                  ><snip>
                  >>
                  >It really depends on what the data you are trying to validate is, what
                  >the rules are, and where the data is coming from. There is no one size
                  >fits all solution.
                  >
                  >Yes, there is.
                  >
                  >The following fragment is taken from the source code used by a major UK
                  >bank, the identity of which will remain anonymous (to protect the guilty).
                  >It was spotted during Y2K work in the late 1990s:
                  >
                  >if(d != 6)
                  >{
                  d = 6;
                  >}
                  >
                  >Viola! All numbers are 6, even if they aren't. QED.
                  >
                  >How we laughed! We laughed and laughed and laughed, until we stopped.
                  <gLooks like a left-over debug statement. A place to set a
                  breakpoint.

                  --
                  Al Balmer
                  Sun City, AZ

                  Comment

                  • Eric Sosman

                    #10
                    Re: Validating Data in C programming

                    Andrew Poelstra wrote:
                    On 2006-08-05, lovecreatesbeau ty <lovecreatesbea uty@gmail.comwr ote:
                    >
                    >>Richard Heathfield wrote:
                    >>
                    >>>The following fragment is taken from the source code used by a major UK
                    >>>bank, the identity of which will remain anonymous (to protect the guilty).
                    >>>It was spotted during Y2K work in the late 1990s:
                    >>>
                    >>>if(d != 6)
                    >>>{
                    >> d = 6;
                    >>>}
                    >>>
                    >>>Viola! All numbers are 6, even if they aren't. QED.
                    >>>
                    >>>How we laughed! We laughed and laughed and laughed, until we stopped.
                    >>
                    >>It is possible that there are occasions for using it.
                    >>
                    >
                    >
                    ...where typing `d = 6' or even just `6' wouldn't suffice?
                    It's an optimization. On a virtual memory system, it avoids
                    setting the "dirty" bit for a memory page unnecessarily, thereby
                    avoiding the (potential) expense of a page-out operation later,
                    or at least not incurring the expense for no good purpose.

                    (Hmmm: That variable name, `d'. Was the author, by any
                    chance, named Reginald?)

                    --
                    Eric Sosman
                    esosman@acm-dot-org.invalid

                    Comment

                    • Richard Heathfield

                      #11
                      Re: Validating Data in C programming

                      Eric Sosman said:

                      <snip>
                      >
                      It's an optimization. On a virtual memory system, it avoids
                      setting the "dirty" bit for a memory page unnecessarily, thereby
                      avoiding the (potential) expense of a page-out operation later,
                      or at least not incurring the expense for no good purpose.
                      >
                      (Hmmm: That variable name, `d'. Was the author, by any
                      chance, named Reginald?)
                      I don't think it would be fair to reveal the source of the source, so to
                      speak.

                      Your optimisation explanation is ingenious, but my recollection of the code
                      base leaves me somewhat unconvinced that it is a correct explanation of the
                      motivation for this particular line.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at above domain (but drop the www, obviously)

                      Comment

                      • Eric Sosman

                        #12
                        Re: Validating Data in C programming

                        Richard Heathfield wrote:
                        Eric Sosman said:
                        >
                        <snip>
                        >
                        > It's an optimization. On a virtual memory system, it avoids
                        >>setting the "dirty" bit for a memory page unnecessarily, thereby
                        >>avoiding the (potential) expense of a page-out operation later,
                        >>or at least not incurring the expense for no good purpose.
                        >>
                        > (Hmmm: That variable name, `d'. Was the author, by any
                        >>chance, named Reginald?)
                        >
                        >
                        I don't think it would be fair to reveal the source of the source, so to
                        speak.
                        >
                        Your optimisation explanation is ingenious, but my recollection of the code
                        base leaves me somewhat unconvinced that it is a correct explanation of the
                        motivation for this particular line.
                        Must have been good old Reginald K. Denktash, then.

                        --
                        Eric Sosman
                        esosman@acm-dot-org.invalid

                        Comment

                        • gurdz

                          #13
                          Re: Validating Data in C programming

                          Well, I'm sorry if I got each and every person confused about data
                          validation, but actually, this question was raised because of my Data
                          Structures and Algorithms lecturer. She didn't teach us nuts about
                          validation, and she comes to class saying that we all have to do "data
                          validation for input".

                          I hope you all don't mind giving me some advice on this. I usually
                          don't ask people for this sort of help for assignments, as some see it
                          as unfair, however, I am totally lost on this subject.

                          Thanks once again,
                          kind regards,
                          Gurdip

                          Comment

                          • gurdz

                            #14
                            Re: Validating Data in C programming

                            Well, I'm sorry if I got each and every person confused about data
                            validation, but actually, this question was raised because of my Data
                            Structures and Algorithms lecturer. She didn't teach us nuts about
                            validation, and she comes to class saying that we all have to do "data
                            validation for input".

                            I hope you all don't mind giving me some advice on this. I usually
                            don't ask people for this sort of help for assignments, as some see it
                            as unfair, however, I am totally lost on this subject.

                            Thanks once again,
                            kind regards,
                            Gurdip

                            Comment

                            • Gordon Burditt

                              #15
                              Re: Validating Data in C programming

                              >Well, I'm sorry if I got each and every person confused about data
                              >validation, but actually, this question was raised because of my Data
                              >Structures and Algorithms lecturer. She didn't teach us nuts about
                              >validation, and she comes to class saying that we all have to do "data
                              >validation for input".
                              Consider, for example, an input of a bet amount for a roulette game.
                              Chips cost $1 each.

                              If the bet is a valid number (e.g. contains no alphabetic characters),
                              and the bet is greater than zero and the bet is less than or equal
                              to the house limit and the bet is an integer and the bet is less
                              than or equal to the player's bank balance, then the bet amount is
                              valid.

                              Reminder: a great way to cheat at poorly-written gambling programs
                              is to bet a negative amount, using the house advantage against it,
                              then try to "lose".

                              Consider, for example, an input which is a telephone number. How
                              do you validate it?

                              Hint: isdigit(buffer[0]) returns 0 if the first character in buffer[]
                              is not a digit, and nonzero if it is.

                              Try 1: A valid telephone number consists only of digits.
                              User inputs: 1-800-555-1212
                              Try 2: A valid telephone number consists only of digits after removing
                              the characters -, (, ), and blanks.
                              User inputs: 1-800-555
                              Try 3: A valid telephone number consists of 10 digits after removing
                              the characters -, (, ), and blanks.
                              User inputs: 18005551212
                              Try 4: A valid telephone number consists of 10 digits after removing
                              the characters -, (, ), and blanks. If the input is 11 digits and
                              the first digit is a 1, remove it.
                              User inputs +44 1277 655112
                              Try 5: A valid USA telephone number consists of 10 digits after removing
                              the characters -, (, ), blanks, and the prefix 1 or 011, if present.
                              We don't care about the rest of the world.
                              User inputs: 18195551212
                              Try 6: A valid USA telephone number consists of 10 digits after removing
                              the characters -, (, ), blanks, and the prefix 1 or 011, if present.
                              Reject input whose first 3 digits after substitutions is a Canadian
                              or Mexican area code. Note that this code requires modification when a
                              new non-USA area code is assigned. We don't care about the rest of the world.
                              User inputs: 12145551212 Extension 234
                              <Programmer shoots User>


                              Comment

                              Working...