Skip to content

Commit 38c2313

Browse files
committed
Avoid inexact read
1 parent 26e166b commit 38c2313

File tree

7 files changed

+35
-41
lines changed

7 files changed

+35
-41
lines changed

Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
<NoWarn>$(NoWarn);CA1862</NoWarn> <!-- Prefer the string comparison method overload -->
9191
<NoWarn>$(NoWarn);CA1872</NoWarn> <!-- Prefer 'System.Convert.ToHexString(byte[])' over call chains based on 'System.BitConverter.ToString(byte[])' -->
9292
<NoWarn>$(NoWarn);CA2019</NoWarn> <!-- 'ThreadStatic' fields should not use inline initialization -->
93-
<NoWarn>$(NoWarn);CA2022</NoWarn> <!-- Avoid inexact read -->
9493
<NoWarn>$(NoWarn);CA2201</NoWarn> <!-- Exception type System.IndexOutOfRangeException is reserved by the runtime -->
9594
<NoWarn>$(NoWarn);CA2208</NoWarn> <!-- Method XXX.Remove passes parameter name 'item' as the message argument to a ArgumentNullException constructor. -->
9695
<NoWarn>$(NoWarn);CA2211</NoWarn> <!-- Non-constant fields should not be visible -->

main/HSSF/Record/OldLabelRecord.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ the License. You may obtain a copy of the License at
1515
limitations under the License.
1616
==================================================================== */
1717

