diff --git a/pkg/model/livestream.go b/pkg/model/livestream.go index fbf5c69b..2bfa3ccf 100644 --- a/pkg/model/livestream.go +++ b/pkg/model/livestream.go @@ -27,6 +27,9 @@ type Livestream struct { } func (ls *Livestream) ToLivestreamView() (*streamplace.Livestream_LivestreamView, error) { + if ls == nil || ls.Livestream == nil { + return nil, fmt.Errorf("livestream record is nil") + } rec, err := lexutil.CborDecodeValue(*ls.Livestream) if err != nil { return nil, fmt.Errorf("error decoding feed post: %w", err) diff --git a/pkg/model/livestream_test.go b/pkg/model/livestream_test.go new file mode 100644 index 00000000..c7e2d014 --- /dev/null +++ b/pkg/model/livestream_test.go @@ -0,0 +1,33 @@ +package model + +import ( + "testing" +) + +// TestToLivestreamViewNil verifies ToLivestreamView returns an error instead of +// panicking when called on a nil receiver or when the embedded Livestream blob +// is nil. Reproduces the panic reported in +// https://github.com/streamplace/streamplace/issues/1079. +func TestToLivestreamViewNil(t *testing.T) { + t.Run("nil receiver", func(t *testing.T) { + var ls *Livestream + view, err := ls.ToLivestreamView() + if err == nil { + t.Fatalf("expected error for nil receiver, got nil") + } + if view != nil { + t.Fatalf("expected nil view for nil receiver, got %+v", view) + } + }) + + t.Run("nil livestream blob", func(t *testing.T) { + ls := &Livestream{} + view, err := ls.ToLivestreamView() + if err == nil { + t.Fatalf("expected error for nil livestream blob, got nil") + } + if view != nil { + t.Fatalf("expected nil view for nil livestream blob, got %+v", view) + } + }) +} diff --git a/pkg/spxrpc/place_stream_live.go b/pkg/spxrpc/place_stream_live.go index c00ee9e6..3802a75e 100644 --- a/pkg/spxrpc/place_stream_live.go +++ b/pkg/spxrpc/place_stream_live.go @@ -561,6 +561,9 @@ func (s *Server) handlePlaceStreamLiveStopLivestream(ctx context.Context, body * if err != nil { return nil, echo.NewHTTPError(http.StatusInternalServerError, "error getting livestream", err) } + if livestream == nil || livestream.Livestream == nil { + return nil, echo.NewHTTPError(http.StatusBadRequest, "no active livestream for this repo") + } livestreamView, err := livestream.ToLivestreamView() if err != nil {