Skip to content

Commit

Permalink
Fix crash when warning offline user
Browse files Browse the repository at this point in the history
When a warned screenname is offline ras would attempt to send an empty SNAC,
which caused a crash when marshalling the nil value. Fix by sending a proper
error message, and also detect the nil value when marshalling so we don't crash
on future empty snacs.

Closes #38
  • Loading branch information
jgknight committed Jun 1, 2024
1 parent e78cfc3 commit bf2613f
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 bf2613f

Please sign in to comment.