NULL to strlen function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam_cit@yahoo.co.in

    NULL to strlen function

    Hi Everyone,

    I tried the following program unit in Microsoft Visual c++ 6.0 and the
    program caused unexpected behavior,

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char *p;
    p = NULL;
    printf("%d\n",s trlen(p));
    }

    Is it that strlen function can't handle NULL pointers?

  • mark_bluemel@pobox.com

    #2
    Re: NULL to strlen function


    sam_...@yahoo.c o.in wrote:
    Is it that strlen function can't handle NULL pointers?
    Strlen expects a string as an argument. Is a NULL pointer a string?
    (Hint: this is a rhetorical question).

    Comment

    • Richard Heathfield

      #3
      Re: NULL to strlen function

      sam_cit@yahoo.c o.in said:
      Hi Everyone,
      >
      I tried the following program unit in Microsoft Visual c++ 6.0 and the
      program caused unexpected behavior,
      Undefined behaviour.
      #include <stdio.h>
      #include <string.h>
      >
      int main()
      {
      char *p;
      p = NULL;
      printf("%d\n",s trlen(p));
      }
      >
      Is it that strlen function can't handle NULL pointers?
      Or is it that strlen returns size_t and your format specifier requires int?

      It is impossible to tell which of these two produces the undefined behaviour
      you have noticed.

      Any good C book will explain the semantics of strlen.

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

      email: rjh at the above domain, - www.

      Comment

      • Barry

        #4
        Re: NULL to strlen function


        <sam_cit@yahoo. co.inwrote in message
        news:1168519098 .421364.11980@o 58g2000hsb.goog legroups.com...
        Hi Everyone,
        >
        I tried the following program unit in Microsoft Visual c++ 6.0 and the
        program caused unexpected behavior,
        >
        #include <stdio.h>
        #include <string.h>
        >
        int main()
        {
        char *p;
        p = NULL;
        printf("%d\n",s trlen(p));
        }
        >
        Is it that strlen function can't handle NULL pointers?
        >
        My interpretation would be by definition strlen cannot
        properly handle a NULL pointer.

        Returns

        The strlen function returns the number of characters that precede the
        terminating null
        character.


        Comment

        • Francois Grieu

          #5
          Re: NULL to strlen function

          In article <1168519098.421 364.11980@o58g2 000hsb.googlegr oups.com>,
          sam_cit@yahoo.c o.in wrote:
          I tried the following program unit in Microsoft Visual c++ 6.0
          and the program caused unexpected behavior,
          #include <stdio.h>
          #include <string.h>

          int main()
          {
          char *p;
          p = NULL;
          printf("%d\n",s trlen(p));
          }
          Is it that strlen function can't handle NULL pointers?
          There is no requirement that it does.
          NULL is not a valid argument for a char* or const char*
          in most functions of <string.h>.
          This is hinted in by the lack of explict allowance for
          NULL in 7.21.1 (String functions conventions) of
          ISO/IEC 9899:1999

          For an empty string, try
          p = "";


          Francois Grieu

          Comment

          • Barry

            #6
            Re: NULL to strlen function


            "Francois Grieu" <fgrieu@gmail.c omwrote in message
            news:fgrieu-6AD040.14185011 012007@news-3.proxad.net...
            In article <1168519098.421 364.11980@o58g2 000hsb.googlegr oups.com>,
            sam_cit@yahoo.c o.in wrote:
            >
            I tried the following program unit in Microsoft Visual c++ 6.0
            and the program caused unexpected behavior,
            >
            #include <stdio.h>
            #include <string.h>
            >
            int main()
            {
            char *p;
            p = NULL;
            printf("%d\n",s trlen(p));
            }
            >
            Is it that strlen function can't handle NULL pointers?
            >
            There is no requirement that it does.
            NULL is not a valid argument for a char* or const char*
            in most functions of <string.h>.
            This is hinted in by the lack of explict allowance for
            NULL in 7.21.1 (String functions conventions) of
            ISO/IEC 9899:1999
            >
            For an empty string, try
            p = "";
            >
            >
            Francois Grieu
            This doesn't need to be hinted by anything.
            strlen() returns a size_t i.e. an unsigned integer.


            Comment

            • Chris Dollin

              #7
              Re: NULL to strlen function

              Barry wrote:
              "Francois Grieu" <fgrieu@gmail.c omwrote in message
              news:fgrieu-6AD040.14185011 012007@news-3.proxad.net...
              >In article <1168519098.421 364.11980@o58g2 000hsb.googlegr oups.com>,
              > sam_cit@yahoo.c o.in wrote:
              >>
              I tried the following program unit in Microsoft Visual c++ 6.0
              and the program caused unexpected behavior,
              >>
              >#include <stdio.h>
              >#include <string.h>
              >>
              >int main()
              >{
              > char *p;
              > p = NULL;
              > printf("%d\n",s trlen(p));
              >}
              >>
              Is it that strlen function can't handle NULL pointers?
              >>
              >There is no requirement that it does.
              >NULL is not a valid argument for a char* or const char*
              >in most functions of <string.h>.
              >This is hinted in by the lack of explict allowance for
              >NULL in 7.21.1 (String functions conventions) of
              >ISO/IEC 9899:1999
              >>
              >For an empty string, try
              > p = "";
              >>
              > Francois Grieu
              >
              This doesn't need to be hinted by anything.
              strlen() returns a size_t i.e. an unsigned integer.
              Even if that is fixed, it doesn't matter. `strlen` is a
              Standard library function, and hence, unless otherwise
              specified, is undefined when a pointer argument is null.

              As someone else said: NULL isn't a string. So asking for
              its string-length is like asking custard for its display
              resolution.

              --
              Chris "first on the Underground!" Dollin
              A rock is not a fact. A rock is a rock.

              Comment

              • Clark S. Cox III

                #8
                Re: NULL to strlen function

                Barry wrote:
                I thought (perhaps mistakenly) the question was trying to be more
                thoughtful and the OP meant to ask something like:
                >
                Why doesn't strlen check for a NULL pointer and return.
                If I pass NULL to strlen, then it was a mistake; simply returning would
                only server to mask that error, and potentially hide it until my product
                was already released. If strlen attempts to dereference NULL then it
                will interrupt the program (on every implementation that I use, and yes,
                I test this assumption before relying on it on every platform for which
                I code).

                I prefer my errors to be caught *before* release as much as possible.

                --
                Clark S. Cox III
                clarkcox3@gmail .com

                Comment

                • Barry

                  #9
                  Re: NULL to strlen function


                  "Chris Dollin" <chris.dollin@h p.comwrote in message
                  news:eo5fj9$o3d $1@murdoch.hpl. hp.com...
                  Barry wrote:
                  >
                  "Francois Grieu" <fgrieu@gmail.c omwrote in message
                  news:fgrieu-6AD040.14185011 012007@news-3.proxad.net...
                  In article <1168519098.421 364.11980@o58g2 000hsb.googlegr oups.com>,
                  sam_cit@yahoo.c o.in wrote:
                  >
                  I tried the following program unit in Microsoft Visual c++ 6.0
                  and the program caused unexpected behavior,
                  >
                  #include <stdio.h>
                  #include <string.h>
                  >
                  int main()
                  {
                  char *p;
                  p = NULL;
                  printf("%d\n",s trlen(p));
                  }
                  >
                  Is it that strlen function can't handle NULL pointers?
                  >
                  There is no requirement that it does.
                  NULL is not a valid argument for a char* or const char*
                  in most functions of <string.h>.
                  This is hinted in by the lack of explict allowance for
                  NULL in 7.21.1 (String functions conventions) of
                  ISO/IEC 9899:1999
                  >
                  For an empty string, try
                  p = "";
                  >
                  Francois Grieu
                  This doesn't need to be hinted by anything.
                  strlen() returns a size_t i.e. an unsigned integer.
                  >
                  Even if that is fixed, it doesn't matter. `strlen` is a
                  Standard library function, and hence, unless otherwise
                  specified, is undefined when a pointer argument is null.
                  >
                  As someone else said: NULL isn't a string. So asking for
                  its string-length is like asking custard for its display
                  resolution.
                  The OP often posts questions that demonstrate he/she
                  hasn't applied what he/she should have taken away from
                  previous responses.

                  I thought (perhaps mistakenly) the question was trying to
                  be more thoughtful and the OP meant to ask something like:

                  Why doesn't strlen check for a NULL pointer and return.


                  Comment

                  • David T. Ashley

                    #10
                    Re: NULL to strlen function

                    <sam_cit@yahoo. co.inwrote in message
                    news:1168519098 .421364.11980@o 58g2000hsb.goog legroups.com...
                    Hi Everyone,
                    >
                    I tried the following program unit in Microsoft Visual c++ 6.0 and the
                    program caused unexpected behavior,
                    >
                    #include <stdio.h>
                    #include <string.h>
                    >
                    int main()
                    {
                    char *p;
                    p = NULL;
                    printf("%d\n",s trlen(p));
                    }
                    >
                    Is it that strlen function can't handle NULL pointers?
                    You're apparently a new programmer, so let me add this:

                    A NULL pointer and a string of length zero are not the same thing.

                    A NULL pointer traditionally has the value of 0, which is usually an
                    unsuitable pointer; but in any case it is a reserved value defined to be a
                    non-functional pointer.

                    A string of length 0 has a non-NULL pointer, but the area of memory that is
                    pointed to has its first byte as 0.

                    The two are very different.


                    Comment

                    • Christopher Benson-Manica

                      #11
                      Re: NULL to strlen function

                      Clark S. Cox III <clarkcox3@gmai l.comwrote:
                      If I pass NULL to strlen, then it was a mistake
                      On the contrary, I think it's certainly possible to imagine code that
                      could profitably take advantage of the hypothetical ability of
                      strlen() to deal reasonably with a null pointer. Given how the
                      language is defined, of course, it's a mistake.
                      only server to mask that error, and potentially hide it until my product
                      was already released. If strlen attempts to dereference NULL then it
                      will interrupt the program (on every implementation that I use, and yes,
                      I test this assumption before relying on it on every platform for which
                      I code).
                      If you find a platform for which your assumption does not hold, what
                      are your plans for testing your code?

                      --
                      C. Benson Manica | I *should* know what I'm talking about - if I
                      cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

                      Comment

                      • David T. Ashley

                        #12
                        Re: NULL to strlen function

                        "Christophe r Benson-Manica" <ataru@otaku.fr eeshell.orgwrot e in message
                        news:eo5jps$p6m $1@chessie.cirr .com...
                        Clark S. Cox III <clarkcox3@gmai l.comwrote:
                        >
                        >If I pass NULL to strlen, then it was a mistake
                        >
                        On the contrary, I think it's certainly possible to imagine code that
                        could profitably take advantage of the hypothetical ability of
                        strlen() to deal reasonably with a null pointer. Given how the
                        language is defined, of course, it's a mistake.
                        >
                        >only server to mask that error, and potentially hide it until my product
                        >was already released. If strlen attempts to dereference NULL then it
                        >will interrupt the program (on every implementation that I use, and yes,
                        >I test this assumption before relying on it on every platform for which
                        >I code).
                        >
                        If you find a platform for which your assumption does not hold, what
                        are your plans for testing your code?
                        I think Clark's comments are reasonable. It is no different than generously
                        using assert(), except he is relying on the [hardware/OS] platform to check
                        the assertions.


                        Comment

                        • Barry

                          #13
                          Re: NULL to strlen function


                          "Clark S. Cox III" <clarkcox3@gmai l.comwrote in message
                          news:12qcj50d7f cii90@corp.supe rnews.com...
                          Barry wrote:
                          I thought (perhaps mistakenly) the question was trying to be more
                          thoughtful and the OP meant to ask something like:

                          Why doesn't strlen check for a NULL pointer and return.
                          >
                          If I pass NULL to strlen, then it was a mistake; simply returning would
                          only server to mask that error, and potentially hide it until my product
                          was already released. If strlen attempts to dereference NULL then it
                          will interrupt the program (on every implementation that I use, and yes,
                          I test this assumption before relying on it on every platform for which
                          I code).
                          >
                          I prefer my errors to be caught *before* release as much as possible.
                          Your inability to read an entire message or thread and reply is
                          laughable.


                          Comment

                          • Barry

                            #14
                            Re: NULL to strlen function


                            "Clark S. Cox III" <clarkcox3@gmai l.comwrote in message
                            news:12qcj50d7f cii90@corp.supe rnews.com...
                            Barry wrote:
                            I thought (perhaps mistakenly) the question was trying to be more
                            thoughtful and the OP meant to ask something like:

                            Why doesn't strlen check for a NULL pointer and return.
                            >
                            If I pass NULL to strlen, then it was a mistake; simply returning would
                            only server to mask that error, and potentially hide it until my product
                            was already released. If strlen attempts to dereference NULL then it
                            will interrupt the program (on every implementation that I use, and yes,
                            I test this assumption before relying on it on every platform for which
                            I code).
                            >
                            I prefer my errors to be caught *before* release as much as possible.
                            >
                            --
                            Clark S. Cox III
                            clarkcox3@gmail .com
                            The NEXT time you choose to pick a sentence out of context you
                            should properly apologize in the same forum.


                            Comment

                            • Clark S. Cox III

                              #15
                              Re: NULL to strlen function

                              Christopher Benson-Manica wrote:
                              Clark S. Cox III <clarkcox3@gmai l.comwrote:
                              >
                              >If I pass NULL to strlen, then it was a mistake
                              >
                              On the contrary, I think it's certainly possible to imagine code that
                              could profitably take advantage of the hypothetical ability of
                              strlen() to deal reasonably with a null pointer. Given how the
                              language is defined, of course, it's a mistake.
                              Other than assert'ing that the parameter is non-NULL, what would you
                              consider a reasonable way for strlen to deal with NULL?
                              only server to mask that error, and potentially hide it until my product
                              was already released. If strlen attempts to dereference NULL then it
                              will interrupt the program (on every implementation that I use, and yes,
                              I test this assumption before relying on it on every platform for which
                              I code).
                              >
                              If you find a platform for which your assumption does not hold, what
                              are your plans for testing your code?
                              If I find a platform where that assumption doesn't hold, then, as I said
                              above, I won't rely on it. The last platform on which I did any
                              extensive coding that didn't, upon a read from NULL, cause programs to
                              immediately crash, interrupt, drop into a debugger, etc. was the old
                              (i.e. pre MacOSX, pre-VM) MacOS. However, even there, it was a simple
                              matter to configure the debugger to watch for access to that memory
                              location.


                              --
                              Clark S. Cox III
                              clarkcox3@gmail .com

                              Comment

                              Working...