strlen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ¬a\\/b

    #46
    Re: strlen

    On 7 Sep 2007 22:46:23 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
    wrote:
    >In article <98i3e3tg83l6m7 r18cmmd69pc4448 fpqom@4ax.com>, ¬a\\/b <al@f.gwrote:
    >>On Fri, 07 Sep 2007 23:52:48 +0200, "¬a\\/b" <al@f.gwrote:
    >>>On Fri, 07 Sep 2007 23:49:45 +0200, "¬a\\/b" <al@f.gwrote:
    >>>>On Mon, 03 Sep 2007 21:24:38 -0000,(Gordon Burditt) wrote:
    >>>>>>>strlen is wrong because can not report if there is some error
    >>>>>
    >>>>>strlen() has no way to report errors, according to its definition
    >>>>>in Standard C.
    >>>>
    >>>>strlen has a way to report error because the string
    >>>
    >>>strlen() has no way to report errors,
    >>
    >>wrong etc
    >
    >Perhaps you'd like to try arguing with yourself in private?
    no i like to arguing with myself in public
    >-- Richard
    your is a not valid signature

    On Mon, 03 Sep 2007 21:24:38 -0000,(Gordon Burditt) wrote:
    >>>strlen is wrong because can not report if there is some error
    >
    >strlen() has no way to report errors, according to its definition
    >in Standard C.
    strlen has a way to report error because the string
    a[strlen(a)];
    not exist if strlen(a)=size_ tmax

    |""| = 0 len, 1 size
    |"a"| = 1 len, 2 size
    .............
    and not exist
    |"..."| = size_tmax len, (size_tmax +1) size

    so strlen() can return only <=(size_tmax -1)
    so size_tmax is for error

    is it above correct?
    >strlen() may well have no way to *detect* the error of an unterminated
    >string without causing undefined behavior
    wrong for above
    >(aborting the program with
    >a smegmentation fault, for example. And nobody said a smegmentation
    >fault has to be a catchable error).
    >
    >>>e.g.
    >>>char *a;
    >>>and "a" point to an array of size=size_t max that has no 0 in it
    >>>
    >>>strlen is defined on strings. A string has a 0 char terminating it. It
    >>>is not strlen that is wrong but the incompetent programmer.
    >>
    >>you have right only if the programmer never make errors
    >>are you a programmer of that kind?
    >
    >How does one *detect* such an error? Especially if a + size_t max is
    >off the end of allocated memory?
    see above: strlen return size_tmax
    >>if strlen sees some problem it has to report an error
    >
    >That's not what Standard C says.
    :)))
    >>example return (size_t)-1 and not continue to run
    >
    >It can either return or not continue to run; it can't do both.
    yes it has to return and not hang the program with the infinite loop
    but possibly i have seen wrong on it
    >>if there is strlen(0); it has to report the error and not shut down
    >>the system
    >
    >That's not what Standard C says. And there's a difference between
    >shutting down the *program* (e.g. with smegmentation fault), and
    >shutting down the *system* (e.g. with blue screen of death or panic:
    >out of swap space).
    if your program is an OS program it can shut down the system
    >>So if i want to hang some of your pragrams i have just give a string
    >>of size_t max len that has 111111111111111 111111111111111 1111111111
    >>etc in it because for these string, there is the chance strlen should
    >>result in an infinite loop (or ggets or fgets or what you want).
    >
    >>the idea is *all* functions should can to report errors if they see
    >>these errors (and the programmer has to see if something goes wrong
    >>until it is printf)
    >
    >I'd be interested in how functions like exit() and abort() report errors,
    >and how a programmer checks for errors.
    i don't know, i say that it is better that every function return in
    some way if the operation goes well
    or all data has some flag that say if all operations with that data
    are all ok

    Comment

    • ¬a\\/b

      #47
      Re: strlen

      On Sat, 08 Sep 2007 07:09:54 +0200, "¬a\\/b" <al@f.gwrote:
      >On 7 Sep 2007 22:46:23 GMT, (Richard Tobin)wrote:
      >>In article :
      >>>On Fri, 07 Sep 2007 23:52:48 +0200, "¬a\\/b" <al@f.gwrote:
      >>>>On Fri, 07 Sep 2007 23:49:45 +0200, "¬a\\/b" <al@f.gwrote:
      >>>>>On Mon, 03 Sep 2007 21:24:38 -0000,(Gordon Burditt) wrote:
      >>>>>>>>strle n is wrong because can not report if there is some error
      >>>>>>
      >>>>>>strlen( ) has no way to report errors, according to its definition
      >>>>>>in Standard C.
      >>>>>
      >>>>>strlen has a way to report error because the string
      >>>>
      >>>>strlen() has no way to report errors,
      >>>
      >>>wrong etc
      >>
      >>Perhaps you'd like to try arguing with yourself in private?
      >
      >no i like to arguing with myself in public
      yes i like it :))

      pheraps it is better that strlen(0); segfault the program
      (if the program is not an OS program)
      so i can see there is some error because program not continue.

      this in the realty is ok and make to find many errors

      the alternative is
      1) the error handling like c++
      2) the programmer controll each call
      3) the system controll each call like:

      e^^printf ret<=0 ? (showerr("print f"), exit(0), 1) : ret ;
      e^^strlen
      ret==size_tmax| |arg1==0 ? (showerr("strle n"),exit(0), 1): ret;
      e^^malloc ret==0 ? (showerr("mallo c"), exit(0),1): ret;
      e1^^malloc ((int)arg1)<0|| ret==0? (showerr("mallo c"), exit(0),1): ret;


      int main()
      {
      if( e1^^printf(a)== printf(b) )
      c=malloc(134);
      e^^strlen(c);

      }

      so each function can be associated with one or more functios that
      determine what the system has to do if found an error in the arguments
      of the function or in the return of the function
      for exampe

      malloc(a);
      is the system malloc that behave like the standard C one

      e^^malloc(a);
      is the system malloc that behave like the standard C one that check
      for ret==0; if find ret==0 show a message and exit

      e1^^malloc(a);
      is the system malloc that behave like the standard C one that check
      for if (int)a<0 (arg1<0) or return ==0 (ret==0) and in that case exit

      but for this i have to blame a language like say Sosman and others
      because i never will write a compiler

      Comment

      • ¬a\\/b

        #48
        Re: strlen

        On Sat, 08 Sep 2007 20:45:35 +0200, "¬a\\/b" <al@f.gwrote:
        >On Sat, 08 Sep 2007 07:09:54 +0200, "¬a\\/b" <al@f.gwrote:
        >>On 7 Sep 2007 22:46:23 GMT, (Richard Tobin)wrote:
        >>>In article :
        >>>>On Fri, 07 Sep 2007 23:52:48 +0200, "¬a\\/b" <al@f.gwrote:
        >>>>>On Fri, 07 Sep 2007 23:49:45 +0200, "¬a\\/b" <al@f.gwrote:
        >>>>>>On Mon, 03 Sep 2007 21:24:38 -0000,(Gordon Burditt) wrote:
        >>>>>>>>>strl en is wrong because can not report if there is some error
        >>>>>>>
        >>>>>>>strlen () has no way to report errors, according to its definition
        >>>>>>>in Standard C.
        >>>>>>
        >>>>>>strlen has a way to report error because the string
        >>>>>
        >>>>>strlen() has no way to report errors,
        >>>>
        >>>>wrong etc
        >>>
        >>>Perhaps you'd like to try arguing with yourself in private?
        >>
        >>no i like to arguing with myself in public
        >
        >yes i like it :))
        >
        >pheraps it is better that strlen(0); segfault the program
        >(if the program is not an OS program)
        >so i can see there is some error because program not continue.
        >
        >this in the realty is ok and make to find many errors
        >
        >the alternative is
        >1) the error handling like c++
        >2) the programmer controll each call
        >3) the system controll each call like:
        4) data object has a number or a flag that rapresent error that
        programmer can check whatever it like and all function that use that
        data can to change in case of errors

        Comment

        • Tor Rustad

          #49
          Re: strlen

          Keith Thompson wrote:

          [...]
          As for strlen, it *could* explicitly check for a null pointer
          argument, and I wouldn't complain if an implementation did so. I
          wouldn't even complain if a future standard required such a check (as
          long as it defines the behavior if the check fails). But it's just
          not practical to check for all possible argument errors. For example:
          ...
          char *ptr = malloc(6);
          assert(ptr != NULL);
          strcpy(ptr, "hello");
          free(ptr);
          strlen(ptr);
          ...
          The strlen call is invalid, but detecting it would probably hurt the
          performance for valid arguments.
          Agreed, a run-time check would hurt performance, but for your example,
          this fault can be detected at no cost:

          $ cat -n invalid_ptr.c
          1 #include <stdio.h>
          2 #include <stdlib.h>
          3 #include <string.h>
          4 #include <assert.h>
          5
          6 int main(void)
          7 {
          8 size_t len;
          9 char *ptr = malloc(6);
          10
          11 assert(ptr != NULL);
          12 strcpy(ptr, "hello");
          13 free(ptr);
          14
          15 len = strlen(ptr);
          16
          17 return 0;
          18 }

          $ splint invalid_ptr.c
          Splint 3.1.1 --- 20 Jun 2006

          invalid_ptr.c: (in function main)
          invalid_ptr.c:1 5:16: Variable ptr used after being released
          Memory is used after it has been released (either by passing as an
          only param
          or assigning to an only global). (Use -usereleased to inhibit warning)
          invalid_ptr.c:1 3:8: Storage ptr released

          Finished checking --- 1 code warning


          However, after calling free(), I always guard the pointer like this:

          free(ptr);
          ptr = NULL;

          An alternative string implementation might at least partly address
          this. Such alternatives have been proposed and can be implemented in
          standard C if you don't mind missing out on a bit of syntactic sugar.
          There are some good commercial memory tools available, which typically
          insturment debug builds during development, this doesn't hurt
          performance either (in release builds), but cut down the development time.


          --
          Tor <torust [at] online [dot] no>

          Comment

          • ¬a\\/b

            #50
            Re: strlen

            On Sat, 08 Sep 2007 20:45:35 +0200, "¬a\\/b" <al@f.gwrote:
            >On Sat, 08 Sep 2007 07:09:54 +0200, "¬a\\/b" <al@f.gwrote:
            >>On 7 Sep 2007 22:46:23 GMT, (Richard Tobin)wrote:
            >>>In article :
            >>>>On Fri, 07 Sep 2007 23:52:48 +0200, "¬a\\/b" <al@f.gwrote:
            >>>>>On Fri, 07 Sep 2007 23:49:45 +0200, "¬a\\/b" <al@f.gwrote:
            >>>>>>On Mon, 03 Sep 2007 21:24:38 -0000,(Gordon Burditt) wrote:
            >>>>>>>>>strl en is wrong because can not report if there is some error
            >>>>>>>
            >>>>>>>strlen () has no way to report errors, according to its definition
            >>>>>>>in Standard C.
            >>>>>>
            >>>>>>strlen has a way to report error because the string
            >>>>>
            >>>>>strlen() has no way to report errors,
            >>>>
            >>>>wrong etc
            >>>
            >>>Perhaps you'd like to try arguing with yourself in private?
            this is a public forum why i have not to write here?
            it is c related, it talk about important problem of how check errors
            so where and why check errors?
            >>no i like to arguing with myself in public
            >
            >yes i like it :))
            >
            >pheraps it is better that strlen(0); segfault the program
            >(if the program is not an OS program)
            >so i can see there is some error because program not continue.
            >
            >this in the realty is ok and make to find many errors
            >
            >the alternative is
            >1) the error handling like c++
            >2) the programmer controll each call
            >3) the system controll each call like:
            >
            >e^^printf ret<=0 ? (showerr("print f"), exit(0), 1) : ret ;
            >e^^strlen
            ret==size_tmax| |arg1==0 ? (showerr("strle n"),exit(0), 1): ret;
            >e^^malloc ret==0 ? (showerr("mallo c"), exit(0),1): ret;
            >e1^^malloc ((int)arg1)<0|| ret==0? (showerr("mallo c"), exit(0),1): ret;
            int e_printf(char* a, ... )
            {int r;
            va_list args;
            va_start(args, a);
            r=printf(a, args);
            va_end(args);
            if(r<=0)
            {showerror("pri ntf"); exit(0);}
            return r;
            }

            size_t e_strlen(char* a)
            {size_t r;
            if(a==0)
            {
            a0: showerror("strl en"); exit(0);
            }
            r=strlen(a);
            if(r==size_tmax ) goto a0;
            return r;
            }

            etc
            >int main()
            >{
            >if( e1^^printf(a)== printf(b) )
            c=malloc(134);
            >e^^strlen(c) ;
            >
            >}
            >
            >so each function can be associated with one or more functios that
            >determine what the system has to do if found an error in the arguments
            >of the function or in the return of the function
            >for exampe
            >
            >malloc(a);
            >is the system malloc that behave like the standard C one
            >
            >e^^malloc(a) ;
            >is the system malloc that behave like the standard C one that check
            >for ret==0; if find ret==0 show a message and exit
            >
            >e1^^malloc(a );
            >is the system malloc that behave like the standard C one that check
            >for if (int)a<0 (arg1<0) or return ==0 (ret==0) and in that case exit
            >
            >but for this i have to blame a language like say Sosman and others
            >because i never will write a compiler
            i have to blame nothing again

            Comment

            • ¬a\\/b

              #51
              Re: strlen

              On Sun, 09 Sep 2007 08:45:12 +0200, "¬a\\/b" <al@f.gwrote:
              >int e_printf(char* a, ... )
              >{int r;
              va_list args;
              va_start(args, a);
              r=printf(a, args);
              va_end(args);
              if(r<=0)
              {showerror("pri ntf"); exit(0);}
              better
              if(r<0)
              {showerror("pri ntf"); exit(0);}


              return r;
              >}
              >
              >size_t e_strlen(char* a)
              >{size_t r;
              if(a==0)
              {
              >a0: showerror("strl en"); exit(0);
              }
              r=strlen(a);
              if(r==size_tmax ) goto a0;
              return r;
              >}
              >
              >etc
              >
              >>int main()
              >>{
              >>if( e1^^printf(a)== printf(b) )
              > c=malloc(134);
              >>e^^strlen(c );
              >>
              >>}
              >>

              Comment

              • Rob Kendrick

                #52
                Re: strlen

                On Sat, 08 Sep 2007 07:09:54 +0200, ¬a\\/b wrote:
                On 7 Sep 2007 22:46:23 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                wrote:
                >
                >>-- Richard
                your is a not valid signature
                Wrong on two counts: The "-- Name" form is old and not wide-spread, but it
                is common - it may even be older than "-- \n". If your client doesn't
                detect this as a signature, then your client should be fixed.

                Additionally, a "-- \n" signature followed it, so one could argue that it
                was his signature at all.

                B.

                Comment

                • Richard Bos

                  #53
                  Re: strlen

                  Charles Richmond <frizzle@tx.rr. comwrote:
                  CBFalconer wrote:
                  Gordon Burditt wrote:
                  a smegmentation fault, for example. And nobody said a smegmentation
                  fault has to be a catchable error).
                  I assume a smegmentation fault is covered under the "undefined
                  behaviour" clauses, which are the known response of strlen() to
                  unterminated strings. However I have grave problems finding
                  "smegmentat ion" in N869.txt. Meanwhile, AFAICT, all my smegs are
                  apparently clear.
                  >
                  He is probably referring to "smegma", which is kind of
                  like genital toe-jam...
                  Or he may have seen Red Dwarf.

                  Richard

                  Comment

                  • Kelsey Bjarnason

                    #54
                    Re: strlen

                    On Mon, 10 Sep 2007 06:29:44 +0000, Richard Bos wrote:
                    Charles Richmond <frizzle@tx.rr. comwrote:
                    >
                    >CBFalconer wrote:
                    Gordon Burditt wrote:
                    >a smegmentation fault, for example. And nobody said a smegmentation
                    >fault has to be a catchable error).
                    >
                    I assume a smegmentation fault is covered under the "undefined
                    behaviour" clauses, which are the known response of strlen() to
                    unterminated strings. However I have grave problems finding
                    "smegmentat ion" in N869.txt. Meanwhile, AFAICT, all my smegs are
                    apparently clear.
                    >>
                    >He is probably referring to "smegma", which is kind of
                    >like genital toe-jam...
                    >
                    Or he may have seen Red Dwarf.
                    And he's a total SmrrrHEEE?

                    Comment

                    Working...