Subtracting 0 from one-past-end pointer

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

    Subtracting 0 from one-past-end pointer

    For the following code:

    int main(void)
    {
    short a[100], *ptr;

    ptr = a + 100;
    ptr -= 0;
    }

    a runtime bounds-checker I use gives an error "Pointer
    underrun" on the ptr -= 0 line. Is there something wrong
    with that statement?
  • Ben Pfaff

    #2
    Re: Subtracting 0 from one-past-end pointer

    oldwolf@inspire .net.nz (Old Wolf) writes:
    [color=blue]
    > int main(void)
    > {
    > short a[100], *ptr;
    >
    > ptr = a + 100;
    > ptr -= 0;
    > }
    >
    > a runtime bounds-checker I use gives an error "Pointer
    > underrun" on the ptr -= 0 line. Is there something wrong
    > with that statement?[/color]

    No, not as far as I can tell. (I'm surprised that the compiler
    emits any code for that statement though.)
    --
    "Large amounts of money tend to quench any scruples I might be having."
    -- Stephan Wilms

    Comment

    • E. Robert Tisdale

      #3
      Re: Subtracting 0 from one-past-end pointer

      Old Wolf wrote:
      [color=blue]
      > For the following code:
      >
      > int main(void)
      > {
      > short a[100], *ptr;
      >
      > ptr = a + 100;
      > ptr -= 0;
      > }
      >
      > a runtime bounds-checker I use
      > gives an error "Pointer underrun" on the ptr -= 0 line.
      > Is there something wrong with that statement?[/color]
      [color=blue]
      > cat main.c[/color]
      int main(int argc, char* argv[]) {
      short a[100], *ptr = a + 100;
      ptr -= 0;
      return 0;
      }
      [color=blue]
      > gcc -Wall -std=c99 -pedantic -o main main.c
      > ./main[/color]

      It works fine for me.

      Your run time bounds checker is probably buggy.

      Comment

      • Robert Gamble

        #4
        Re: Subtracting 0 from one-past-end pointer

        On Tue, 16 Nov 2004 18:05:11 -0800, Old Wolf wrote:
        [color=blue]
        > For the following code:
        >
        > int main(void)
        > {
        > short a[100], *ptr;
        >
        > ptr = a + 100;
        > ptr -= 0;
        > }
        >
        > a runtime bounds-checker I use gives an error "Pointer
        > underrun" on the ptr -= 0 line. Is there something wrong
        > with that statement?[/color]

        I don't see anything wrong with any part of the code.

        Perhaps for some reason your bounds-checker does not like the statement
        before the one in question where you assign ptr to point to one past the
        last element of a. Do you experience the same error if you change it to
        "ptr = a + 99"?

        Rob Gamble

        Comment

        • Robert Gamble

          #5
          Re: Subtracting 0 from one-past-end pointer

          On Tue, 16 Nov 2004 18:27:25 -0800, E. Robert Tisdale wrote:
          [color=blue]
          > Old Wolf wrote:
          >[color=green]
          >> For the following code:
          >>
          >> int main(void)
          >> {
          >> short a[100], *ptr;
          >>
          >> ptr = a + 100;
          >> ptr -= 0;
          >> }
          >>
          >> a runtime bounds-checker I use
          >> gives an error "Pointer underrun" on the ptr -= 0 line.
          >> Is there something wrong with that statement?[/color]
          >[color=green]
          > > cat main.c[/color]
          > int main(int argc, char* argv[]) {
          > short a[100], *ptr = a + 100;
          > ptr -= 0;
          > return 0;
          > }
          >[color=green]
          > > gcc -Wall -std=c99 -pedantic -o main main.c
          > > ./main[/color]
          >
          > It works fine for me.
          >
          > Your run time bounds checker is probably buggy.[/color]

          $ cat test7.c
          int main(void)
          {
          short a[100], *ptr;

          ptr = a + 1000;
          ptr -= 0;
          }

          $ gcc -Wall --std=c99 -pedantic test7.c
          $ ./a.out

          Works fine for me.

          The point here of course being that just because your compiler didn't
          produce any warnings, or even that the resulting program does not crash,
          does not imply that the code exhibits no undefined behavior.

          Rob Gamble

          Comment

          • E. Robert Tisdale

            #6
            Re: Subtracting 0 from one-past-end pointer

            Ben Pfaff wrote:
            [color=blue]
            > Old Wolf writes:
            >[color=green]
            >> int main(void)
            >> {
            >> short a[100], *ptr;
            >>
            >> ptr = a + 100;
            >> ptr -= 0;
            >> }
            >>
            >>a runtime bounds-checker I use
            >>gives an error "Pointer underrun" on the ptr -= 0 line.
            >>Is there something wrong with that statement?[/color]
            >
            > No, not as far as I can tell.
            > (I'm surprised that
            > the compiler emits any code for that statement though.)[/color]
            [color=blue]
            > cat main.c[/color]
            int main(int argc, char* argv[]) {
            short a[100], *ptr = a + 100;
            ptr -= 0;
            return 0;
            }
            [color=blue]
            > gcc -Wall -std=c99 -pedantic -S main.c
            > cat main.sold[/color]
            .file "main.c"
            .text
            .globl main
            .type main, @function
            main:
            pushl %ebp
            movl %esp, %ebp
            subl $232, %esp
            andl $-16, %esp
            movl $0, %eax
            addl $15, %eax
            addl $15, %eax
            shrl $4, %eax
            sall $4, %eax
            subl %eax, %esp
            leal -216(%ebp), %eax// a
            addl $200, %eax // a + 100;
            movl %eax, -220(%ebp)// ptr = a + 100
            movl $0, %eax // return 0;
            leave
            ret
            .size main, .-main
            .section .note.GNU-stack,"",@progb its
            .ident "GCC: (GNU) 3.4.1"

            My compiler doesn't.

            Comment

            • Dave Vandervies

              #7
              Re: Subtracting 0 from one-past-end pointer

              In article <pan.2004.11.17 .03.19.52.59798 2@gmail.com>,
              Robert Gamble <rgamble99@gmai l.com> wrote:[color=blue]
              >On Tue, 16 Nov 2004 18:05:11 -0800, Old Wolf wrote:
              >[color=green]
              >> For the following code:
              >>
              >> int main(void)
              >> {
              >> short a[100], *ptr;
              >>
              >> ptr = a + 100;
              >> ptr -= 0;
              >> }
              >>
              >> a runtime bounds-checker I use gives an error "Pointer
              >> underrun" on the ptr -= 0 line. Is there something wrong
              >> with that statement?[/color]
              >
              >I don't see anything wrong with any part of the code.[/color]

              Nor do I. One past the end of an array is a valid pointer for any use
              other than dereferencing it, and adding or subtracting 0 to/from a valid
              pointer should be a no-op (and, in particular, give you back a (the same)
              valid pointer).

              [color=blue]
              >Perhaps for some reason your bounds-checker does not like the statement
              >before the one in question where you assign ptr to point to one past the
              >last element of a. Do you experience the same error if you change it to
              >"ptr = a + 99"?[/color]

              If that's the case, then the bounds-checker is wrong. It should only
              trap if the OP's code tries to dereference ptr, not if it subtracts a
              nonnegative value from it. (Well, nonnegative and not greater than 100.)


              dave

              --
              Dave Vandervies dj3vande@csclub .uwaterloo.ca

              Silly, yes. But have we shown that it is unhelpful?
              --Daniel Fox in comp.lang.c

              Comment

              • Dan Pop

                #8
                Re: Subtracting 0 from one-past-end pointer

                In <843a4f78.04111 61805.27326514@ posting.google. com> oldwolf@inspire .net.nz (Old Wolf) writes:
                [color=blue]
                >For the following code:
                >
                > int main(void)
                > {
                > short a[100], *ptr;
                >
                > ptr = a + 100;
                > ptr -= 0;
                > }
                >
                >a runtime bounds-checker I use gives an error "Pointer
                >underrun" on the ptr -= 0 line. Is there something wrong
                >with that statement?[/color]

                Nope, the standard explicitly allows it:

                If both the pointer operand and the result point to elements
                of the same array object, or one past the last element of the
                array object, the evaluation shall not produce an overflow;

                The definition of C makes *reliable* bound checking at run time incredibly
                difficult, but your case can be trivially handled by a run time bound
                checker.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de
                Currently looking for a job in the European Union

                Comment

                Working...