@@ -88,15 +88,31 @@ Java_tel_schich_libdatachannel_LibDataChannelNative_rtcCreatePeerConnection(JNIE
8888
8989 for (int i = 0 ; i < config .iceServersCount ; i ++ ) {
9090 serverStrings [i ] = (* env )-> GetObjectArrayElement (env , iceServers , i );// we need a reference to release later
91+ if (serverStrings [i ] == NULL ) {
92+ for (int j = 0 ; j < i ; j ++ ) {
93+ if (config .iceServers [j ] != NULL ) {
94+ (* env )-> ReleaseStringUTFChars (env , serverStrings [j ], config .iceServers [j ]);
95+ }
96+ (* env )-> DeleteLocalRef (env , serverStrings [j ]);
97+ }
98+ free (config .iceServers );
99+ free (serverStrings );
100+ throw_native_exception (env , "iceServers must not contain null values" );
101+ return EXCEPTION_THROWN ;
102+ }
91103 config .iceServers [i ] = (* env )-> GetStringUTFChars (env , serverStrings [i ], NULL );
92104 if (config .iceServers [i ] == NULL ) {
93105 // release everything and throw an exception
94- for (int j = 0 ; j < i ; j ++ ) {
95- (* env )-> ReleaseStringUTFChars (env , serverStrings [j ], config .iceServers [j ]);
106+ for (int j = 0 ; j <= i ; j ++ ) {
107+ if (j < i && config .iceServers [j ] != NULL ) {
108+ (* env )-> ReleaseStringUTFChars (env , serverStrings [j ], config .iceServers [j ]);
109+ }
110+ if (serverStrings [j ] != NULL ) {
111+ (* env )-> DeleteLocalRef (env , serverStrings [j ]);
112+ }
96113 }
97114 free (config .iceServers );
98115 free (serverStrings );
99- throw_native_exception (env , "Failed to get ice server string!" );
100116 return EXCEPTION_THROWN ;
101117 }
102118 }
@@ -105,9 +121,34 @@ Java_tel_schich_libdatachannel_LibDataChannelNative_rtcCreatePeerConnection(JNIE
105121
106122 if (proxyServer != NULL ) {
107123 config .proxyServer = (* env )-> GetStringUTFChars (env , proxyServer , NULL );
124+ if (config .proxyServer == NULL ) {
125+ if (config .iceServers != NULL && config .iceServersCount > 0 ) {
126+ for (int i = 0 ; i < config .iceServersCount ; i ++ ) {
127+ (* env )-> ReleaseStringUTFChars (env , serverStrings [i ], config .iceServers [i ]);
128+ (* env )-> DeleteLocalRef (env , serverStrings [i ]);
129+ }
130+ free (config .iceServers );
131+ free (serverStrings );
132+ }
133+ return EXCEPTION_THROWN ;
134+ }
108135 }
109136 if (bindAddress != NULL ) {
110137 config .bindAddress = (* env )-> GetStringUTFChars (env , bindAddress , NULL );
138+ if (config .bindAddress == NULL ) {
139+ if (proxyServer != NULL ) {
140+ (* env )-> ReleaseStringUTFChars (env , proxyServer , config .proxyServer );
141+ }
142+ if (config .iceServers != NULL && config .iceServersCount > 0 ) {
143+ for (int i = 0 ; i < config .iceServersCount ; i ++ ) {
144+ (* env )-> ReleaseStringUTFChars (env , serverStrings [i ], config .iceServers [i ]);
145+ (* env )-> DeleteLocalRef (env , serverStrings [i ]);
146+ }
147+ free (config .iceServers );
148+ free (serverStrings );
149+ }
150+ return EXCEPTION_THROWN ;
151+ }
111152 }
112153
113154 const jint result = (jint ) rtcCreatePeerConnection (& config );
@@ -120,10 +161,9 @@ Java_tel_schich_libdatachannel_LibDataChannelNative_rtcCreatePeerConnection(JNIE
120161 }
121162
122163 if (config .iceServers != NULL && config .iceServersCount > 0 ) {
123- if (serverStrings != NULL ) {
124- for (int i = 0 ; i < config .iceServersCount ; i ++ ) {
125- (* env )-> ReleaseStringUTFChars (env , serverStrings [i ], config .iceServers [i ]);
126- }
164+ for (int i = 0 ; i < config .iceServersCount ; i ++ ) {
165+ (* env )-> ReleaseStringUTFChars (env , serverStrings [i ], config .iceServers [i ]);
166+ (* env )-> DeleteLocalRef (env , serverStrings [i ]);
127167 }
128168 free (config .iceServers );
129169 free (serverStrings );
@@ -141,15 +181,18 @@ JNIEXPORT jint JNICALL
141181Java_tel_schich_libdatachannel_LibDataChannelNative_rtcDeletePeerConnection (JNIEnv * env , jclass clazz ,
142182 const jint peerHandle ) {
143183 struct jvm_callback * callback = rtcGetUserPointer (peerHandle );
144- if (callback != NULL ) {
184+ jint result = rtcDeletePeerConnection (peerHandle );
185+ if ((result == RTC_ERR_SUCCESS || result == RTC_ERR_INVALID ) && callback != NULL ) {
145186 free_callback (env , callback );
146187 }
147-
148- return rtcDeletePeerConnection (peerHandle );
188+ return result ;
149189}
150190
151191
152192JNIEXPORT jint JNICALL Java_tel_schich_libdatachannel_LibDataChannelNative_rtcSetLocalDescription (JNIEnv * env , jclass clazz , const jint peerHandle , const jstring type ) {
193+ if (type == NULL ) {
194+ return WRAP_ERROR (env , rtcSetLocalDescription (peerHandle , NULL ));
195+ }
153196 const char * c_type = (* env )-> GetStringUTFChars (env , type , NULL );
154197 if (c_type == NULL ) {
155198 THROW_FAILED_GET_STR (env , type );
@@ -169,20 +212,29 @@ JNIEXPORT jstring JNICALL Java_tel_schich_libdatachannel_LibDataChannelNative_rt
169212}
170213
171214JNIEXPORT jint JNICALL Java_tel_schich_libdatachannel_LibDataChannelNative_rtcSetRemoteDescription (JNIEnv * env , jclass clazz , const jint peerHandle , const jstring sdp , const jstring type ) {
215+ if (sdp == NULL ) {
216+ throw_native_exception (env , "sdp must not be null" );
217+ return EXCEPTION_THROWN ;
218+ }
172219 const char * c_sdp = (* env )-> GetStringUTFChars (env , sdp , NULL );
173220 if (c_sdp == NULL ) {
174221 THROW_FAILED_GET_STR (env , sdp );
175222 return EXCEPTION_THROWN ;
176223 }
177- const char * c_type = (* env )-> GetStringUTFChars (env , type , NULL );
178- if (c_type == NULL ) {
179- (* env )-> ReleaseStringUTFChars (env , sdp , c_sdp );
180- THROW_FAILED_GET_STR (env , type );
181- return EXCEPTION_THROWN ;
224+ const char * c_type = NULL ;
225+ if (type != NULL ) {
226+ c_type = (* env )-> GetStringUTFChars (env , type , NULL );
227+ if (c_type == NULL ) {
228+ (* env )-> ReleaseStringUTFChars (env , sdp , c_sdp );
229+ THROW_FAILED_GET_STR (env , type );
230+ return EXCEPTION_THROWN ;
231+ }
182232 }
183233 const int result = WRAP_ERROR (env , rtcSetRemoteDescription (peerHandle , c_sdp , c_type ));
184234 (* env )-> ReleaseStringUTFChars (env , sdp , c_sdp );
185- (* env )-> ReleaseStringUTFChars (env , type , c_type );
235+ if (c_type != NULL ) {
236+ (* env )-> ReleaseStringUTFChars (env , type , c_type );
237+ }
186238 return result ;
187239}
188240
0 commit comments