Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7220B200C75 for ; Sun, 9 Apr 2017 20:25:50 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6FBE2160BA4; Sun, 9 Apr 2017 18:25:50 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9BF6E160BA8 for ; Sun, 9 Apr 2017 20:25:49 +0200 (CEST) Received: (qmail 9317 invoked by uid 500); 9 Apr 2017 18:25:48 -0000 Mailing-List: contact commits-help@lucenenet.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucene-net-dev@lucenenet.apache.org Delivered-To: mailing list commits@lucenenet.apache.org Received: (qmail 9222 invoked by uid 99); 9 Apr 2017 18:25:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Apr 2017 18:25:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 8ED9EE080F; Sun, 9 Apr 2017 18:25:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: nightowl888@apache.org To: commits@lucenenet.apache.org Date: Sun, 09 Apr 2017 18:25:53 -0000 Message-Id: <4e1320d38318451f82bd566767434a98@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [6/6] lucenenet git commit: BUG: Lucene.Net.Tests.Support.TestDataInputStream: Calculation of the array length was done on Windows, but due to line endings the size of the file may be different on other OS's, so changed the test to measure the file lengt archived-at: Sun, 09 Apr 2017 18:25:50 -0000 BUG: Lucene.Net.Tests.Support.TestDataInputStream: Calculation of the array length was done on Windows, but due to line endings the size of the file may be different on other OS's, so changed the test to measure the file length before declaring the array for the tests. Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/1b50f1dd Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/1b50f1dd Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/1b50f1dd Branch: refs/heads/api-work Commit: 1b50f1dd9f68544a0028b318c63ab490422aeaad Parents: a850302 Author: Shad Storhaug Authored: Mon Apr 10 01:18:42 2017 +0700 Committer: Shad Storhaug Committed: Mon Apr 10 01:18:42 2017 +0700 ---------------------------------------------------------------------- .../Support/TestDataInputStream.cs | 29 +++++++++++++++----- src/Lucene.Net/Support/DataInputStream.cs | 26 ++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1b50f1dd/src/Lucene.Net.Tests/Support/TestDataInputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs index cbb480a..5bc3816 100644 --- a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs +++ b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs @@ -14,22 +14,38 @@ namespace Lucene.Net.Support public void TestReadFully() { const string READFULLY_TEST_FILE = "ReadFully.txt"; - byte[] buffer = new byte[1367]; + int fileLength; + Stream @in; - Stream @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); + // Read one time to measure the length of the file (it may be different + // on different operating systems because of line endings) + using (@in = GetType().getResourceAsStream(READFULLY_TEST_FILE)) + { + using (var ms = new MemoryStream()) + { + @in.CopyTo(ms); + fileLength = ms.ToArray().Length; + } + } + + // Declare the buffer one byte too large + byte[] buffer = new byte[fileLength + 1]; + @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); DataInputStream dis; using (dis = new DataInputStream(@in)) { - // Read once for real - dis.ReadFully(buffer, 0, 1366); + // Read once for real (to the exact length) + dis.ReadFully(buffer, 0, fileLength); } // Read past the end of the stream - @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); + @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); dis = new DataInputStream(@in); bool caughtException = false; try { + // Using the buffer length (that is 1 byte too many) + // should generate EndOfStreamException. dis.ReadFully(buffer, 0, buffer.Length); } #pragma warning disable 168 @@ -46,8 +62,7 @@ namespace Lucene.Net.Support } // Ensure we get an IndexOutOfRangeException exception when length is negative - @in = GetType().GetTypeInfo(). - Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); + @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); dis = new DataInputStream(@in); caughtException = false; try http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1b50f1dd/src/Lucene.Net/Support/DataInputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/DataInputStream.cs b/src/Lucene.Net/Support/DataInputStream.cs index 8265735..c8d3894 100644 --- a/src/Lucene.Net/Support/DataInputStream.cs +++ b/src/Lucene.Net/Support/DataInputStream.cs @@ -50,13 +50,17 @@ namespace Lucene.Net.Support public void ReadFully(byte[] b, int off, int len) { if (len < 0) + { throw new IndexOutOfRangeException(); + } int n = 0; while (n < len) { int count = @in.Read(b, off + n, len - n); if (count == 0) + { throw new EndOfStreamException(); + } n += count; } } @@ -92,7 +96,9 @@ namespace Lucene.Net.Support { int ch = @in.ReadByte(); if (ch < 0) + { throw new EndOfStreamException(); + } return (ch != 0); } @@ -103,7 +109,9 @@ namespace Lucene.Net.Support { int ch = @in.ReadByte(); if (ch < 0) + { throw new EndOfStreamException(); + } return ch; } @@ -114,7 +122,9 @@ namespace Lucene.Net.Support { int ch = @in.ReadByte(); if (ch < 0) + { throw new EndOfStreamException(); + } return (byte)ch; } @@ -126,7 +136,9 @@ namespace Lucene.Net.Support int ch1 = @in.ReadByte(); int ch2 = @in.ReadByte(); if ((ch1 | ch2) < 0) + { throw new EndOfStreamException(); + } return (short)((ch1 << 8) + (ch2 << 0)); } @@ -138,7 +150,9 @@ namespace Lucene.Net.Support int ch1 = @in.ReadByte(); int ch2 = @in.ReadByte(); if ((ch1 | ch2) < 0) + { throw new EndOfStreamException(); + } return (ch1 << 8) + (ch2 << 0); } @@ -147,7 +161,9 @@ namespace Lucene.Net.Support int ch1 = @in.ReadByte(); int ch2 = @in.ReadByte(); if ((ch1 | ch2) < 0) + { throw new EndOfStreamException(); + } return (char)((ch1 << 8) + (ch2 << 0)); } @@ -161,7 +177,9 @@ namespace Lucene.Net.Support int ch3 = @in.ReadByte(); int ch4 = @in.ReadByte(); if ((ch1 | ch2 | ch3 | ch4) < 0) + { throw new EndOfStreamException(); + } return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); } @@ -320,12 +338,16 @@ namespace Lucene.Net.Support /* 110x xxxx 10xx xxxx*/ count += 2; if (count > utflen) + { throw new FormatException( "malformed input: partial character at end"); + } char2 = (int)bytearr[count - 1]; if ((char2 & 0xC0) != 0x80) + { throw new FormatException( "malformed input around byte " + count); + } chararr[chararr_count++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F)); break; @@ -333,13 +355,17 @@ namespace Lucene.Net.Support /* 1110 xxxx 10xx xxxx 10xx xxxx */ count += 3; if (count > utflen) + { throw new FormatException( "malformed input: partial character at end"); + } char2 = (int)bytearr[count - 2]; char3 = (int)bytearr[count - 1]; if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) + { throw new FormatException( "malformed input around byte " + (count - 1)); + } chararr[chararr_count++] = (char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));