Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/gob: spin / crash #2267

Closed
bradfitz opened this issue Sep 17, 2011 · 3 comments
Closed

encoding/gob: spin / crash #2267

bradfitz opened this issue Sep 17, 2011 · 3 comments
Assignees

Comments

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Sep 17, 2011

Either the docs or implementation are wrong.

This crashes (after a few second pause burning CPU):

$ cat gob.go
package main

import (
    "bytes"
    "gob"
    "log"
    "os"
)

type BlobRef struct {
    foo, bar string
}

func (br *BlobRef) String() string {
    if br == nil {
        return "<nil BlobRef>"
    }
    return br.foo + "-" + br.bar
}

func (br *BlobRef) GobEncode() ([]byte, os.Error) {
    log.Printf("GobEncode")
        return []byte(br.String()), nil
}

func (br *BlobRef) GobDecode(b []byte) os.Error {
    if br == nil {
        panic("nil receiver in GobDecode")
    }
    log.Printf("GobDecode(%q)", string(b))
    br.foo = "foo"
    br.bar = "bar"
    return nil
}

func main() {
    br := &BlobRef{"foo", "bar"}
    log.Printf("blobref String = %q", br)

    buf := new(bytes.Buffer)
    e := gob.NewEncoder(buf)
    err := e.Encode(br)
        if err != nil {
        log.Fatalf("Encode: %v", err)
        }

    d := gob.NewDecoder(buf)
        var got *BlobRef
        err = d.Decode(&got)
        if err != nil {
        log.Fatalf("Decode: %v", err)
        }
    if got == nil {
                log.Fatalf("got == nil")
        }

    if got.String() != br.String() {
                log.Fatalf("got = %q, want %q", got, br)
    }
    log.Printf("got = %q", &got)
}


$ gorun gob.go
2011/09/16 17:30:30 blobref String = "foo-bar"
2011/09/16 17:30:30 GobEncode
2011/09/16 17:30:30 GobDecode("foo-bar")
unexpected fault address 0x0
throw: fault
[signal 0xb code=0x80 addr=0x0 pc=0x409172]

goroutine 1 [running]:
main.(*BlobRef).String(0xf840000740, 0x45e7b0, 0xf8400002b8, 0x0)
    /home/bradfitz/goplay/gob.go:18 +0x7d
main.main()
    /home/bradfitz/goplay/gob.go:57 +0x427



$ hg identify
849002209017 tip

Attachments:

  1. gob.go (1089 bytes)
@gopherbot
Copy link

@gopherbot gopherbot commented Sep 17, 2011

Comment 1 by robpike:

It's decoding incorrectly, probably due to an incorrect indirection calculation. The
value of got is not right - the strings inside are invalid, and the + operation is
looping. I want to debug this thoroughly - the runtime should protect itself better -
but in the meantime:
A simple fix for this example is to change got to be a BlobRef rather than a *BlobRef
(and remove the comparison against nil). That will work.
@bradfitz
Copy link
Contributor Author

@bradfitz bradfitz commented Sep 17, 2011

Comment 2:

Yeah, I ended up finding that workaround, but it felt wrong.  Somewhat glad to hear it's
a real bug and I'm not confused.
@robpike
Copy link
Contributor

@robpike robpike commented Sep 20, 2011

Comment 3:

This issue was closed by revision 9ddc2b5.

Status changed to Fixed.

@bradfitz bradfitz added fixed labels Sep 20, 2011
@mikioh mikioh changed the title gob spin / crash encoding/gob: spin / crash Feb 26, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.