Skip to content

Commit

Permalink
Fix json.Unmarshal as a pointer should be passed to it
Browse files Browse the repository at this point in the history
  • Loading branch information
claucece authored and armfazh committed Sep 13, 2023
1 parent ae23192 commit 4c71580
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crypto/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func (p *Point) MarshalJSON() ([]byte, error) {

func (p *Point) UnmarshalJSON(data []byte) error {
var byteRepr []byte
err := json.Unmarshal(data, byteRepr)
err := json.Unmarshal(data, &byteRepr)
if err != nil {
return err
}
return p.Unmarshal(p.Curve, data)
return p.Unmarshal(p.Curve, byteRepr)
}

// Marshal calls through to elliptic.Marshal using the Curve field of the
Expand Down
26 changes: 25 additions & 1 deletion crypto/curve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ import (
"testing"
)

func TestMarshalAndUnmarshalJSONP256(t *testing.T) {
curve := elliptic.P256()
_, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
t.Fatal(err)
}
P := &Point{Curve: curve, X: x, Y: y}
uBytes, err := P.MarshalJSON()
if err != nil {
t.Fatal(err)
}
Q := &Point{Curve: curve, X: nil, Y: nil}
err = Q.UnmarshalJSON(uBytes)
if err != nil {
t.Fatal(err)
}
if P.X.Cmp(Q.X) != 0 || P.Y.Cmp(Q.Y) != 0 {
t.Fatal("point came back different")
}
}

func TestUncompressedRoundTripP256(t *testing.T) {
curve := elliptic.P256()
_, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
Expand All @@ -17,7 +38,10 @@ func TestUncompressedRoundTripP256(t *testing.T) {
P := &Point{Curve: curve, X: x, Y: y}
uBytes := P.Marshal()
Q := &Point{Curve: curve, X: nil, Y: nil}
Q.Unmarshal(curve, uBytes)
err = Q.Unmarshal(curve, uBytes)
if err != nil {
t.Fatal(err)
}
if P.X.Cmp(Q.X) != 0 || P.Y.Cmp(Q.Y) != 0 {
t.Fatal("point came back different")
}
Expand Down
2 changes: 1 addition & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func loadConfigFile(filePath string) (Server, error) {
if err != nil {
return conf, err
}
err = json.Unmarshal(data, conf)
err = json.Unmarshal(data, &conf)
if err != nil {
return conf, err
}
Expand Down

0 comments on commit 4c71580

Please sign in to comment.