f4b6a3 / uuid-creator

UUID Creator is a Java library for generating Universally Unique Identifiers.
MIT License
410 stars 44 forks source link

NPE in MacNodeIdFunction.getHardwareAddress #56

Closed tyushkin closed 2 years ago

tyushkin commented 2 years ago

On my machine InetAddress.getByName(hostname()) (in NetworkUtil::nic) returns ipv4 loopback address (127.0.0.1). (this happens only with virtual interfaces from openconnect up)

It result in NPE in all mac addr using functionality - e.g. see stack.txt

This seem like regression, in 4.1.3 this functionality works fine.

My ugly workaround Index: src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java =================================================================== diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java --- a/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java (revision a13b55e4ea443bbd6f39edbaa3cc1d73ef8c691f) +++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java (date 1643102865684) @@ -193,8 +193,10 @@ // try to find the network interface for the host name ip = InetAddress.getByName(hostname()); + // sometimes ip would be localhost ip (127.0.0.1) for normal hostname (e.g. andrey-amd) ni = NetworkInterface.getByInetAddress(ip); - if (acceptable(ni)) { + // ni would be null for localhost ip (127.0.0.1) + if (ni != null && acceptable(ni)) { nic = ni; return nic; } @@ -225,7 +227,8 @@ */ private static synchronized boolean acceptable(NetworkInterface ni) { try { - if (ni != null && ni.isUp() && !ni.isLoopback() && !ni.isVirtual()) { + // isVirtual() returns false and getHardwareAddress() returns null for (at least some) openconnect interfaces + if (ni != null && ni.isUp() && !ni.isLoopback() && !ni.isVirtual() && ni.getHardwareAddress() != null) { return true; } } catch (SocketException | NullPointerException e) {
fabiolimace commented 2 years ago

Hi @tyushkin

It's really a regression. Thanks for reporting it. We will fix this soon.

PR are welcome.

tyushkin commented 2 years ago

Created PR https://github.com/f4b6a3/uuid-creator/pull/57

fabiolimace commented 2 years ago

Merged PR #57.

Passed all tests.

Thanks, @tyushkin !

fabiolimace commented 2 years ago

Released version v4.3.2.