Re: [go-nuts] weird sighup

69 views
Skip to first unread message

Ian Lance Taylor

unread,
May 19, 2020, 8:09:42 PM5/19/20
to anderson....@gmail.com, golang-nuts
On Tue, May 19, 2020 at 9:53 AM <anderson....@gmail.com> wrote:
>
> I am using go-1.14.1. I am using this code to remove i-th element from the slice.
>
> s = append(s[:i], s[i+1:]...)
>
> It works on my machine but gives panic on docker-container with error "range out of bound" when it has just 1 element left.
>
> I want to check if this is an unsafe code and how can I improve this?

It's hard to tell with only a code fragment. The line you show will
crash if i >= len(s).

Ian

Adrian Ratnapala

unread,
May 19, 2020, 10:53:53 PM5/19/20
to Ian Lance Taylor, anderson....@gmail.com, golang-nuts
I'd have expected that to work, because syntactically the slice is
assigned to only after the new, smaller slice is fully constructed --
including all the data copying. But it still make me twitchy about
the possibility that the runtime shortens the slice before reading all
the data, resulting in an out-of-bounds read.

As defensive programming, you can first make an explicit slice
variable for the tail and then do the append.

tail := s[i+1:]
s = append(s[:i], tail...)
// tail is still in scope ... so maybe that makes a differences?

I'd be interested to see if this changes anything. Because if it
works, it smells like a bug in the runtime.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUdMiJLvdfYe9%2B7fzjeuwJtu6uGxmW3UE-%3D%2BZ2DQLU%3D1Q%40mail.gmail.com.



--
Adrian Ratnapala

Ian Lance Taylor

unread,
May 19, 2020, 10:58:08 PM5/19/20
to Adrian Ratnapala, anderson....@gmail.com, golang-nuts
On Tue, May 19, 2020 at 7:53 PM Adrian Ratnapala
<adrian.r...@gmail.com> wrote:
>
> I'd have expected that to work, because syntactically the slice is
> assigned to only after the new, smaller slice is fully constructed --
> including all the data copying. But it still make me twitchy about
> the possibility that the runtime shortens the slice before reading all
> the data, resulting in an out-of-bounds read.
>
> As defensive programming, you can first make an explicit slice
> variable for the tail and then do the append.
>
> tail := s[i+1:]
> s = append(s[:i], tail...)
> // tail is still in scope ... so maybe that makes a differences?
>
> I'd be interested to see if this changes anything. Because if it
> works, it smells like a bug in the runtime.

I'm really quite certain that that is not what is happening here.
This expression is a standard approach for removing an element from a
slice. E.g., see https://golang.org/wiki/SliceTricks.

That said, it's impossible to tell what the problem is without a
complete standalone test case.

Ian
Reply all
Reply to author
Forward
0 new messages