Skip to content

Commit

Permalink
dispose encodeResult in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-git committed Mar 28, 2024
1 parent 79f0ee5 commit f27ee3d
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions tracer/src/Datadog.Trace/AppSec/Waf/Waf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,13 @@ internal static unsafe InitResult Create(WafLibraryInvoker wafLibraryInvoker, st

DdwafObjectStruct rulesObj;
DdwafConfigStruct configWafStruct = default;
IEncodeResult? result = null;
IEncoder encoder;
var keyRegex = Marshal.StringToHGlobalAnsi(obfuscationParameterKeyRegex);
var valueRegex = Marshal.StringToHGlobalAnsi(obfuscationParameterValueRegex);
configWafStruct.KeyRegex = keyRegex;
configWafStruct.ValueRegex = valueRegex;
// here we decide not to configure any free function like `configWafStruct.FreeWafFunction = wafLibraryInvoker.ObjectFreeFuncPtr`
// as we free the object ourselves in both cases calling for the legacy encoder wafLibraryInvoker.ObjectFreeFuncPtr manually and for the other ones, handling our own allocations
if (useUnsafeEncoder)
{
encoder = new Encoder();
result = encoder.Encode(jtokenRoot, applySafetyLimits: false);
rulesObj = result.ResultDdwafObject;
}
else
{
encoder = new EncoderLegacy(wafLibraryInvoker);
var configObjWrapper = encoder.Encode(jtokenRoot, applySafetyLimits: false);
rulesObj = configObjWrapper.ResultDdwafObject;
}
IEncoder encoder = useUnsafeEncoder ? new Encoder() : new EncoderLegacy(wafLibraryInvoker);
var result = encoder.Encode(jtokenRoot, applySafetyLimits: false);
rulesObj = result.ResultDdwafObject;

var diagnostics = new DdwafObjectStruct { Type = DDWAF_OBJ_TYPE.DDWAF_OBJ_MAP };

Expand All @@ -114,11 +101,7 @@ internal static unsafe InitResult Create(WafLibraryInvoker wafLibraryInvoker, st
}

wafLibraryInvoker.ObjectFree(ref diagnostics);

if (useUnsafeEncoder)
{
result?.Dispose();
}
result.Dispose();
}
}

Expand Down

0 comments on commit f27ee3d

Please sign in to comment.