cx
cx copied to clipboard
Unexpected result when dereferencing pointer to slice
@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
@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")?
@amherag commented on Feb 9
Ohhhhhhh you're right
@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?
@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.