Skip to content

Commit f4107a9

Browse files
Fix getting port issue in posix os_socket_bind (bytecodealliance#1981)
In the previous code, the `*port` is assigned before `getsockname`, so the caller may be not able to get the actual port number assigned by system. Move the assigning of `*port` to be after `getsockname` to resolve the issue.
1 parent 8b41ea0 commit f4107a9

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

core/shared/platform/common/posix/posix_socket.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,6 @@ os_socket_bind(bh_socket_t socket, const char *host, int *port)
156156
goto fail;
157157
}
158158

159-
if (addr.ss_family == AF_INET) {
160-
*port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
161-
}
162-
else {
163-
#ifdef IPPROTO_IPV6
164-
*port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
165-
#else
166-
goto fail;
167-
#endif
168-
}
169-
170159
ret = fcntl(socket, F_SETFD, FD_CLOEXEC);
171160
if (ret < 0) {
172161
goto fail;
@@ -187,6 +176,17 @@ os_socket_bind(bh_socket_t socket, const char *host, int *port)
187176
goto fail;
188177
}
189178

179+
if (addr.ss_family == AF_INET) {
180+
*port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
181+
}
182+
else {
183+
#ifdef IPPROTO_IPV6
184+
*port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
185+
#else
186+
goto fail;
187+
#endif
188+
}
189+
190190
return BHT_OK;
191191

192192
fail:

0 commit comments

Comments
 (0)