@@ -817,6 +817,11 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_
817817 if (ftp == NULL ) {
818818 return false;
819819 }
820+ if (ftp -> in_use ) {
821+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
822+ return false;
823+ }
824+ ftp -> in_use = true;
820825 if (!ftp_type (ftp , type )) {
821826 goto bail ;
822827 }
@@ -913,9 +918,11 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_
913918 goto bail ;
914919 }
915920
921+ ftp -> in_use = false;
916922 return true;
917923bail :
918924 data_close (ftp );
925+ ftp -> in_use = false;
919926 return false;
920927}
921928
@@ -1000,6 +1007,11 @@ bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream
10001007 if (ftp == NULL ) {
10011008 return false;
10021009 }
1010+ if (ftp -> in_use ) {
1011+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
1012+ return false;
1013+ }
1014+ ftp -> in_use = true;
10031015 if (!ftp_type (ftp , type )) {
10041016 goto bail ;
10051017 }
@@ -1040,9 +1052,11 @@ bool ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream
10401052 if (!ftp_getresp (ftp ) || (ftp -> resp != 226 && ftp -> resp != 250 && ftp -> resp != 200 )) {
10411053 goto bail ;
10421054 }
1055+ ftp -> in_use = false;
10431056 return true;
10441057bail :
10451058 data_close (ftp );
1059+ ftp -> in_use = false;
10461060 return false;
10471061}
10481062
@@ -1053,6 +1067,11 @@ bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stre
10531067 if (ftp == NULL ) {
10541068 return false;
10551069 }
1070+ if (ftp -> in_use ) {
1071+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
1072+ return false;
1073+ }
1074+ ftp -> in_use = true;
10561075 if (!ftp_type (ftp , type )) {
10571076 goto bail ;
10581077 }
@@ -1080,9 +1099,11 @@ bool ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stre
10801099 if (!ftp_getresp (ftp ) || (ftp -> resp != 226 && ftp -> resp != 250 && ftp -> resp != 200 )) {
10811100 goto bail ;
10821101 }
1102+ ftp -> in_use = false;
10831103 return true;
10841104bail :
10851105 data_close (ftp );
1106+ ftp -> in_use = false;
10861107 return false;
10871108}
10881109
@@ -1939,6 +1960,10 @@ static char** ftp_genlist(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len,
19391960 char * * entry ;
19401961 char * text ;
19411962
1963+ if (ftp -> in_use ) {
1964+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
1965+ return NULL ;
1966+ }
19421967
19431968 if ((tmpstream = php_stream_fopen_tmpfile ()) == NULL ) {
19441969 php_error_docref (NULL , E_WARNING , "Unable to create temporary file. Check permissions in temporary files directory." );
@@ -2037,6 +2062,11 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz
20372062 return PHP_FTP_FAILED ;
20382063 }
20392064
2065+ if (ftp -> in_use ) {
2066+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
2067+ return PHP_FTP_FAILED ;
2068+ }
2069+
20402070 if (ftp -> data != NULL ) {
20412071 /* If there is a transfer in action, abort it.
20422072 * If we don't, we get an invalid state and memory leaks when the new connection gets opened. */
@@ -2101,11 +2131,17 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
21012131
21022132 data = ftp -> data ;
21032133
2134+ if (ftp -> in_use ) {
2135+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
2136+ return PHP_FTP_FAILED ;
2137+ }
2138+
21042139 /* check if there is already more data */
21052140 if (!data_available (ftp , data -> fd , false)) {
21062141 return PHP_FTP_MOREDATA ;
21072142 }
21082143
2144+ ftp -> in_use = true;
21092145 type = ftp -> type ;
21102146
21112147 lastch = ftp -> lastch ;
@@ -2129,6 +2165,7 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
21292165 }
21302166
21312167 ftp -> lastch = lastch ;
2168+ ftp -> in_use = false;
21322169 return PHP_FTP_MOREDATA ;
21332170 }
21342171
@@ -2143,9 +2180,11 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
21432180 }
21442181
21452182 ftp -> nb = false;
2183+ ftp -> in_use = false;
21462184 return PHP_FTP_FINISHED ;
21472185bail :
21482186 ftp -> nb = false;
2187+ ftp -> in_use = false;
21492188 data_close (ftp );
21502189 return PHP_FTP_FAILED ;
21512190}
@@ -2158,6 +2197,10 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea
21582197 if (ftp == NULL ) {
21592198 return 0 ;
21602199 }
2200+ if (ftp -> in_use ) {
2201+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
2202+ return PHP_FTP_FAILED ;
2203+ }
21612204 if (!ftp_type (ftp , type )) {
21622205 goto bail ;
21632206 }
@@ -2201,16 +2244,24 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea
22012244
22022245int ftp_nb_continue_write (ftpbuf_t * ftp )
22032246{
2247+ if (ftp -> in_use ) {
2248+ php_error_docref (NULL , E_WARNING , "FTP\\Connection is already in use" );
2249+ return PHP_FTP_FAILED ;
2250+ }
2251+
22042252 /* check if we can write more data */
22052253 if (!data_writeable (ftp , ftp -> data -> fd )) {
22062254 return PHP_FTP_MOREDATA ;
22072255 }
22082256
2257+ ftp -> in_use = true;
2258+
22092259 if (ftp_send_stream_to_data_socket (ftp , ftp -> data , ftp -> stream , ftp -> type , true) != SUCCESS ) {
22102260 goto bail ;
22112261 }
22122262
22132263 if (!php_stream_eof (ftp -> stream )) {
2264+ ftp -> in_use = false;
22142265 return PHP_FTP_MOREDATA ;
22152266 }
22162267
@@ -2220,9 +2271,11 @@ int ftp_nb_continue_write(ftpbuf_t *ftp)
22202271 goto bail ;
22212272 }
22222273 ftp -> nb = false;
2274+ ftp -> in_use = false;
22232275 return PHP_FTP_FINISHED ;
22242276bail :
22252277 data_close (ftp );
22262278 ftp -> nb = false;
2279+ ftp -> in_use = false;
22272280 return PHP_FTP_FAILED ;
22282281}
0 commit comments