18+
using System.Diagnostics;
19+
1820
namespace NPOI.HSSF.Record
1921
{
2022
using System;
@@ -54,7 +56,8 @@ public OldLabelRecord(RecordInputStream in1)
5456

5557
// Can only decode properly later when you know the codepage
5658
field_5_bytes = new byte[field_4_string_len];
57-
in1.Read(field_5_bytes, 0, field_4_string_len);
59+
int read = in1.Read(field_5_bytes, 0, field_4_string_len);
60+
Debug.Assert(read == field_4_string_len, "Didn't read the right number of bytes");;
5861

5962
if (in1.Remaining > 0)
6063
{

main/HSSF/Record/OldSheetRecord.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ the License. You may obtain a copy of the License at
1515
limitations under the License.
1616
==================================================================== */
1717

18+
using System.Diagnostics;
19+
1820
namespace NPOI.HSSF.Record
1921
{
2022
using System;
@@ -44,7 +46,8 @@ public OldSheetRecord(RecordInputStream in1)
4446
field_3_type = in1.ReadUByte();
4547
int field_4_sheetname_length = in1.ReadUByte();
4648
field_5_sheetname = new byte[field_4_sheetname_length];
47-
in1.Read(field_5_sheetname, 0, field_4_sheetname_length);
49+
int read = in1.Read(field_5_sheetname, 0, field_4_sheetname_length);
50+
Debug.Assert(read == field_4_sheetname_length, "Didn't read the right number of bytes");
4851
}
4952

5053
public void SetCodePage(CodepageRecord codepage)

main/HSSF/Record/OldStringRecord.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ the License. You may obtain a copy of the License at
1515
limitations under the License.
1616
==================================================================== */
1717

18+
using System.Diagnostics;
19+
1820
namespace NPOI.HSSF.Record
1921
{
2022
using System;
@@ -54,7 +56,8 @@ public OldStringRecord(RecordInputStream in1)
5456

5557
// Can only decode properly later when you know the codepage
5658
field_2_bytes = new byte[field_1_string_len];
57-
in1.Read(field_2_bytes, 0, field_1_string_len);
59+
int read = in1.Read(field_2_bytes, 0, field_1_string_len);
60+
Debug.Assert(read == field_1_string_len, "Didn't read the right number of bytes");
5861
}
5962

6063
public bool IsBiff2

main/POIFS/NIO/FileBackedDataSource.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,24 @@ public FileBackedDataSource(FileInfo file)
5353
}
5454
public FileBackedDataSource(FileInfo file, bool readOnly)
5555
{
56-
if (!file.Exists)
56+
if(!file.Exists)
57+
{
5758
throw new FileNotFoundException(file.FullName);
58-
this.fileinfo = file;
59-
FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
60-
byte[] temp = new byte[stream.Length];
61-
stream.Read(temp, 0, (int)stream.Length);
62-
MemoryStream ms = new MemoryStream(temp, 0, temp.Length);
59+
}
60+
fileinfo = file;
61+
using FileStream stream = File.OpenRead(file.FullName);
62+
MemoryStream ms = new();
63+
stream.CopyTo(ms);
6364
fileStream = ms;
64-
this.writable = !readOnly;
65-
stream.Position = 0;
65+
writable = !readOnly;
6666
}
6767
public FileBackedDataSource(FileStream stream, bool readOnly)
6868
{
6969
stream.Position = 0;
70-
byte[] temp = new byte[stream.Length];
71-
stream.Read(temp, 0, (int)stream.Length);
72-
MemoryStream ms = new MemoryStream(temp, 0, temp.Length);
70+
MemoryStream ms = new();
71+
stream.CopyTo(ms);
7372
fileStream = ms;
74-
this.writable = !readOnly;
73+
writable = !readOnly;
7574
stream.Position = 0;
7675
}
7776

@@ -102,18 +101,9 @@ private void Dispose(bool disposing)
102101

103102
#endregion
104103

105-
public bool IsWriteable
106-
{
107-
get
108-
{
109-
return this.writable;
110-
}
111-
}
104+
public bool IsWriteable => writable;
112105

113-
public Stream Stream
114-
{
115-
get { return this.fileStream; }
116-
}
106+
public Stream Stream => fileStream;
117107

118108
/// <summary>
119109
/// Reads a sequence of bytes from this FileStream starting at the given file position.

main/Util/CRC32.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,8 @@ public ulong StringCRC(string sInputString)
104104
/// <returns></returns>
105105
public long FileCRC(string sInputFilename)
106106
{
107-
using (FileStream inFile = new System.IO.FileStream(sInputFilename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
108-
{
109-
byte[] bInput = new byte[inFile.Length];
110-
inFile.Read(bInput, 0, bInput.Length);
111-
112-
return (long)ByteCRC(ref bInput);
113-
}
107+
byte[] bInput = File.ReadAllBytes(sInputFilename);
108+
return (long) ByteCRC(ref bInput);
114109
}
115110

116111
/// <summary>
@@ -120,11 +115,12 @@ public long FileCRC(string sInputFilename)
120115
/// <returns></returns>
121116
public long StreamCRC(Stream inFile)
122117
{
123-
byte[] bInput = new byte[inFile.Length];
124-
inFile.Read(bInput, 0, bInput.Length);
118+
using MemoryStream ms = new();
119+
inFile.CopyTo(ms);
125120
inFile.Close();
126121

127-
return (long)ByteCRC(ref bInput);
122+
byte[] bytes = ms.ToArray();
123+
return (long)ByteCRC(ref bytes);
128124
}
129125

130126
public ulong Value { get; set; }

testcases/ooxml/XSSF/UserModel/TestXSSFName.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ public void TestSetNameNameCellAddress()
150150
name.NameName = ref1;
151151
Assert.Fail("cell addresses are not allowed: " + ref1);
152152
}
153-
catch (ArgumentException e)
153+
catch (ArgumentException)
154154
{
155155
// expected
156156
}
157157
}
158158

159159
// Name that looks similar to a cell reference but is outside the cell reference row and column limits
160-
name.NameName = ("A0");
161-
name.NameName = ("F04030020010");
162-
name.NameName = ("XFDXFD10");
160+
name.NameName = "A0";
161+
name.NameName = "F04030020010";
162+
name.NameName = "XFDXFD10";
163163
}
164164
}
165165
}

0 commit comments

Comments
 (0)