NehanPathan commented on code in PR #1154:
URL: https://github.com/apache/lucenenet/pull/1154#discussion_r2378365436
##########
src/Lucene.Net.Analysis.SmartCn/Hhmm/BigramDictionary.cs:
##########
@@ -286,37 +303,37 @@ public virtual void LoadFromFile(string dctFilePath)
int j = 0;
while (j < cnt)
{
- dctFile.Read(intBuffer, 0, intBuffer.Length);
- buffer[0] =
ByteBuffer.Wrap(intBuffer).SetOrder(ByteOrder.LittleEndian)
- .GetInt32();// frequency
- dctFile.Read(intBuffer, 0, intBuffer.Length);
- buffer[1] =
ByteBuffer.Wrap(intBuffer).SetOrder(ByteOrder.LittleEndian)
- .GetInt32();// length
- dctFile.Read(intBuffer, 0, intBuffer.Length);
- // buffer[2] = ByteBuffer.wrap(intBuffer).order(
- // ByteOrder.LITTLE_ENDIAN).getInt();// handle
+ // LUCENENET: Use BinaryReader to decode little endian
instead of ByteBuffer, since this is the default in .NET
+ buffer[0] = reader.ReadInt32(); // frequency
+ buffer[1] = reader.ReadInt32(); // length
+ buffer[2] = reader.ReadInt32(); // Skip handle value
(unused)
length = buffer[1];
- if (length > 0)
+ if (length > 0 && dctFile.Position + length <=
dctFile.Length)
Review Comment:
If the file is read sequentially and nothing else is buffering, the current
check will probably work fine.
But in some edge cases (larger files, concurrent access, or future changes),
the length check could fail or skip bytes incorrectly.
so i guess we should change it to reader.BaseStream.Position
It’s a small change and future-proofs the code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]