From f7f9fad9965f88206c5977423a51124bd6811eef Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Tue, 5 Mar 2024 17:11:36 +0100 Subject: [PATCH] Update methods and (safely) ignore errors --- databases/postgres/query_parser_test.go | 3 ++- databases/postgres/wire.go | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/databases/postgres/query_parser_test.go b/databases/postgres/query_parser_test.go index 69810b7..b4c52d9 100644 --- a/databases/postgres/query_parser_test.go +++ b/databases/postgres/query_parser_test.go @@ -12,7 +12,8 @@ func testQueryRequest() (string, string) { query := "SELECT * FROM users" queryMsg := pgproto3.Query{String: query} // Encode the data to base64. - return query, base64.StdEncoding.EncodeToString(queryMsg.Encode(nil)) + queryBytes, _ := queryMsg.Encode(nil) + return query, base64.StdEncoding.EncodeToString(queryBytes) } func Test_GetQueryFromRequest(t *testing.T) { diff --git a/databases/postgres/wire.go b/databases/postgres/wire.go index f33d3e9..ffc3170 100644 --- a/databases/postgres/wire.go +++ b/databases/postgres/wire.go @@ -42,12 +42,16 @@ func IsPostgresStartupMessage(b []byte) bool { } func ErrorResponse(msg, severity, code, detail string) []byte { - return (&pgproto3.ErrorResponse{ + // NOTE: The error from the Encode method can be safely ignored because + // the result will be nil on error. The parameters MUST be provided by us + // and they MUST NOT be filled with user input. + errResp, _ := (&pgproto3.ErrorResponse{ Severity: severity, Message: msg, Code: code, Detail: detail, }).Encode(nil) + return errResp } // IsPostgresSSLRequest returns true if the message is a SSL request.