fix: Prevent panics in Marshal and FormatTEXT
- Sanitize the Chats slice before marshaling or formatting to remove any nil entries that may have been introduced. - This is a robust fix for the segmentation fault panics.
This commit is contained in:
parent
142c649200
commit
d6f308d94a
32
config.go
32
config.go
|
@ -24,18 +24,32 @@ func (all *Chats) ConfigSave() error {
|
||||||
return errors.New("chatpb.ConfigSave() all == nil")
|
return errors.New("chatpb.ConfigSave() all == nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := all.Marshal()
|
// --- Start of Fix ---
|
||||||
|
// Create a new, clean Chats object to avoid marshaling a slice with nil entries.
|
||||||
|
cleanChats := NewChats() // Assuming NewChats() initializes the struct correctly.
|
||||||
|
cleanChats.Uuid = all.Uuid
|
||||||
|
cleanChats.Version = all.Version
|
||||||
|
|
||||||
|
// Loop through the original chats and append only the non-nil ones.
|
||||||
|
for _, chat := range all.GetChats() {
|
||||||
|
if chat != nil {
|
||||||
|
cleanChats.Chats = append(cleanChats.Chats, chat)
|
||||||
|
} else {
|
||||||
|
log.Warn("Found and skipped a nil chat entry during ConfigSave")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --- End of Fix ---
|
||||||
|
|
||||||
|
data, err := cleanChats.Marshal() // Marshal the clean object, not 'all'
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("chatpb proto.Marshal() failed len", len(data), err)
|
log.Info("chatpb proto.Marshal() failed len", len(data), err)
|
||||||
// often this is because strings have invalid UTF-8. This should probably be fixed in the protobuf code
|
// The tryValidate logic might be less necessary now but kept for safety.
|
||||||
if err := all.tryValidate(); err != nil {
|
if err := all.tryValidate(); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
// re-attempt Marshal() here
|
data, err = cleanChats.Marshal() // Retry with the clean object
|
||||||
data, err = all.Marshal()
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// validate & sanitize strings worked
|
log.Info("chatpb.ConfigSave() pb.Marshal() worked after validation len", len(cleanChats.Chats), "chats")
|
||||||
log.Info("chatpb.ConfigSave() pb.Marshal() worked len", len(all.Chats), "chats")
|
|
||||||
configWrite("gemini.pb", data)
|
configWrite("gemini.pb", data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -43,11 +57,11 @@ func (all *Chats) ConfigSave() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := configWrite("gemini.pb", data); err != nil {
|
if err := configWrite("gemini.pb", data); err != nil {
|
||||||
log.Infof("chatpb.ConfigSave() failed len(Chats)=%d bytes=%d", len(all.Chats), len(data))
|
log.Infof("chatpb.ConfigSave() failed len(Chats)=%d bytes=%d", len(cleanChats.Chats), len(data))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
configWrite("gemini.text", []byte(all.FormatTEXT()))
|
configWrite("gemini.text", []byte(cleanChats.FormatTEXT()))
|
||||||
log.Infof("chatpb.ConfigSave() worked len(Chats)=%d bytes=%d", len(all.Chats), len(data))
|
log.Infof("chatpb.ConfigSave() worked len(Chats)=%d bytes=%d", len(cleanChats.Chats), len(data))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue