public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: Tom Tromey <tromey@redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org Subject: Re: java/3426: java.io.InputStreamReader, infinite loop Date: Fri, 29 Jun 2001 17:06:00 -0000 [thread overview] Message-ID: <20010630000601.8310.qmail@sourceware.cygnus.com> (raw) The following reply was made to PR libgcj/3426; it has been noted by GNATS. From: Tom Tromey <tromey@redhat.com> To: david-b@pacbell.net Cc: gcc-gnats@gcc.gnu.org Subject: Re: java/3426: java.io.InputStreamReader, infinite loop Date: 29 Jun 2001 18:20:47 -0600 Hi David. Could you try this untested patch and see if it helps this problem? If it doesn't work for you I will try to reproduce the failure myself and then I'll debug it for real. I'll also do that if you can't test it. (However, if I do have to do that it will have to be a lower priority...) 2001-06-29 Tom Tromey <tromey@redhat.com> Fix for PR libgcj/3426: * java/io/BufferedInputStream.java (refill): Now package-private. * java/io/InputStreamReader.java (ready): Simplified. (refill): New method. (read): Use it. Tom Index: java/io/BufferedInputStream.java =================================================================== RCS file: /cvs/gcc/gcc/libjava/java/io/BufferedInputStream.java,v retrieving revision 1.3 diff -u -r1.3 BufferedInputStream.java --- BufferedInputStream.java 2000/03/07 19:55:26 1.3 +++ BufferedInputStream.java 2001/06/29 23:56:54 @@ -1,4 +1,4 @@ -/* Copyright (C) 1998, 1999 Free Software Foundation +/* Copyright (C) 1998, 1999, 2001 Free Software Foundation This file is part of libgcj. @@ -131,7 +131,7 @@ return origN - n; } - private boolean refill() throws IOException + boolean refill() throws IOException { if (markpos < 0) count = pos = 0; Index: java/io/InputStreamReader.java =================================================================== RCS file: /cvs/gcc/gcc/libjava/java/io/InputStreamReader.java,v retrieving revision 1.8 diff -u -r1.8 InputStreamReader.java --- InputStreamReader.java 2001/02/09 01:54:38 1.8 +++ InputStreamReader.java 2001/06/29 23:56:54 @@ -44,6 +44,11 @@ private InputStreamReader(InputStream in, BytesToUnicode decoder) { + // FIXME: someone could pass in a BufferedInputStream whose buffer + // is smaller than the longest encoded character for this + // encoding. We will probably go into an infinite loop in this + // case. We probably ought to just have our own byte buffering + // here. this.in = in instanceof BufferedInputStream ? (BufferedInputStream) in : new BufferedInputStream(in); @@ -76,28 +81,11 @@ if (wpos < wcount) return true; - if (work == null) - { - work = new char[100]; - wpos = 0; - wcount = 0; - } - for (;;) - { - if (in.available() <= 0) - return false; - in.mark(1); - int b = in.read(); - if (b < 0) - return true; - in.reset(); - converter.setInput(in.buf, in.pos, in.count); - wpos = 0; - wcount = converter.read(work, 0, work.length); - in.skip(converter.inpos - in.pos); - if (wcount > 0) - return true; - } + + // According to the spec, an InputStreamReader is ready if its + // input buffer is not empty (above), or if bytes are + // available on the underlying byte stream. + return in.available () > 0; } } @@ -108,33 +96,23 @@ if (in == null) throw new IOException("Stream closed"); + if (length == 0) + return 0; + int wavail = wcount - wpos; - if (wavail > 0) - { - if (length > wavail) - length = wavail; - System.arraycopy(work, wpos, buf, offset, length); - wpos += length; - return length; - } - else + if (wavail <= 0) { - if (length == 0) - return 0; - for (;;) - { - in.mark(1); - int b = in.read(); - if (b < 0) - return -1; - in.reset(); - converter.setInput(in.buf, in.pos, in.count); - int count = converter.read (buf, offset, length); - in.skip(converter.inpos - in.pos); - if (count > 0) - return count; - } + // Nothing waiting, so refill our buffer. + if (! refill ()) + return -1; + wavail = wcount - wpos; } + + if (length > wavail) + length = wavail; + System.arraycopy(work, wpos, buf, offset, length); + wpos += length; + return length; } } @@ -146,24 +124,39 @@ throw new IOException("Stream closed"); int wavail = wcount - wpos; - if (wavail > 0) - return work[wpos++]; - if (work == null) + if (wavail <= 0) { - work = new char[100]; - wpos = 0; - wcount = 0; + // Nothing waiting, so refill our buffer. + if (! refill ()) + return -1; } - else if (wavail == 0) + + return work[wpos++]; + } + } + + // Read more bytes and convert them into the WORK buffer. + // Return false on EOF. + private boolean refill () throws IOException + { + wcount = wpos = 0; + for (;;) + { + // We have knowledge of the internals of BufferedInputStream + // here. Eww. + in.mark (0); + boolean r = ! in.refill (); + in.reset (); + if (! r) + return false; + converter.setInput(in.buf, in.pos, in.count); + int count = converter.read (work, wpos, work.length - wpos); + in.skip(converter.inpos - in.pos); + if (count > 0) { - wpos = 0; - wcount = 0; + wcount += count; + return true; } - int count = read(work, wpos, work.length-wpos); - if (count <= 0) - return -1; - wcount = wpos + count; - return work[wpos++]; } } }
next reply other threads:[~2001-06-29 17:06 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2001-06-29 17:06 Tom Tromey [this message] -- strict thread matches above, loose matches on Subject: below -- 2001-07-12 22:56 David Brownell 2001-07-12 22:36 Tom Tromey 2001-07-12 22:36 Tom Tromey 2001-07-12 21:36 Tom Tromey 2001-07-04 23:56 David Brownell 2001-07-04 23:46 David Brownell 2001-06-29 16:26 Tom Tromey 2001-06-29 15:46 Tom Tromey 2001-06-26 8:16 david-b
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20010630000601.8310.qmail@sourceware.cygnus.com \ --to=tromey@redhat.com \ --cc=gcc-prs@gcc.gnu.org \ --cc=nobody@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).