RFR: 8261949: fileStream::readln returns incorrect line string

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

RFR: 8261949: fileStream::readln returns incorrect line string

Yang Yi
When the last line does not contain a NEWLINE character, fileStream::readln would read
truncated line string:

$ cat file_content:
AA
BB
CC<EOF>

fileStream::readlnresult:
"AA"
"BB"
"C"

This patch address this problem, it works for Posix and Windows since the last character
of these systems is always '\n'.

-------------

Commit messages:
 - fileStream::readln returns incorrect line string

Changes: https://git.openjdk.java.net/jdk/pull/2626/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2626&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8261949
  Stats: 5 lines in 1 file changed: 3 ins; 0 del; 2 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2626.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2626/head:pull/2626

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string

Daniel D.Daugherty
On Thu, 18 Feb 2021 12:31:29 GMT, Yang Yi <[hidden email]> wrote:

> When the last line does not contain a NEWLINE character, fileStream::readln would read
> truncated line string:
>
> $ cat file_content:
> AA
> BB
> CC<EOF>
>
> fileStream::readln result:
> "AA"
> "BB"
> "C"
>
> This patch address this problem, it works for Posix and Windows since the last character
> of these systems is always '\n'.

Changes requested by dcubed (Reviewer).

src/hotspot/share/utilities/ostream.cpp line 593:

> 591:     ret = ::fgets(data, count, _file);
> 592:     // Get rid of annoying \n char only if it presents, it works for Posix
> 593:     // and Windows since the last character of these systems is always '\n'

s/of these/on these/
Please end the comment with a period.

src/hotspot/share/utilities/ostream.cpp line 595:

