Repository: netty/netty
File: transport-native-unix-common/src/main/c/netty_unix_socket.c (C)

Code before:
    }

    NETTY_JNI_UTIL_NEW_LOCAL_FROM_WEAK(env, datagramSocketAddressClass, datagramSocketAddressClassWeak, done);

    obj = (*env)->NewObject(env, datagramSocketAddressClass, datagramSocketAddrMethodId, addressBytes, scopeId, port, len, local);
    if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
        obj = NULL;
        goto done;
    }
done:
    NETTY_JNI_UTIL_DELETE_LOCAL(env, datagramSocketAddressClass);

    return obj;
}

static int domainSocketPathLength(const struct sockaddr_un* s, const socklen_t addrlen) {
#ifdef __linux__
    // Linux supports abstract domain sockets so we need to handle it.
    // https://man7.org/linux/man-pages/man7/unix.7.html
    if (s->sun_path[0] == '\0') {
       // This is an abstract domain socket address
       return (addrlen - sizeof(sa_family_t));
    }
#else
    return strlen(s->sun_path);
#endif
}

static jobject createDomainDatagramSocketAddress(JNIEnv* env, const struct sockaddr_storage* addr, const socklen_t addrlen, int len, jobject local) {
    jclass domainDatagramSocketAddressClass = NULL;
    jobject obj  = NULL;
    struct sockaddr_un* s = (struct sockaddr_un*) addr;
    int pathLength = domainSocketPathLength(s, addrlen);
    jbyteArray pathBytes = (*env)->NewByteArray(env, pathLength);
    if (pathBytes == NULL) {
        return NULL;
    }

    (*env)->SetByteArrayRegion(env, pathBytes, 0, pathLength, (jbyte*) &s->sun_path);

    NETTY_JNI_UTIL_NEW_LOCAL_FROM_WEAK(env, domainDatagramSocketAddressClass, domainDatagramSocketAddressClassWeak, done);

    obj = (*env)->NewObject(env, domainDatagramSocketAddressClass, domainDatagramSocketAddrMethodId, pathBytes, len, local);
    if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
        return NULL;
    }
done:
    NETTY_JNI_UTIL_DELETE_LOCAL(env, domainDatagramSocketAddressClass);

    return obj;
}

Review comment (suggestion):
```suggestion
#endif
    return strlen(s->sun_path);
```

Code after:
    }

    NETTY_JNI_UTIL_NEW_LOCAL_FROM_WEAK(env, datagramSocketAddressClass, datagramSocketAddressClassWeak, done);

    obj = (*env)->NewObject(env, datagramSocketAddressClass, datagramSocketAddrMethodId, addressBytes, scopeId, port, len, local);
    if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
        obj = NULL;
        goto done;
    }
done:
    NETTY_JNI_UTIL_DELETE_LOCAL(env, datagramSocketAddressClass);

    return obj;
}

static int domainSocketPathLength(const struct sockaddr_un* s, const socklen_t addrlen) {
#ifdef __linux__
    // Linux supports abstract domain sockets so we need to handle it.
    // https://man7.org/linux/man-pages/man7/unix.7.html
    if (addrlen >= sizeof(sa_family_t) && s->sun_path[0] == '\0') {
       // This is an abstract domain socket address
       return (addrlen - sizeof(sa_family_t));
    }
#endif
    return strlen(s->sun_path);
}

static jobject createDomainDatagramSocketAddress(JNIEnv* env, const struct sockaddr_storage* addr, const socklen_t addrlen, int len, jobject local) {
    jclass domainDatagramSocketAddressClass = NULL;
    jobject obj  = NULL;
    struct sockaddr_un* s = (struct sockaddr_un*) addr;
    int pathLength = domainSocketPathLength(s, addrlen);
    jbyteArray pathBytes = (*env)->NewByteArray(env, pathLength);
    if (pathBytes == NULL) {
        return NULL;
    }

    (*env)->SetByteArrayRegion(env, pathBytes, 0, pathLength, (jbyte*) &s->sun_path);

    NETTY_JNI_UTIL_NEW_LOCAL_FROM_WEAK(env, domainDatagramSocketAddressClass, domainDatagramSocketAddressClassWeak, done);

    obj = (*env)->NewObject(env, domainDatagramSocketAddressClass, domainDatagramSocketAddrMethodId, pathBytes, len, local);
    if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
        return NULL;
    }
done:
    NETTY_JNI_UTIL_DELETE_LOCAL(env, domainDatagramSocketAddressClass);

    return obj;
}