Skip to content

Commit

Permalink
Fix CString reader in BufferSliceExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Dec 20, 2022
1 parent 4a46bf3 commit d095f84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
27 changes: 27 additions & 0 deletions LiteDB/Engine/FileReader/FileReaderV8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ private void LoadPragmas()
}
}

/// <summary>
/// Read all file (and log) to find all data pages (and store groupby colPageID)
/// </summary>
private void LoadDataPages()
{
var header = ReadPage(0, out _, out _);

var lastPageID = header.Buffer.ReadUInt32(HeaderPage.P_LAST_PAGE_ID);

for (uint i = 0; i < lastPageID; i++)
{
var page = ReadPage(i, out _, out _);

if (page.PageType == PageType.Data)
{
if (_collectionsDataPages.TryGetValue(page.ColID, out var list))
{
list.Add(page.PageID);
}
else
{
_collectionsDataPages[page.ColID] = new List<uint> { page.PageID };
}
}
}
}

/// <summary>
/// Load all collections from header OR via all data-pages ColID
/// </summary>
Expand Down
11 changes: 3 additions & 8 deletions LiteDB/Utils/Extensions/BufferSliceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,16 @@ public static string ReadCString(this BufferSlice buffer, int offset, out int le
{
length = buffer.Count - buffer.Offset - offset;

var i = offset;

for (; i < buffer.Count; i++)
for (var i = offset + buffer.Offset; i < buffer.Count; i++)
{
if (buffer[i] == '\0')
{
length = i - offset + 1; // +1 for \0
length = i - buffer.Offset - offset + 1; // +1 for \0
break;
}
}

// exclude \0 is last char is \0
var readLength = buffer[i] == '\0' ? length - 1 : length;

return Encoding.UTF8.GetString(buffer.Array, buffer.Offset + offset, readLength);
return Encoding.UTF8.GetString(buffer.Array, buffer.Offset + offset, length);
}

/// <summary>
Expand Down

0 comments on commit d095f84

Please sign in to comment.