Skip to content

Commit 7338df2

Browse files
fix(channels): check json marshal/unmarshal errors in toChannelHashes
Replace silently discarded json.Marshal and json.Unmarshal errors with explicit checks. If serialization fails, log a warning and either return early (for the config-level marshal/unmarshal) or skip the channel (for per-channel marshal). This prevents silent data loss when channel configuration contains unexpected types.
1 parent 413d374 commit 7338df2

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

pkg/channels/manager_channel.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@ import (
44
"crypto/md5"
55
"encoding/hex"
66
"encoding/json"
7+
"log"
78

89
"github.com/sipeed/picoclaw/pkg/config"
910
)
1011

1112
func toChannelHashes(cfg *config.Config) map[string]string {
1213
result := make(map[string]string)
1314
ch := cfg.Channels
14-
// should not be error
15-
marshal, _ := json.Marshal(ch)
15+
marshal, err := json.Marshal(ch)
16+
if err != nil {
17+
log.Printf("[manager_channel] failed to marshal channels config: %v", err)
18+
return result
19+
}
1620
var channelConfig map[string]map[string]any
17-
_ = json.Unmarshal(marshal, &channelConfig)
21+
if err := json.Unmarshal(marshal, &channelConfig); err != nil {
22+
log.Printf("[manager_channel] failed to unmarshal channels config: %v", err)
23+
return result
24+
}
1825

1926
for key, value := range channelConfig {
2027
if enabled, ok := value["enabled"].(bool); !ok || !enabled {
2128
continue
2229
}
2330
hiddenValues(key, value, ch.Get(key))
24-
valueBytes, _ := json.Marshal(value)
31+
valueBytes, err := json.Marshal(value)
32+
if err != nil {
33+
log.Printf("[manager_channel] failed to marshal channel %s config: %v", key, err)
34+
continue
35+
}
2536
hash := md5.Sum(valueBytes)
2637
result[key] = hex.EncodeToString(hash[:])
2738
}

pkg/tools/toolloop.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ func RunToolLoop(
132132
Content: response.Content,
133133
}
134134
for _, tc := range normalizedToolCalls {
135-
argumentsJSON, _ := json.Marshal(tc.Arguments)
135+
argumentsJSON, err := json.Marshal(tc.Arguments)
136+
if err != nil {
137+
logger.Warnf("toolloop: failed to marshal tool call arguments for %s: %v", tc.Name, err)
138+
argumentsJSON = []byte("{}")
139+
}
136140
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
137141
ID: tc.ID,
138142
Type: "function",

0 commit comments

Comments
 (0)