Skip to content

Commit

Permalink
Merge pull request #39 from jgknight/jgknight/fix-38-warn-crash
Browse files Browse the repository at this point in the history
Fix crash when warning offline user
  • Loading branch information
mk6i authored Jun 1, 2024
2 parents e78cfc3 + bf2613f commit d03adfc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion foodgroup/icbm.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,16 @@ func (s ICBMService) EvilRequest(ctx context.Context, sess *state.Session, inFra

recipSess := s.messageRelayer.RetrieveByScreenName(inBody.ScreenName)
if recipSess == nil {
return wire.SNACMessage{}, nil
return wire.SNACMessage{
Frame: wire.SNACFrame{
FoodGroup: wire.ICBM,
SubGroup: wire.ICBMErr,
RequestID: inFrame.RequestID,
},
Body: wire.SNACError{
Code: wire.ErrorCodeNotLoggedOn,
},
}, nil
}

increase := evilDelta
Expand Down
4 changes: 4 additions & 0 deletions wire/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
)

var ErrMarshalFailure = errors.New("failed to marshal")
var ErrMarshalFailureNilSNAC = errors.New("attempting to marshal a nil SNAC")

func Marshal(v any, w io.Writer) error {
return marshal(reflect.TypeOf(v), reflect.ValueOf(v), "", w)
}

func marshal(t reflect.Type, v reflect.Value, tag reflect.StructTag, w io.Writer) error {
if t == nil {
return ErrMarshalFailureNilSNAC
}
switch t.Kind() {
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
Expand Down
6 changes: 6 additions & 0 deletions wire/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ func TestMarshal(t *testing.T) {
},
wantErr: ErrMarshalFailure,
},
{
name: "empty snac",
w: &bytes.Buffer{},
given: nil,
wantErr: ErrMarshalFailureNilSNAC,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d03adfc

Please sign in to comment.