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

fix: fix CompactTextString panics with nested Anys and private fields #24

Merged
merged 7 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## [Unreleased]

## [v1.4.3](https://github.com/cosmos/gogoproto/releases/tag/v1.4.2) - 2022-10-14

### Bug Fixes

- [#24](https://github.com/cosmos/gogoproto/pull/24) Fix `CompactTextString` panics with nested Anys and private fields.
- [#14](https://github.com/cosmos/gogoproto/pull/14) Fix `make regenerate`.

## [v1.4.2](https://github.com/cosmos/gogoproto/releases/tag/v1.4.2) - 2022-09-14
Expand Down
11 changes: 9 additions & 2 deletions proto/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,15 @@ func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error {
sprops := GetProperties(st)
for i := 0; i < sv.NumField(); i++ {
fv := sv.Field(i)

props := sprops.Prop[i]
name := st.Field(i).Name

// skip unexported fields (i.e first letter is lowercase)
if name[0] != strings.ToUpper(name)[0] {
continue
}

if name == "XXX_NoUnkeyedLiteral" {
continue
}
Expand Down Expand Up @@ -481,6 +487,7 @@ var textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
// writeAny writes an arbitrary field.
func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Properties) error {
v = reflect.Indirect(v)
k := v.Kind()

if props != nil {
if len(props.CustomType) > 0 {
Expand Down Expand Up @@ -533,7 +540,7 @@ func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Propert
}

// Floats have special cases.
if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
if k == reflect.Float32 || k == reflect.Float64 {
x := v.Float()
var b []byte
switch {
Expand All @@ -553,7 +560,7 @@ func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Propert

// We don't attempt to serialise every possible value type; only those
// that can occur in protocol buffers.
switch v.Kind() {
switch k {
case reflect.Slice:
// Should only be a []byte; repeated fields are handled in writeStruct.
if err := writeString(w, string(v.Bytes())); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions proto/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,28 @@ func TestRacyMarshal(t *testing.T) {
}()
}
}

func TestAny(t *testing.T) {
any := &pb.MyMessage{Count: proto.Int32(47), Name: proto.String("David")}
proto.SetExtension(any, pb.E_Ext_Text, proto.String("bar"))
b, err := proto.Marshal(any)
if err != nil {
panic(err)
}
m := &proto3pb.Message{
Name: "David",
ResultCount: 47,
Anything: &types.Any{TypeUrl: proto.MessageName(any), Value: b},
}

expected := `name: "David"
result_count: 47
anything: <
type_url: "test_proto.MyMessage"
value: "\302\006\003bar\010/\022\005David"
>`
got := proto.MarshalTextString(m)
if strings.EqualFold(expected, got) {
t.Errorf("got = %s, want %s", expected, got)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid order of the error log; got should be first then expected :-) Also this test is saying that the values match with the same case then error out, did you mean if !strings.EqualFold?

Copy link
Member Author

@julienrbrt julienrbrt Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, actually the test is not so useful either tbh (and wrong as you've noticed).
I have tested the fix using replace in the SDK directly, and quickly wrong this without paying too much attention :/ I can fix it or just remove it 🤷🏾‍♂️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix it :-) I am up and will gladly review it.

}
}