Announcement

Collapse
No announcement yet.

Question to Bob Zale

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Question to Bob Zale

    Bob,

    I learned from your latest newsletter the availability of #DEBUG DISPLAY ON. I found it a great feature and tried to use it right away with a program that calls a DLL. Soon enough I got the popups but the error made no sense to me. Here is a little code that reproduces the same error:

    The DLL:

    Code:
    #COMPILE DLL
    #DIM ALL
    GLOBAL secPop3Host AS STRING
    GLOBAL secUser AS STRING
    GLOBAL secPassword AS STRING
    SUB SecuredSMTP ALIAS "SecuredSMTP"(Pop3Host AS ASCIIZ, sUSER AS ASCIIZ, _
                         password AS ASCIIZ) EXPORT
        secPop3Host = Pop3Host
        secUser = sUSER
        secPassword = password
    END SUB
    The program that calls the DLL:
    Code:
    #DEBUG DISPLAY ON
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "win32api.inc"
    
    GLOBAL hRemailLib AS DWORD
    DECLARE SUB FAL_SecuredSMTP(Pop3Host AS ASCIIZ, USER AS ASCIIZ, password AS ASCIIZ)
    FUNCTION PBMAIN () AS LONG
          LoadRemailLib
          SecuredSMTP "", "", ""
          UnloadRemailLib
    END FUNCTION
    SUB SecuredSMTP(Pop3Host AS ASCIIZ, sUSER AS ASCIIZ, password AS ASCIIZ)
       LOCAL procAddr   AS DWORD
       IF hRemailLib <> 0 THEN
          procAddr = GetProcAddress(hRemailLib, "SecuredSMTP")
          CALL DWORD procAddr USING FAL_SecuredSMTP(Pop3Host, sUSER, password)
       END IF
    END SUB
    SUB LoadRemailLib()
         IF hRemailLib = 0 THEN
            hRemailLib = LoadLibrary("debsecure.dll")
        END IF
    END SUB
    SUB UnloadRemailLib()
        IF hRemailLib <> 0 THEN
            FreeLibrary hRemailLib
            hRemailLib = 0
        END IF
    END SUB
    It looks like the line
    CALL DWORD procAddr USING FAL_SecuredSMTP(Pop3Host, sUSER, password)
    generates a harmless exception. Why?


    Thanks,


    Peter Redei

    #2
    Peter -

    You will get a much faster response to your question (from Bob Zale or his support staff) if you go to the Help Desk and look for Tech Support.

    -- Walter
    Last edited by Walter Henn; 31 Mar 2009, 08:32 PM.

    Comment


      #3
      Best guess without trying anything......

      Code:
      SecuredSMTP "", "", ""
      A quirk of the compiler's handling of ASCIIZ string prarameters is that literal nul ("") is converted to BYVAL %NULL (zero) at the point of call.

      "AS ASCIIZ" in your DLL-based function is looking for the address of an asciiz string.. but that is coming in BYVAL %NULL, which is recognized as a 'null pointer,' probably because it IS a null pointer.

      "By convention" a null pointer for an Nts-string parameter means "null string - deal with it"

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


        #4
        Michael,

        You got it right. Thank you very much.
        The issue raises another question: since we have no control over the value someone passes to a DLL function does that mean we should never create a function passing ASCIIZ? I have to admit I used ASCIIZ in such places broadly in the last 10+ years.

        Peter Redei

        Comment


          #5
          Peter--

          There's nothing that unique about ASCIIZ parameters. You cannot control what the caller is going to pass as a parameter. It's really easy to pass a dangerous value for any data type if the user isn't careful enough.

          It may be that your DLL should check every parameter for valid data and pointers before it even starts to function. That's the safest thing to do, but you have to weigh it against the time it takes to do that. Most DLL's, including the WinAPI, put that responsibility on the caller, and do not validate.

          Bob Zale
          PowerBASIC Inc.

          Comment


            #6
            The issue raises another question: since we have no control over the value someone passes to a DLL function does that mean we should never create a function passing ASCIIZ?
            Au contraire... if you are designing a function for "other language" programmers to use, NTS (ASCIIZ) strings are probably the best way to design the call.

            But I would never design a function for others to use that did NOT validate every passed parameter. As far as I'm concerned that just comes with the territory.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


              #7
              Thank you Bob and Michael. The problem is that there is no way to check the passed value in the DLL when I pass ASCIIZ. The exception occurs before the value gets to the DLL but only when Call DWORD/LoadLibrary, etc is used. I believe the best way is probably not to use ASCIIZ that will bring an additional advantage of supporting unicode strings and localization. Until Bob wrote about #DEBUG DISPLAY ON I had no idea about this problem and perhaps it caused some later crashes I could not account anything for.


              Peter Redei

              Comment


                #8
                Huh? checking the parameter is simple enough...
                Code:
                #COMPILE DLL 
                
                FUNCTION Foo (szA AS ASCIIZ, szB AS ASCIIZ) EXPORT AS LONG 
                
                    IF VARPTR (szA) <> 0& THEN 
                         szA  is available to read or write (up to valid length) 
                        .....
                   IF VARPTR (szB) <> 0& THEN 
                         szB  is available to read or write (up to valid length)
                If the string is not available to your procedure, just DON'T USE IT.

                That the #DEBUG DISPLAY compile option does something is immaterial, since that is not used at production time.

                Using null-terminated (ASCIIZ) strings by reference and setting that address to zero when the string is null is far and away the most common way to pass string parameters among procedures in the World of Windows.

                Maybe you were out sick that day, but testing for and handling "null string" like this is part of the "Writing Library Functions 101" class.

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                  #9
                  Originally posted by Michael Mattias View Post
                  ...
                  Maybe you were out sick that day, but testing for and handling "null string" like this is part of the "Writing Library Functions 101" class.

                  MCM
                  Is this kind of comment really necessary, Michael? Put downs like this say a lot more about you than the person you're putting down.
                  Last edited by Walter Henn; 5 Apr 2009, 04:26 AM.

                  Comment


                    #10
                    Hi Peter--

                    The first part of Michael's response was very correct. ASCIIZ parameters are a very important concept, and should be considered for any multi-language DLL. It's easy to test them for nul, as he demonstrated, and that's a good procedure to follow.

                    I apologize for his last sentence.

                    Best regards,

                    Bob Zale
                    PowerBASIC Inc.

                    Comment


                      #11
                      Originally posted by Bob Zale View Post
                      I apologize for his last sentence.
                      Bob, I don't think you want to start doing that. It could turn out to be a major undertaking.

                      =============================================
                      I have taken all knowledge to be my province.
                      Sir Francis Bacon
                      =============================================
                      It's a pretty day. I hope you enjoy it.

                      Gösta

                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                      Comment


                        #12
                        Michael,
                        I think you totally missed the point.
                        The problem is not how to evaluate the passed ASCIIZ data in the DLL. The exception occurs BEFORE the data gets to the DLL when it is an empty string.
                        In real life #DEBUG DISPLAY ON indeed is not there but the exception itself is. As I said I used ASCIIZ extensively in my DLLs and I am not aware of any problem with them. The #DEBUG DISPLAY ON just made me suspicious.


                        Peter Redei

                        Comment


                          #13
                          Well, it IS an exception.... a null pointer was passed to a function expecting a pointer.

                          You can't ask the compiler and related tools to know if a null pointer is "OK" in this case, you can only ask the tool to tell you if the pointer was NULL.

                          BTW, you would not get that exception if instead of passing literal null ("") at the point of call you passed asciiz variables which just happen to be null. Of course, your function would now have to deal with this possibility as well.

                          MCM
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                            #14
                            I had to post here - Gosta made me chuckle out loud!

                            Bob, I don't think you want to start doing that. It could turn out to be a major undertaking.
                            What would the forums be without their personalities!

                            When I first joined the forums I was often taken aback - I couldn't decide if acerbic or acumenical responses were the norm. Now, I can't wait to get my daily dose of a stick in the eye!

                            It's like when I play tennis (I have a very strong forehand) - I feel obligated to crush the ball when someone has the temerity to hit a ball to that side.

                            That's kind of how it is in the forums here. If you ask a question, you have to expect a drink from a fire hose - and sometimes the water is very cold or very hot!

                            It's a great place to have fun.

                            (and yes, I have been reading "Thomas Covenant - The Unbeliever". It's author tosses out words I've never even seen so I'm finding myself trying to use a broader vocabulary than usual).

                            Comment


                              #15
                              I was beginning to wonder Gary (your typing style does not usually warrant $20 words)

                              $20 word scare me.

                              (That and I have to look them up just to get the gist of the joke)

                              Bob....when are you hiring for the team to handle obscure sarcasms that insult until the insult-ee learns WHYYYYyyy the insult???

                              (not to mention when the insult is unwarranted, given the experience of the person asking, vs experience of the answer (and often the answer can be wrong depending the scope of the question))
                              Engineer's Motto: If it aint broke take it apart and fix it

                              "If at 1st you don't succeed... call it version 1.0"

                              "Half of Programming is coding"....."The other 90% is DEBUGGING"

                              "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                              Comment


                                #16
                                Originally posted by Gary Beene View Post
                                I had to post here -
                                ... acumenical ...
                                ???

                                =================================
                                Why worry about martial law when
                                there is pork to be harvested
                                and photo ops to attend?"
                                Joe Byrne
                                =================================
                                It's a pretty day. I hope you enjoy it.

                                Gösta

                                JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                Comment


                                  #17
                                  Quote:
                                  Originally Posted by Gary Beene
                                  I had to post here -
                                  ... acumenical ...


                                  ???

                                  My best answer
                                  orangutan
                                  ?????


                                  (Anyone missing the razz must not be in NY and not see the hilarious joke over words and kind of a locale classic)

                                  By the way what the heck is acumenical??? Google eludes to it being something religious...but could not find a definition....

                                  $50 word I take it???
                                  Engineer's Motto: If it aint broke take it apart and fix it

                                  "If at 1st you don't succeed... call it version 1.0"

                                  "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                  "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                  Comment


                                    #18
                                    Originally posted by Cliff Nichols View Post
                                    Quote:...
                                    By the way what the heck is acumenical??? Google eludes
                                    Very very very clever, as ... acumenical ... does not appear to be an actual word. (Elude 2. To escape the understanding or grasp of ...)

                                    to it being something religious...
                                    Oh bummer. I see you now meant allude (Allude - To make an indirect reference). Not so clever after all. Actually ... {laughing here at my own droll cleveriosity}

                                    ===================================
                                    "Being politically correct means
                                    always having to say you're sorry."
                                    Anonymous
                                    ===================================
                                    It's a pretty day. I hope you enjoy it.

                                    Gösta

                                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                    Comment


                                      #19
                                      I couldn't decide if acerbic or acumenical responses were the norm
                                      I think Gary is just trying to modernise the English language

                                      Many of the responses are acerbic and/or contain acumen.

                                      Bob
                                      Last edited by Robert Wallace; 9 Apr 2009, 05:04 AM.

                                      Comment

                                      Working...
                                      X
                                      😀
                                      🥰
                                      🤢
                                      😎
                                      😡
                                      👍
                                      👎