mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-09-27 04:36:12 +08:00
Fix panic on reading nil TLV8 #1507
This commit is contained in:
@@ -2,12 +2,21 @@ package camera
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/hap"
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNilCharacter(t *testing.T) {
|
||||||
|
var res SetupEndpoints
|
||||||
|
char := &hap.Character{}
|
||||||
|
err := char.ReadTLV8(&res)
|
||||||
|
require.NotNil(t, err)
|
||||||
|
require.NotNil(t, strings.Contains(err.Error(), "can't read value"))
|
||||||
|
}
|
||||||
|
|
||||||
type testTLV8 struct {
|
type testTLV8 struct {
|
||||||
name string
|
name string
|
||||||
value string
|
value string
|
||||||
|
@@ -3,6 +3,7 @@ package hap
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -126,11 +127,17 @@ func (c *Character) Write(v any) (err error) {
|
|||||||
|
|
||||||
// ReadTLV8 value to right struct
|
// ReadTLV8 value to right struct
|
||||||
func (c *Character) ReadTLV8(v any) (err error) {
|
func (c *Character) ReadTLV8(v any) (err error) {
|
||||||
return tlv8.UnmarshalBase64(c.Value.(string), v)
|
if s, ok := c.Value.(string); ok {
|
||||||
|
return tlv8.UnmarshalBase64(s, v)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("hap: can't read value: %v", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Character) ReadBool() bool {
|
func (c *Character) ReadBool() (bool, error) {
|
||||||
return c.Value.(bool)
|
if v, ok := c.Value.(bool); ok {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
return false, fmt.Errorf("hap: can't read value: %v", c.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Character) String() string {
|
func (c *Character) String() string {
|
||||||
|
Reference in New Issue
Block a user