cx icon indicating copy to clipboard operation
cx copied to clipboard

Unexpected result when dereferencing pointer to slice

Open vidya88 opened this issue 5 years ago • 4 comments

@amherag commented on Feb 1

Describe the bug Dereferencing a pointer to a slice is not working correctly when used in append or when trying to access a value from that slice in certain cases.

To Reproduce Run:

package main

func foo (slc *[]i32) {
        *slc = append(*slc, 3)
        *slc = append(*slc, 5)
        *slc = append(*slc, 7)

        i32.print((*slc)[0]) // prints the value correctly
        i32.print((*slc)[1]) // prints the value correctly
        i32.print((*slc)[2]) // prints the value correctly
}

func main () {
        var slc *[]i32
        foo(slc)

        // *slc = append(*slc, 1)
        // *slc = append(*slc, 2) // this throws a panic

        i32.print(len(slc)) // prints len correctly
        i32.print((*slc)[0]) // prints garbage: 1048642
}

Expected behavior Not printing garbage (maybe it's the slice's address) and not panicking in *slc = append(*slc, 2).

Desktop (please complete the following information):

  • OS: Linux
  • CX 0.6.0

vidya88 avatar Sep 24 '19 05:09 vidya88

@ingwal commented on Feb 2

Hmm, it looks to me that slc is an unitialized pointer on the line containing "foo(slc)". Or is there an actual slice created on the line just before ("var slc *[]i32")?

vidya88 avatar Sep 24 '19 05:09 vidya88

@amherag commented on Feb 9

Ohhhhhhh you're right

vidya88 avatar Sep 24 '19 05:09 vidya88

@ingwal commented on Feb 9

What's the intented behaviour here? Creating an anonymous slice that the pointer can point to, or throw an error?

vidya88 avatar Sep 24 '19 05:09 vidya88

@arfan499 @amherag

1> Verify if this bug still exists. If it is fixed, then close bug

2> If bug still exists, then notify me.

3> And make sure we have a unit test for this ticket. And reply to ticket, with link to unit test.

SkycoinSynth avatar Feb 04 '21 16:02 SkycoinSynth