ipfs / go-cid

Content ID v1 implemented in go
MIT License
157 stars 47 forks source link

cid Undef cannot be marshalled #129

Closed ickby closed 3 years ago

ickby commented 3 years ago

Cid implements multiple go encoding interfaces, including golangs "encoding.BinaryUnmarshaler". For this it uses the function "CidFromBytes" via "Cast". However, this function cannot handle empty bytes. As the default value of a undefined cid, CidUndef, is represented via empty string/byte sequence, a problem arises. One can marshal all cids, except cid Undef, which results in an error. Imho this should be possible, as CidUndef is a valid part of the implementation.

To reproduce the problem: `

      func main() {
          gob.Register(new(cid.Cid))

          c1 := cid.Undef
          var network bytes.Buffer
          enc := gob.NewEncoder(&network)
          err := enc.Encode(c1)
          if err != nil {
              fmt.Printf("encode error: %v", err)
          }

          dec := gob.NewDecoder(&network)
          var c2 cid.Cid
          err = dec.Decode(&c2)
          if err != nil {
              fmt.Printf("decode error: %v\n", err)
          }
      }

` This results in "decode error: varints malformed, could not reach the end"

welcome[bot] commented 3 years ago

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review. In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additional round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment. Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

Finally, remember to use https://discuss.ipfs.io if you just need general support.

Stebalien commented 3 years ago

"Undef" is really just the "nil" type for CIDs, and it's not a valid CID. We refuse to unmarshal zero-length json/text CIDs as well.

Unfortunately, with "UnmarshalBinary", there is no real "null".

ickby commented 3 years ago

ok, if it's a deliberate decision I work around it.