> 593:     // and Windows since the last character of these systems is always '\n'
> 594:     if (data[::strlen(data)-1] == '\n') {
> 595:       data[::strlen(data)-1] = '\0';

Perhaps:
    size_t last_char = ::strlen(data) - 1;
    if (last_char >= 0 && data[last_char] == '\n') {
      data[last_char] = '\0';

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string

David Holmes-2
In reply to this post by Yang Yi
On Thu, 18 Feb 2021 12:31:29 GMT, Yang Yi <[hidden email]> wrote:

> When the last line does not contain a NEWLINE character, fileStream::readln would read
> truncated line string:
>
> $ cat file_content:
> AA
> BB
> CC<EOF>
>
> fileStream::readln result:
> "AA"
> "BB"
> "C"
>
> This patch address this problem, it works for Posix and Windows since the last character
> of these systems is always '\n'.

Changes requested by dholmes (Reviewer).

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string

David Holmes-2
In reply to this post by Daniel D.Daugherty
On Thu, 18 Feb 2021 16:15:11 GMT, Daniel D. Daugherty <[hidden email]> wrote:

>> When the last line does not contain a NEWLINE character, fileStream::readln would read
>> truncated line string:
>>
>> $ cat file_content:
>> AA
>> BB
>> CC<EOF>
>>
>> fileStream::readln result:
>> "AA"
>> "BB"
>> "C"
>>
>> This patch address this problem, it works for Posix and Windows since the last character
>> of these systems is always '\n'.
>
> src/hotspot/share/utilities/ostream.cpp line 593:
>
>> 591:     ret = ::fgets(data, count, _file);
>> 592:     // Get rid of annoying \n char only if it presents, it works for Posix
>> 593:     // and Windows since the last character of these systems is always '\n'
>
> s/of these/on these/
> Please end the comment with a period.

I suggest simply:

// Get rid of \n char if it is present

> src/hotspot/share/utilities/ostream.cpp line 595:
>
>> 593:     // and Windows since the last character of these systems is always '\n'
>> 594:     if (data[::strlen(data)-1] == '\n') {
>> 595:       data[::strlen(data)-1] = '\0';
>
> Perhaps:
>     size_t last_char = ::strlen(data) - 1;
>     if (last_char >= 0 && data[last_char] == '\n') {
>       data[last_char] = '\0';

@dcubed-ojdk size_t is unsigned so by definition always >= 0. :) I suggest:
`size_t len = ::strlen(data);`
`if (len > 0 && data[len - 1] == '\n') {`
`  data[len - 1] = '\0';`
`}`

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

Yang Yi
In reply to this post by Yang Yi
> When the last line does not contain a NEWLINE character, fileStream::readln would read
> truncated line string:
>
> $ cat file_content:
> AA
> BB
> CC<EOF>
>
> fileStream::readln result:
> "AA"
> "BB"
> "C"
>
> This patch address this problem, it works for Posix and Windows since the last character
> of these systems is always '\n'.

Yang Yi has updated the pull request incrementally with one additional commit since the last revision:

  tweak

-------------

Changes:
  - all: https://git.openjdk.java.net/jdk/pull/2626/files
  - new: https://git.openjdk.java.net/jdk/pull/2626/files/f7bd8206..2e816b2e

Webrevs:
 - full: https://webrevs.openjdk.java.net/?repo=jdk&pr=2626&range=01
 - incr: https://webrevs.openjdk.java.net/?repo=jdk&pr=2626&range=00-01

  Stats: 4 lines in 1 file changed: 0 ins; 0 del; 4 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2626.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2626/head:pull/2626

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

Yang Yi
In reply to this post by David Holmes-2
On Fri, 19 Feb 2021 01:22:51 GMT, David Holmes <[hidden email]> wrote:

>> src/hotspot/share/utilities/ostream.cpp line 593:
>>
>>> 591:     ret = ::fgets(data, count, _file);
>>> 592:     // Get rid of annoying \n char only if it presents, it works for Posix
>>> 593:     // and Windows since the last character of these systems is always '\n'
>>
>> s/of these/on these/
>> Please end the comment with a period.
>
> I suggest simply:
>
> // Get rid of \n char if it is present

Changed.

>> src/hotspot/share/utilities/ostream.cpp line 595:
>>
>>> 593:     // and Windows since the last character of these systems is always '\n'
>>> 594:     if (data[::strlen(data)-1] == '\n') {
>>> 595:       data[::strlen(data)-1] = '\0';
>>
>> Perhaps:
>>     size_t last_char = ::strlen(data) - 1;
>>     if (last_char >= 0 && data[last_char] == '\n') {
>>       data[last_char] = '\0';
>
> @dcubed-ojdk size_t is unsigned so by definition always >= 0. :) I suggest:
> `size_t len = ::strlen(data);`
> `if (len > 0 && data[len - 1] == '\n') {`
> `  data[len - 1] = '\0';`
> `}`

Make sense. Changed.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

Daniel D.Daugherty
In reply to this post by Yang Yi
On Fri, 19 Feb 2021 02:08:52 GMT, Yang Yi <[hidden email]> wrote:

>> When the last line does not contain a NEWLINE character, fileStream::readln would read
>> truncated line string:
>>
>> $ cat file_content:
>> AA
>> BB
>> CC<EOF>
>>
>> fileStream::readln result:
>> "AA"
>> "BB"
>> "C"
>>
>> This patch address this problem, it works for Posix and Windows since the last character
>> of these systems is always '\n'.
>
> Yang Yi has updated the pull request incrementally with one additional commit since the last revision:
>
>   tweak

I like David's suggestion better than mine.
Thumbs up.

-------------

Marked as reviewed by dcubed (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

David Holmes-2
In reply to this post by Yang Yi
On Fri, 19 Feb 2021 02:08:52 GMT, Yang Yi <[hidden email]> wrote:

>> When the last line does not contain a NEWLINE character, fileStream::readln would read
>> truncated line string:
>>
>> $ cat file_content:
>> AA
>> BB
>> CC<EOF>
>>
>> fileStream::readln result:
>> "AA"
>> "BB"
>> "C"
>>
>> This patch address this problem, it works for Posix and Windows since the last character
>> of these systems is always '\n'.
>
> Yang Yi has updated the pull request incrementally with one additional commit since the last revision:
>
>   tweak

Looks good!

Thanks,
David

-------------

Marked as reviewed by dholmes (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

Yang Yi
In reply to this post by Daniel D.Daugherty
On Fri, 19 Feb 2021 02:55:56 GMT, Daniel D. Daugherty <[hidden email]> wrote:

>> Yang Yi has updated the pull request incrementally with one additional commit since the last revision:
>>
>>   tweak
>
> I like David's suggestion better than mine.
> Thumbs up.

Thanks @dcubed-ojdk  @dholmes-ora  for reviews!

Would you be able to sponsor this patch? I'm not a committer so that I can not push this commit directly.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Integrated: 8261949: fileStream::readln returns incorrect line string

Yang Yi
In reply to this post by Yang Yi
On Thu, 18 Feb 2021 12:31:29 GMT, Yang Yi <[hidden email]> wrote:

> When the last line does not contain a NEWLINE character, fileStream::readln would read
> truncated line string:
>
> $ cat file_content:
> AA
> BB
> CC<EOF>
>
> fileStream::readln result:
> "AA"
> "BB"
> "C"
>
> This patch address this problem, it works for Posix and Windows since the last character
> of these systems is always '\n'.

This pull request has now been integrated.

Changeset: 2b555015
Author:    Yang Yi <[hidden email]>
Committer: Jie Fu <[hidden email]>
URL:       https://git.openjdk.java.net/jdk/commit/2b555015
Stats:     5 lines in 1 file changed: 3 ins; 0 del; 2 mod

8261949: fileStream::readln returns incorrect line string

Reviewed-by: dcubed, dholmes

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626
Reply | Threaded
Open this post in threaded view
|

Re: RFR: 8261949: fileStream::readln returns incorrect line string [v2]

Yang Yi
In reply to this post by Yang Yi
On Mon, 22 Feb 2021 02:12:02 GMT, Yang Yi <[hidden email]> wrote:

>> I like David's suggestion better than mine.
>> Thumbs up.
>
> Thanks @dcubed-ojdk  @dholmes-ora  for reviews!
>
> Would you be able to sponsor this patch? I'm not a committer so that I can not push this commit directly.

@DamonFool I'm grateful for your sponsorship.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2626