google / uuid

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
BSD 3-Clause "New" or "Revised" License
5.24k stars 363 forks source link

NullUUID Scan method returns valid true on empty string #109

Open davidspiess opened 1 year ago

davidspiess commented 1 year ago
var id uuid.NullUUID
id.Scan("")

id.Valid == true // Shouldn't this be false?
fgm commented 1 year ago

The issue is actually deeper: the code does not do what its comments describe, on both NULL values, empty strings and empty byte slices. The comments say "if an empty UUID comes from a table, we return a null UUID", but actually what happens is:

See https://github.com/google/uuid/blob/v1.3.0/sql.go#L17. for details.

Sample program showing the issue (bug ?) https://go.dev/play/p/EzXldTzj4X_u. Run it locally: playground does not support CGO.

Release 1.3.0 introduced the uuid.Valid field partly for such cases, but that field is not actually used. Maybe the changes to sql.go which used that new feature were not committed when building the release ?

LogitechUser71592 commented 1 year ago

Thank you for this repository code. Me and my team leader are using this to make our recipe blog project for college a whole lot smoother.