Skip to content

Commit 107306a

Browse files
xsyetopzclaude
andcommitted
fix(core): replace Stream.Read with ReadExactly to fix CA2022
Stream.Read can return fewer bytes than requested. ReadExactly (available since .NET 7) throws if the stream ends early, which is the correct behavior for structured binary parsing. 11 occurrences across ExeExtractor, DpmExtractor, and AxData. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 35a18a5 commit 107306a

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/HspDecompiler.Core/Ax2/Data/AxData.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,31 @@ private void ReadData(Stream stream)
6969
Header head = header;
7070
tokenData = new byte[head.ScriptByte];
7171
stream.Seek(startPosition + head.ScriptOffset, SeekOrigin.Begin);
72-
stream.Read(tokenData, 0, head.ScriptByte);
72+
stream.ReadExactly(tokenData, 0, head.ScriptByte);
7373

7474
dllData = new byte[head.DllByte];
7575
stream.Seek(startPosition + head.DllOffset, SeekOrigin.Begin);
76-
stream.Read(dllData, 0, head.DllByte);
76+
stream.ReadExactly(dllData, 0, head.DllByte);
7777

7878
funcData = new byte[head.FuncByte];
7979
stream.Seek(startPosition + head.FuncOffset, SeekOrigin.Begin);
80-
stream.Read(funcData, 0, head.FuncByte);
80+
stream.ReadExactly(funcData, 0, head.FuncByte);
8181

8282
deffuncData = new byte[head.DeffuncByte];
8383
stream.Seek(startPosition + head.DeffuncOffset, SeekOrigin.Begin);
84-
stream.Read(deffuncData, 0, head.DeffuncByte);
84+
stream.ReadExactly(deffuncData, 0, head.DeffuncByte);
8585

8686
moduleData = new byte[head.ModuleByte];
8787
stream.Seek(startPosition + head.ModuleOffset, SeekOrigin.Begin);
88-
stream.Read(moduleData, 0, head.ModuleByte);
88+
stream.ReadExactly(moduleData, 0, head.ModuleByte);
8989

9090
labelData = new byte[head.LabelByte];
9191
stream.Seek(startPosition + head.LabelOffset, SeekOrigin.Begin);
92-
stream.Read(labelData, 0, head.LabelByte);
92+
stream.ReadExactly(labelData, 0, head.LabelByte);
9393

9494
stringData = new byte[head.TextByte];
9595
stream.Seek(startPosition + head.TextOffset, SeekOrigin.Begin);
96-
stream.Read(stringData, 0, head.TextByte);
96+
stream.ReadExactly(stringData, 0, head.TextByte);
9797
}
9898
catch (Exception e)
9999
{

src/HspDecompiler.Core/DpmToAx/DpmExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ internal byte[] GetFile(int fileOffset, int fileSize)
115115
{
116116
reader!.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
117117
byte[] buffer = new byte[fileSize];
118-
reader.BaseStream.Read(buffer, 0, fileSize);
118+
reader.BaseStream.ReadExactly(buffer, 0, fileSize);
119119
return buffer;
120120
}
121121

src/HspDecompiler.Core/ExeToDpm/ExeExtractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal void GetDpmFile(Stream exeStream, Stream dpmStream)
4949
exeStream.Seek(dpmOffset, SeekOrigin.Begin);
5050
int dpmSize = (int)(exeStream.Length - dpmOffset);
5151
byte[] data = new byte[dpmSize];
52-
exeStream.Read(data, 0, dpmSize);
52+
exeStream.ReadExactly(data, 0, dpmSize);
5353
dpmStream.Write(data, 0, dpmSize);
5454
}
5555
catch (IOException ex)
@@ -64,7 +64,7 @@ private long seekDpmStart(Stream exeStream)
6464
if (exeStream.Length >= DpmxOffsetLarge + 4)
6565
{
6666
exeStream.Seek(DpmxOffsetLarge, SeekOrigin.Begin);
67-
exeStream.Read(header, 0, 4);
67+
exeStream.ReadExactly(header, 0, 4);
6868
if (header[0] == DpmxMagicD && header[1] == DpmxMagicP && header[2] == DpmxMagicM && header[3] == DpmxMagicX)
6969
{
7070
return DpmxOffsetLarge;
@@ -74,7 +74,7 @@ private long seekDpmStart(Stream exeStream)
7474
if (exeStream.Length >= DpmxOffsetSmall + 4)
7575
{
7676
exeStream.Seek(DpmxOffsetSmall, SeekOrigin.Begin);
77-
exeStream.Read(header, 0, 4);
77+
exeStream.ReadExactly(header, 0, 4);
7878
if (header[0] == DpmxMagicD && header[1] == DpmxMagicP && header[2] == DpmxMagicM && header[3] == DpmxMagicX)
7979
{
8080
return DpmxOffsetSmall;

0 commit comments

Comments
 (0)