Bug 79941 - com.sun.star.io.TextInputStream incorrectly detects permanent EOF when working with partial reads from readSomeBytes
Summary: com.sun.star.io.TextInputStream incorrectly detects permanent EOF when workin...
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: sdk (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium major
Assignee: Stephan Bergmann
URL:
Whiteboard: target:4.5.0
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-12 12:26 UTC by Tomasz Sztejka
Modified: 2014-11-24 21:07 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tomasz Sztejka 2014-06-12 12:26:36 UTC
When "com.sun.star.io.TextInputStream" service is used with XInputStream which return partial read (that is reads less bytes than required) from its readSomeBytes com.sun.star.io.TextInputStream assumes that it's a permanent End Of File condition and never attempts to read any data again.

This is an important fault when reading data from dynamic resources built on request like network sockets or similar connections - in may case a remote measuring equipment.

For an example one may attempt to send a request to a server using self made XOutputStream implementation and then may like to retrieve response with com.sun.star.io.TextInputStream.readLine

The com.sun.star.io.TextInputStream.readLine will request readSomeBytes(...., 256), what means it is trying to read some data ahead. Obviously if server response is shorter than that XInputStream  will either timeout or return only what can be returned immediately. The result will be that readSomeBytes will return less bytes than requires. Or even a zero. It doesn't mean link is broken, it just has no more data at the moment.

The current implementation of com.sun.star.io.TextInputStream.readLine fills data in a buffer and detects that this is an eof. I sets mbReachedEOF in TextInputStream.cxx to true.

line 295:
        sal_Int32 nRead = mxStream->readSomeBytes( mSeqSource, nBytesToRead );
        sal_Int32 nTotalRead = nRead;
        if( nRead < nBytesToRead )
            mbReachedEOF = true;

Later any attempt to call implReadString returns without updating buffer 
content due to check in line 214:

  // Already reached EOF? Then we can't read any more
            if( mbReachedEOF )
                break;


Effectively it prevents using TextInputStream to read anything from sources other than a fast, blocking read, local files.

Please, do not make EOF condition permanent. Personally I don't feel capable enough to tinker with this code and fix it. It seems than mbReachedEOF is tested in many places and there is too less comments for me to grasp what's going on.

I mark it as a "major" since this renders this service unusable. A work around is possible by writing own UNO service which doing it correctly.

Best regards,
  Tomasz Sztejka
Comment 1 Commit Notification 2014-11-21 17:33:06 UTC
Stephan Bergmann committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=7a0bfcbce147eff61dc9b7d243b571dd34110df8

fdo#79941: Properly handle short reads

It will be available in 4.4.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds
Affected users are encouraged to test the fix and report feedback.
Comment 2 Stephan Bergmann 2014-11-21 17:35:58 UTC
(In reply to Commit Notification from comment #1)
> It will be available in 4.4.0.

...should read "4.5.0."
Comment 3 Commit Notification 2014-11-24 21:07:34 UTC
Stephan Bergmann committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=ed30f63364883ffa7e0f496aa626ebd3e291965a

Unit test for fdo#79941 (handle short reads)

It will be available in 4.5.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds
Affected users are encouraged to test the fix and report feedback.