-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-3802: [Csharp] Fix memory leak on deflate codec decompression #2439
Changes from all commits
e00d66f
d239dcc
45ddd47
42dcaf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
using System.Collections.Generic; | ||
using Avro.IO; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Avro.Generic | ||
{ | ||
|
@@ -290,21 +291,21 @@ protected virtual object ReadRecord(object reuse, RecordSchema writerSchema, Sch | |
} | ||
} | ||
|
||
var defaultStream = new MemoryStream(); | ||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs) | ||
using (var defaultStream = new MemoryStream()) | ||
{ | ||
if (writerSchema.Contains(rf.Name)) continue; | ||
|
||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
|
||
object obj = null; | ||
TryGetField(rec, rf.Name, rf.Pos, out obj); | ||
AddField(rec, rf.Name, rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs.Fields.Where(rf => !writerSchema.Contains(rf.Name))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CodeQL warnings like this, I ussuallyh just ignore, since it is really not a concern in a legacy code like avro. However thanks for getting rid of it ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect this change makes the code slower, as it now has to allocate a closure and a delegate, and the calls become indirect too. Roslyn contribution guidelines advise "Avoid LINQ" for that reason. So I'd prefer reverting this and disabling the CodeQL warning. |
||
{ | ||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
|
||
object obj = null; | ||
TryGetField(rec, rf.Name, rf.Pos, out obj); | ||
AddField(rec, rf.Name, rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
} | ||
} | ||
|
||
return rec; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,20 +130,22 @@ | |
} | ||
} | ||
|
||
var defaultStream = new MemoryStream(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar code like this is in GenericReader and PreresolvingDatumReader. If SpecificReader fixes the MemoryStream leak, those others should be fixed as well. |
||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs) | ||
using (var defaultStream = new MemoryStream()) | ||
{ | ||
if (writerSchema.Contains(rf.Name)) continue; | ||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs) | ||
{ | ||
if (writerSchema.Contains(rf.Name)) continue; | ||
|
||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
|
||
obj = rec.Get(rf.Pos); | ||
rec.Put(rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
obj = rec.Get(rf.Pos); | ||
Check warning Code scanning / CodeQL Dereferenced variable may be null Warning
Variable
rec Error loading related location Loading this Error loading related location Loading |
||
rec.Put(rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
} | ||
Comment on lines
+137
to
+148
Check notice Code scanning / CodeQL Missed opportunity to use Where Note
This foreach loop
implicitly filters its target sequence Error loading related location Loading |
||
} | ||
|
||
return rec; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could make
MemoryStream inStream
read-only by constructing it withwritable: false
(the default istrue
). Not important though, as DeflateStream with CompressionMode.Decompress won't try to write to it anyway.