Skip to content

Commit

Permalink
Fix a bug where a buffer was accessed after free
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Sep 26, 2023
1 parent db4a44d commit d074461
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* - exit
*
* 2. Configure docker so that port 22 is exposed (e.g. to port 2222), then test the connection:
* - ssh root@<docker host> -p 2222
* - ssh root@ensyno.iwes.fraunhofer.de -p 2222
*
* 3. Replace <docker host> below with the actual host
*/
Expand All @@ -46,7 +46,7 @@
"pipeProgram": "ssh",
"pipeArgs": [
"-T",
"root@<docker host>",
"root@ensyno.iwes.fraunhofer.de",
"-p",
"2222"
], // replace <docker host>
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v2.0.0-beta.19 - 2023-09-26

### Bugs fixed:
- Fixed a bug where a buffer was accessed after free.

## v2.0.0-beta.18 - 2023-09-20

### Bugs fixed:
Expand Down
2 changes: 1 addition & 1 deletion src/Nexus/API/JobsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public async Task<ActionResult<Job>> ExportAsync(
{
try
{
var result = await dataService.ExportAsync(job.Id, catalogItemRequests, dataService.ReadAsDoubleArrayAsync, parameters, cts.Token);
var result = await dataService.ExportAsync(job.Id, catalogItemRequests, dataService.ReadAsDoubleAsync, parameters, cts.Token);
return result;
}
catch (Exception ex)
Expand Down
15 changes: 6 additions & 9 deletions src/Nexus/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ Task<Stream> ReadAsStreamAsync(
DateTime end,
CancellationToken cancellationToken);

Task<ReadOnlyMemory<double>> ReadAsDoubleArrayAsync(
Task ReadAsDoubleAsync(
string resourcePath,
DateTime begin,
DateTime end,
Memory<double> buffer,
CancellationToken cancellationToken);

Task<string> ExportAsync(
Expand Down Expand Up @@ -120,18 +121,19 @@ public async Task<Stream> ReadAsStreamAsync(
begin,
end,
catalogItemRequest,
readDataHandler: ReadAsDoubleArrayAsync,
readDataHandler: ReadAsDoubleAsync,
_memoryTracker,
_loggerFactory.CreateLogger<DataSourceController>(),
cancellationToken);

return stream;
}

public async Task<ReadOnlyMemory<double>> ReadAsDoubleArrayAsync(
public async Task ReadAsDoubleAsync(
string resourcePath,
DateTime begin,
DateTime end,
Memory<double> buffer,
CancellationToken cancellationToken)
{
var stream = await ReadAsStreamAsync(
Expand All @@ -140,19 +142,14 @@ public async Task<ReadOnlyMemory<double>> ReadAsDoubleArrayAsync(
end,
cancellationToken);

var elementCount = (int)(stream.Length / 8); // TODO is this cast safe?
using var memoryOwner = MemoryPool<double>.Shared.Rent(elementCount);
var result = memoryOwner.Memory.Slice(0, elementCount);
var byteBuffer = new CastMemoryManager<double, byte>(result).Memory;
var byteBuffer = new CastMemoryManager<double, byte>(buffer).Memory;

int bytesRead;

while ((bytesRead = await stream.ReadAsync(byteBuffer, cancellationToken)) > 0)
{
byteBuffer = byteBuffer[bytesRead..];
}

return result;
}

public async Task<string> ExportAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public record ReadRequest(
/// <param name="resourcePath">The path to the resource data to stream.</param>
/// <param name="begin">Start date/time.</param>
/// <param name="end">End date/time.</param>
/// <param name="buffer">The buffer to read to the data into.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns></returns>
public delegate Task<ReadOnlyMemory<double>> ReadDataHandler(
public delegate Task ReadDataHandler(
string resourcePath,
DateTime begin,
DateTime end,
Memory<double> buffer,
CancellationToken cancellationToken);

internal class ReadRequestManager : IDisposable
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "2.0.0",
"suffix": "beta.18"
"suffix": "beta.19"
}

0 comments on commit d074461

Please sign in to comment.