@@ -656,12 +656,13 @@ fd_table_insert_fd(struct fd_table *ft, int in, __wasi_filetype_t type,
656
656
REQUIRES_UNLOCKED (ft - > lock )
657
657
{
658
658
struct fd_object * fo ;
659
- __wasi_errno_t error = fd_object_new (type , & fo );
660
659
660
+ __wasi_errno_t error = fd_object_new (type , & fo );
661
661
if (error != 0 ) {
662
662
close (in );
663
663
return error ;
664
664
}
665
+
665
666
fo -> number = in ;
666
667
if (type == __WASI_FILETYPE_DIRECTORY ) {
667
668
if (!mutex_init (& fo -> directory .lock )) {
@@ -2808,33 +2809,42 @@ wasi_ssp_sock_accept(
2808
2809
__wasi_filetype_t wasi_type ;
2809
2810
__wasi_rights_t max_base , max_inheriting ;
2810
2811
struct fd_object * fo ;
2811
- bh_socket_t new_sock ;
2812
+ bh_socket_t new_sock = -1 ;
2812
2813
int ret ;
2813
2814
__wasi_errno_t error =
2814
2815
fd_object_get (curfds , & fo , fd , __WASI_RIGHT_SOCK_ACCEPT , 0 );
2815
- if (error != __WASI_ESUCCESS )
2816
- return error ;
2816
+ if (error != __WASI_ESUCCESS ) {
2817
+ goto fail ;
2818
+ }
2817
2819
2818
2820
ret = os_socket_accept (fd_number (fo ), & new_sock , NULL , NULL );
2819
2821
fd_object_release (fo );
2820
- if (ret == BHT_ERROR )
2821
- return convert_errno (errno );
2822
+ if (BHT_OK != ret ) {
2823
+ error = convert_errno (errno );
2824
+ goto fail ;
2825
+ }
2822
2826
2823
2827
error = fd_determine_type_rights (new_sock , & wasi_type , & max_base ,
2824
2828
& max_inheriting );
2825
2829
if (error != __WASI_ESUCCESS ) {
2826
- os_socket_close (ret );
2827
- return error ;
2830
+ goto fail ;
2828
2831
}
2829
2832
2830
2833
error = fd_table_insert_fd (curfds , new_sock , wasi_type , max_base ,
2831
2834
max_inheriting , fd_new );
2832
2835
if (error != __WASI_ESUCCESS ) {
2833
- os_socket_close (ret );
2834
- return error ;
2836
+ /* released in fd_table_insert_fd() */
2837
+ new_sock = -1 ;
2838
+ goto fail ;
2835
2839
}
2836
2840
2837
2841
return __WASI_ESUCCESS ;
2842
+
2843
+ fail :
2844
+ if (-1 != new_sock ) {
2845
+ os_socket_close (new_sock );
2846
+ }
2847
+ return error ;
2838
2848
}
2839
2849
2840
2850
__wasi_errno_t
@@ -2898,7 +2908,7 @@ wasi_ssp_sock_bind(
2898
2908
2899
2909
ret = os_socket_bind (fd_number (fo ), buf , & port );
2900
2910
fd_object_release (fo );
2901
- if (ret == BHT_ERROR ) {
2911
+ if (BHT_OK != ret ) {
2902
2912
return convert_errno (errno );
2903
2913
}
2904
2914
@@ -2931,7 +2941,7 @@ wasi_ssp_sock_connect(
2931
2941
2932
2942
ret = os_socket_connect (fd_number (fo ), buf , addr -> addr .ip4 .port );
2933
2943
fd_object_release (fo );
2934
- if (ret == BHT_ERROR ) {
2944
+ if (BHT_OK != ret ) {
2935
2945
return convert_errno (errno );
2936
2946
}
2937
2947
@@ -2954,7 +2964,7 @@ wasi_ssp_sock_listen(
2954
2964
2955
2965
ret = os_socket_listen (fd_number (fo ), backlog );
2956
2966
fd_object_release (fo );
2957
- if (ret == BHT_ERROR ) {
2967
+ if (BHT_OK != ret ) {
2958
2968
return convert_errno (errno );
2959
2969
}
2960
2970
@@ -2985,7 +2995,7 @@ wasi_ssp_sock_open(
2985
2995
tcp_or_udp = SOCKET_DGRAM == socktype ? 0 : 1 ;
2986
2996
2987
2997
ret = os_socket_create (& sock , tcp_or_udp );
2988
- if (ret == BHT_ERROR ) {
2998
+ if (BHT_OK != ret ) {
2989
2999
return convert_errno (errno );
2990
3000
}
2991
3001
@@ -3007,7 +3017,6 @@ wasi_ssp_sock_open(
3007
3017
error = fd_table_insert_fd (curfds , sock , wasi_type , max_base ,
3008
3018
max_inheriting , sockfd );
3009
3019
if (error != __WASI_ESUCCESS ) {
3010
- os_socket_close (sock );
3011
3020
return error ;
3012
3021
}
3013
3022
@@ -3032,7 +3041,7 @@ wasmtime_ssp_sock_recv(
3032
3041
3033
3042
ret = os_socket_recv (fd_number (fo ), buf , buf_len );
3034
3043
fd_object_release (fo );
3035
- if (ret == BHT_ERROR ) {
3044
+ if (BHT_OK != ret ) {
3036
3045
return convert_errno (errno );
3037
3046
}
3038
3047
@@ -3058,7 +3067,7 @@ wasmtime_ssp_sock_send(
3058
3067
3059
3068
ret = os_socket_send (fd_number (fo ), buf , buf_len );
3060
3069
fd_object_release (fo );
3061
- if (ret == BHT_ERROR ) {
3070
+ if (BHT_OK != ret ) {
3062
3071
return convert_errno (errno );
3063
3072
}
3064
3073
@@ -3083,7 +3092,7 @@ wasmtime_ssp_sock_shutdown(
3083
3092
3084
3093
ret = os_socket_shutdown (fd_number (fo ));
3085
3094
fd_object_release (fo );
3086
- if (ret == BHT_ERROR )
3095
+ if (BHT_OK != ret )
3087
3096
return convert_errno (errno );
3088
3097
3089
3098
return __WASI_ESUCCESS ;
0 commit comments