2015-02-11 16:42:36 |
Pierre-Olivier Benoit |
description |
When using fseek on an SSH2 stream, I will get unpredictable results.
The following sequence will fail :
- fopen text file on SSH2 stream
- fread 20 bytes
- fseek TO THE BEGINNING
- fgets /*should read first line but does not; instead reading half of Line 211 */
Removing the initial fread() will not cause the issue.
Also using fclose()/fopen() after the fread() will return the pointer to the beginning as well - but that is not a good workaround.
I was only able to reproduce it on 12.04.
The sample code below will work properly on 14.04 or even 10.10.
CORRECT OUTPUT ON 14.04:
administrator@srv-web1:~$ php VW-232.php
Starting tests...
Position: 0
Read...
Read bytes: Line 1
Line 2
Lin
Fseek...
Position: 0
*** NOTE: OUTPUT BELOW IS CORRECT ***
line 1: Line 1
line 2: Line 2
line 3: Line 3
Tests done
administrator@srv-web1:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
INCORRECT OUTPUT ON 12.04:
administrator@inetserver2:/home/pobenoit$ php VW-232.php
Starting tests...
Position: 0
Read...
Read bytes: Line 1
Line 2
Lin
Fseek...
Position: 0
*** NOTE: OUTPUT IS INCORRECT HERE ***
line 1: 1
line 2: Line 212
line 3: Line 213
Tests done
administrator@inetserver2:/home/pobenoit$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"
ProblemType: Bug
DistroRelease: Ubuntu 12.04
Package: libssh2-php 0.11.2-1
ProcVersionSignature: Ubuntu 3.2.0-70.105-generic 3.2.63
Uname: Linux 3.2.0-70-generic x86_64
ApportVersion: 2.0.1-0ubuntu17.8
Architecture: amd64
Date: Wed Feb 11 11:16:47 2015
InstallationMedia: Ubuntu-Server 12.04.1 LTS "Precise Pangolin" - Release amd64 (20120817.3)
MarkForUpload: True
ProcEnviron:
LANGUAGE=en_CA:en
TERM=xterm
PATH=(custom, no user)
LANG=en_CA.UTF-8
SHELL=/bin/bash
SourcePackage: php-ssh2
UpgradeStatus: No upgrade log present (probably fresh install) |
When using fseek on an SSH2 stream, I will get unpredictable results.
The following sequence will fail :
- fopen text file on SSH2 stream
- fread 20 bytes
- fseek TO THE BEGINNING
- fgets /*should read first line but does not; instead reading half of Line 211 */
Removing the initial fread() will not cause the issue.
Also using fclose()/fopen() after the fread() will return the pointer to the beginning as well - but that is not a good workaround.
I was only able to reproduce it on 12.04.
The sample code ATTACHED will work properly on 14.04 or even 10.10.
You will need to edit the constants at the beginning of the file to match your SSH server username and password. Also you need to drop the test file on that server.
CORRECT OUTPUT ON 14.04:
administrator@srv-web1:~$ php VW-232.php
Starting tests...
Position: 0
Read...
Read bytes: Line 1
Line 2
Lin
Fseek...
Position: 0
*** NOTE: OUTPUT BELOW IS CORRECT ***
line 1: Line 1
line 2: Line 2
line 3: Line 3
Tests done
administrator@srv-web1:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
INCORRECT OUTPUT ON 12.04:
administrator@inetserver2:/home/pobenoit$ php VW-232.php
Starting tests...
Position: 0
Read...
Read bytes: Line 1
Line 2
Lin
Fseek...
Position: 0
*** NOTE: OUTPUT IS INCORRECT HERE ***
line 1: 1
line 2: Line 212
line 3: Line 213
Tests done
administrator@inetserver2:/home/pobenoit$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"
ProblemType: Bug
DistroRelease: Ubuntu 12.04
Package: libssh2-php 0.11.2-1
ProcVersionSignature: Ubuntu 3.2.0-70.105-generic 3.2.63
Uname: Linux 3.2.0-70-generic x86_64
ApportVersion: 2.0.1-0ubuntu17.8
Architecture: amd64
Date: Wed Feb 11 11:16:47 2015
InstallationMedia: Ubuntu-Server 12.04.1 LTS "Precise Pangolin" - Release amd64 (20120817.3)
MarkForUpload: True
ProcEnviron:
LANGUAGE=en_CA:en
TERM=xterm
PATH=(custom, no user)
LANG=en_CA.UTF-8
SHELL=/bin/bash
SourcePackage: php-ssh2
UpgradeStatus: No upgrade log present (probably fresh install) |
|