Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AfxIsValidString does not seem to work.

AfxIsValidString does not seem to work.

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcgahanfl
    wrote on last edited by
    #1

    char szDescription[200]; BOOL bValid; bValid= AfxIsValidString(szDescription, 400); //bValid is TRUE, shouldn't it be FALSE?????????/ thanks

    D B T 3 Replies Last reply
    0
    • M mcgahanfl

      char szDescription[200]; BOOL bValid; bValid= AfxIsValidString(szDescription, 400); //bValid is TRUE, shouldn't it be FALSE?????????/ thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      McGahanFL wrote: ...shouldn't it be FALSE?????????/ What makes you think so? This function tests the memory address to ensure that it is contained entirely within the program’s memory space.


      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

      R 1 Reply Last reply
      0
      • D David Crow

        McGahanFL wrote: ...shouldn't it be FALSE?????????/ What makes you think so? This function tests the memory address to ensure that it is contained entirely within the program’s memory space.


        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        DavidCrow wrote: This function tests the memory address to ensure that it is contained entirely within the program’s memory space. I thought this was AfxIsValidAddress(), or maybe does AfsIsValidString call AfxIsValidAddress() ? Anyway, I can understand that defining a string of 200 chars, and getting true when checking if it is valid for 400 chars is a bit disturbing, especially since the MSDN states : Nonzero if the specified pointer points to a string of the specified size; otherwise 0. ~RaGE();

        D M 2 Replies Last reply
        0
        • R Rage

          DavidCrow wrote: This function tests the memory address to ensure that it is contained entirely within the program’s memory space. I thought this was AfxIsValidAddress(), or maybe does AfsIsValidString call AfxIsValidAddress() ? Anyway, I can understand that defining a string of 200 chars, and getting true when checking if it is valid for 400 chars is a bit disturbing, especially since the MSDN states : Nonzero if the specified pointer points to a string of the specified size; otherwise 0. ~RaGE();

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          I hate it when that happens. I was looking at AfxIsValidString() on my local MSDN, but then somehow started looking at AfxIsValidAddress() on MSDN online. Thanks.


          "One must learn from the bite of the fire to leave it alone." - Native American Proverb

          M 1 Reply Last reply
          0
          • D David Crow

            I hate it when that happens. I was looking at AfxIsValidString() on my local MSDN, but then somehow started looking at AfxIsValidAddress() on MSDN online. Thanks.


            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

            M Offline
            M Offline
            mcgahanfl
            wrote on last edited by
            #5

            Thank you for your effort.

            1 Reply Last reply
            0
            • R Rage

              DavidCrow wrote: This function tests the memory address to ensure that it is contained entirely within the program’s memory space. I thought this was AfxIsValidAddress(), or maybe does AfsIsValidString call AfxIsValidAddress() ? Anyway, I can understand that defining a string of 200 chars, and getting true when checking if it is valid for 400 chars is a bit disturbing, especially since the MSDN states : Nonzero if the specified pointer points to a string of the specified size; otherwise 0. ~RaGE();

              M Offline
              M Offline
              mcgahanfl
              wrote on last edited by
              #6

              Thank you for verifing what seems reasonable.

              1 Reply Last reply
              0
              • M mcgahanfl

                char szDescription[200]; BOOL bValid; bValid= AfxIsValidString(szDescription, 400); //bValid is TRUE, shouldn't it be FALSE?????????/ thanks

                B Offline
                B Offline
                Blake Miller
                wrote on last edited by
                #7

                Well, what is IN the szDescription, since you did not initialize it to anything? There might be at least one NUL byte within the 400 or so off the stack, in which case the 'length' of the string was less than or equal to 400 bytes. So the test passed, this time... I would not ever use AfxIsValidSring unless you had actually placed (or at least suspected one had been) a zero-terminated string into the variable first.

                M 1 Reply Last reply
                0
                • B Blake Miller

                  Well, what is IN the szDescription, since you did not initialize it to anything? There might be at least one NUL byte within the 400 or so off the stack, in which case the 'length' of the string was less than or equal to 400 bytes. So the test passed, this time... I would not ever use AfxIsValidSring unless you had actually placed (or at least suspected one had been) a zero-terminated string into the variable first.

                  M Offline
                  M Offline
                  mcgahanfl
                  wrote on last edited by
                  #8

                  Thank you for your reply. The -1 parm looks for NULL terminated, otherwise it is documnented that it verifies the length. In any event, there is no test set of data that will fail this test other than a NULL pointer.

                  1 Reply Last reply
                  0
                  • M mcgahanfl

                    char szDescription[200]; BOOL bValid; bValid= AfxIsValidString(szDescription, 400); //bValid is TRUE, shouldn't it be FALSE?????????/ thanks

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    AfxIsValidString can only verify that the string is readable. If you happen to pass in garbage that contains no \0 character, then it will return an error. However, in your case, even if there were no \0 characters in the first 200 bytes, the memory past the end of the string is probably readable and thus will not generate an error. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    M 1 Reply Last reply
                    0
                    • T Tim Smith

                      AfxIsValidString can only verify that the string is readable. If you happen to pass in garbage that contains no \0 character, then it will return an error. However, in your case, even if there were no \0 characters in the first 200 bytes, the memory past the end of the string is probably readable and thus will not generate an error. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      M Offline
                      M Offline
                      mcgahanfl
                      wrote on last edited by
                      #10

                      thank you, that is a reasonable explanation

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups