Skip to content

Commit 45b223a

Browse files
committed
Fix panic on reading nil TLV8 #1507
1 parent 90544ba commit 45b223a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pkg/hap/camera/accessory_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package camera
22

33
import (
44
"encoding/base64"
5+
"strings"
56
"testing"
67

78
"github.com/AlexxIT/go2rtc/pkg/hap"
89
"github.com/stretchr/testify/require"
910
)
1011

12+
func TestNilCharacter(t *testing.T) {
13+
var res SetupEndpoints
14+
char := &hap.Character{}
15+
err := char.ReadTLV8(&res)
16+
require.NotNil(t, err)
17+
require.NotNil(t, strings.Contains(err.Error(), "can't read value"))
18+
}
19+
1120
type testTLV8 struct {
1221
name string
1322
value string

pkg/hap/character.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hap
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"net/http"
89

@@ -126,11 +127,17 @@ func (c *Character) Write(v any) (err error) {
126127

127128
// ReadTLV8 value to right struct
128129
func (c *Character) ReadTLV8(v any) (err error) {
129-
return tlv8.UnmarshalBase64(c.Value.(string), v)
130+
if s, ok := c.Value.(string); ok {
131+
return tlv8.UnmarshalBase64(s, v)
132+
}
133+
return fmt.Errorf("hap: can't read value: %v", v)
130134
}
131135

132-
func (c *Character) ReadBool() bool {
133-
return c.Value.(bool)
136+
func (c *Character) ReadBool() (bool, error) {
137+
if v, ok := c.Value.(bool); ok {
138+
return v, nil
139+
}
140+
return false, fmt.Errorf("hap: can't read value: %v", c.Value)
134141
}
135142

136143
func (c *Character) String() string {

0 commit comments

Comments
 (0)