From 7c17874b41b471dc09ac3f6b6faa8e2f84bdbb92 Mon Sep 17 00:00:00 2001 From: AI Engineer Date: Sun, 28 Jun 2026 10:33:32 +0800 Subject: [PATCH] fix(aof): fix RESP length encoding for SELECT command database index --- internal/aof/log/store.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/aof/log/store.go b/internal/aof/log/store.go index d6eeb9e..66d7829 100644 --- a/internal/aof/log/store.go +++ b/internal/aof/log/store.go @@ -148,7 +148,8 @@ func (store *Store) Write(database int, command []byte) error { // log the SELECT command before logging the incoming command. // This allows us to switch databases appropriately when restoring the state on startup. if database != store.currentDatabase { - _, err := store.rw.Write([]byte(fmt.Sprintf("*2\r\n$6\r\nSELECT\r\n$1\r\n%s\r\n", strconv.Itoa(database)))) + dbStr := strconv.Itoa(database) + _, err := store.rw.Write([]byte(fmt.Sprintf("*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n", len(dbStr), dbStr))) if err != nil { return fmt.Errorf("log select error: %+v", err) } @@ -237,8 +238,9 @@ func (store *Store) Truncate() error { } // Add command to select the current database at the top of the file. - _, err := store.rw.Write([]byte( - fmt.Sprintf("*2\r\n$6\r\nSELECT\r\n$1\r\n%s\r\n", strconv.Itoa(store.currentDatabase)))) + dbStr := strconv.Itoa(store.currentDatabase) + _, err := store.rw.Write([]byte( + fmt.Sprintf("*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n", len(dbStr), dbStr))) if err != nil { return fmt.Errorf("truncate: log select error: %+v", err) }