Skip to content

Commit

Permalink
Remove null bytes from strings
Browse files Browse the repository at this point in the history
PostgreSQL does not allow null bytes in varchar, char and
text fields.
  • Loading branch information
lippserd authored and Al2Klimov committed Mar 9, 2022
1 parent 48da1a8 commit 76b447b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding"
"encoding/json"
"github.com/icinga/icingadb/internal"
"strings"
)

// String adds JSON support to sql.NullString.
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 76b447b

Please sign in to comment.