Skip to content

Commit c2ef717

Browse files
Gerdriaan Mulderlangemeijer
authored andcommitted
Fix [s]size_t types in ssh2_scp_send, also check written bytes in ssh2_scp_recv
1 parent fec7b79 commit c2ef717

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

ssh2_fopen_wrappers.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ PHP_FUNCTION(ssh2_scp_recv)
11471147

11481148
while (sb.st_size) {
11491149
char buffer[8192];
1150-
int bytes_read;
1150+
ssize_t bytes_read, bytes_written;
11511151

11521152
bytes_read = libssh2_channel_read(remote_file, buffer, sb.st_size > 8192 ? 8192 : sb.st_size);
11531153
if (bytes_read < 0) {
@@ -1156,7 +1156,19 @@ PHP_FUNCTION(ssh2_scp_recv)
11561156
php_stream_close(local_file);
11571157
RETURN_FALSE;
11581158
}
1159-
php_stream_write(local_file, buffer, bytes_read);
1159+
bytes_written = php_stream_write(local_file, buffer, bytes_read);
1160+
if (bytes_written < 0) {
1161+
php_error_docref(NULL, E_WARNING, "Error writing to local file");
1162+
libssh2_channel_free(remote_file);
1163+
php_stream_close(local_file);
1164+
RETURN_FALSE;
1165+
}
1166+
if (bytes_read != bytes_written) {
1167+
php_error_docref(NULL, E_WARNING, "Mismatch in bytes read from remote file and bytes written to local file");
1168+
libssh2_channel_free(remote_file);
1169+
php_stream_close(local_file);
1170+
RETURN_FALSE;
1171+
}
11601172
sb.st_size -= bytes_read;
11611173
}
11621174

@@ -1220,9 +1232,9 @@ PHP_FUNCTION(ssh2_scp_send)
12201232
while (ssb.sb.st_size) {
12211233
char buffer[8192];
12221234
size_t toread = MIN(8192, ssb.sb.st_size);
1223-
size_t bytesread = php_stream_read(local_file, buffer, toread);
1235+
ssize_t bytesread = php_stream_read(local_file, buffer, toread);
12241236
size_t sent = 0;
1225-
size_t justsent = 0;
1237+
ssize_t justsent = 0;
12261238

12271239
if (bytesread <= 0 || bytesread > toread) {
12281240
php_error_docref(NULL, E_WARNING, "Failed copying file 2");

0 commit comments

Comments
 (0)