Skip to content

bug: unix_addr check is useless #1348

@aMOPel

Description

@aMOPel
  • [ x] I have looked at the documentation here first?
  • [ x] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10.23.0

Issue, Question or Enhancement:

The unix_addr check is useless. I looked into the src:

https://github.com/go-playground/validator/blob/6c3307e6c64040ebc0efffe9366927c68146ffba/baked_in.go#L2564C16-L2564C31

func isUnixAddrResolvable(fl FieldLevel) bool {
	_, err := net.ResolveUnixAddr("unix", fl.Field().String())

	return err == nil
}

and in stdlib net:

https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/net/unixsock.go;l=57

func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
	switch network {
	case "unix", "unixgram", "unixpacket":
		return &UnixAddr{Name: address, Net: network}, nil
	default:
		return nil, UnknownNetworkError(network)
	}
}

effectively this will never throw an error, since you're always passing "unix".

In fact, ResolveUnixAddr is itself useless to check if the socket actually exists.

You need something that checks if the file exists and if that file is a socket:

func IsSocket(filePath string) (err error) {
	stats, err := os.Stat(filePath)
	if err != nil {
		err = fmt.Errorf("check file existence: %w", err)
		return
	}
	if stats.Mode().Type() != fs.ModeSocket {
		err = fmt.Errorf("not socket file: %v", filePath)
		return
	}
	return
}

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions