Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BufferedInputStream.read() for values bigger than 0x7f #1922

Merged
merged 1 commit into from Oct 7, 2020

Conversation

catap
Copy link
Contributor

@catap catap commented Oct 7, 2020

read() should return a value between -1 and 255 and -1 means EOF.

When buf(pos) contains value bigger than 0x7f for example 171.toByte and makes .toInt, it converts this value to -85 that can be regarded by enduser as EOF if he uses < 0 condition.

Unfortunately this bug hasn't got workaround on user side because when stream contains 0xff, the read() returns -1 and it is impossibly to distinguish the EOF and 0xff.

Copy link
Collaborator

@sjrd sjrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, other than the formatting issue.

Comment on lines 42 to 46
val inputArray = Array[Byte](0x85.toByte)
val arrayIn = new ByteArrayInputStream(inputArray)
val in = new BufferedInputStream(arrayIn)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correctly formatted according to scalafmt.

`read()` should return a value between `-1` and `255` and `-1` means `EOF`.

When `buf(pos)` contains value bigger than `0x7f` for example `171.toByte` and
makes `.toInt`, it converts this value to `-85` that can be regarded by enduser
as `EOF` if he uses `< 0` condition.

Unfortunately this bug hasn't got workaround on user side because when stream
contains `0xff`, the `read()` returns `-1` and it is impossibly to distinguish
the `EOF` and `0xff`.
@catap catap requested a review from sjrd October 7, 2020 10:03
@sjrd sjrd changed the title Fixed read value bigger than 0x7f via BufferedInputStream Fix BufferedInputStream.read() for values bigger than 0x7f Oct 7, 2020
@sjrd sjrd merged commit 8b4e409 into scala-native:master Oct 7, 2020
@catap catap deleted the BufferedInputStream-read branch October 7, 2020 14:07
@LeeTibbert
Copy link
Contributor

This has already been merged which is good.

@catap I should point out pending PRs #1624 & #1623. They deal with
I believe the exact same issue in DataInputStream and readLine(). You
may end up encountering the same problem they fix.

@ekrich
Copy link
Member

ekrich commented Oct 7, 2020

I was hoping that #1918 might be fixed by this but it isn't. Maybe a stream is reading passed the end of a buffer.

@LeeTibbert I don't think your fixes would hurt either.

vicopem pushed a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
…ive#1922)

`read()` should return a value between `-1` and `255` and `-1` means `EOF`.
vicopem added a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
vicopem added a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
@catap
Copy link
Contributor Author

catap commented Dec 10, 2020

I was wrong. It is possibly to overstep this issue on 0.4.0-M2.

The workaround:

class ScalaNativeFriendlyBufferedInputStream(in: InputStream) extends BufferedInputStream(in) {
  private val singleByte = new Array[Byte](1)

  override def read(): Int = synchronized {
    val r = read(singleByte)
    if (r < 0) r
    else singleByte(0).toInt & 0xff
  }
}

ekrich pushed a commit to ekrich/scala-native that referenced this pull request May 21, 2021
…ive#1922)

`read()` should return a value between `-1` and `255` and `-1` means `EOF`.
WojciechMazur pushed a commit to WojciechMazur/scala-native that referenced this pull request Aug 25, 2021
…ive#1922)

`read()` should return a value between `-1` and `255` and `-1` means `EOF`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants