From 76b447b44c4f66c2efa9a46cc1663c668f7a0f66 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 23 Feb 2022 17:44:54 +0100 Subject: [PATCH] Remove null bytes from strings PostgreSQL does not allow null bytes in varchar, char and text fields. --- pkg/types/string.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/types/string.go b/pkg/types/string.go index a3e2174d9..f8ead450c 100644 --- a/pkg/types/string.go +++ b/pkg/types/string.go @@ -7,6 +7,7 @@ import ( "encoding" "encoding/json" "github.com/icinga/icingadb/internal" + "strings" ) // String adds JSON support to sql.NullString. @@ -52,6 +53,17 @@ func (s *String) UnmarshalJSON(data []byte) error { return nil } +// Value implements the driver.Valuer interface. +// Supports SQL NULL. +func (s String) Value() (driver.Value, error) { + if !s.Valid { + return nil, nil + } + + // PostgreSQL does not allow null bytes in varchar, char and text fields. + return strings.ReplaceAll(s.String, "\x00", ""), nil +} + // Assert interface compliance. var ( _ json.Marshaler = String{}