From c5cbb035341e09ec79a2ec02d17667fc9d78a7dc Mon Sep 17 00:00:00 2001 From: yck1509 Date: Wed, 16 Mar 2016 02:39:34 +0800 Subject: [PATCH] Fix NRE in namespace function & removing initializer data field when not all references are removed --- .../Project/Patterns/NamespaceFunction.cs | 5 ++++- Confuser.Protections/Constants/EncodePhase.cs | 21 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Confuser.Core/Project/Patterns/NamespaceFunction.cs b/Confuser.Core/Project/Patterns/NamespaceFunction.cs index 918d1ef55..3d29c1e26 100644 --- a/Confuser.Core/Project/Patterns/NamespaceFunction.cs +++ b/Confuser.Core/Project/Patterns/NamespaceFunction.cs @@ -29,10 +29,13 @@ public override object Evaluate(IDnlibDef definition) { if (type == null) type = ((IMemberDef)definition).DeclaringType; + if (type == null) + return false; + while (type.IsNested) type = type.DeclaringType; - return type != null && Regex.IsMatch(type.Namespace, ns); + return type != null && Regex.IsMatch(type.Namespace ?? "", ns); } } } \ No newline at end of file diff --git a/Confuser.Protections/Constants/EncodePhase.cs b/Confuser.Protections/Constants/EncodePhase.cs index 7902cf2b5..65d7ba7f4 100644 --- a/Confuser.Protections/Constants/EncodePhase.cs +++ b/Confuser.Protections/Constants/EncodePhase.cs @@ -215,10 +215,25 @@ void UpdateReference(CEContext moduleCtx, TypeSig valueType, List dataFields, HashSet fieldRefs) { + foreach (var type in context.CurrentModule.GetTypes()) + foreach (var method in type.Methods.Where(m => m.HasBody)) { + foreach (var instr in method.Body.Instructions) + if (instr.Operand is FieldDef && !fieldRefs.Contains(instr)) + dataFields.Remove((FieldDef)instr.Operand); + } + + foreach (var fieldToRemove in dataFields) { + fieldToRemove.DeclaringType.Fields.Remove(fieldToRemove); + } + } + void ExtractConstants( ConfuserContext context, ProtectionParameters parameters, CEContext moduleCtx, Dictionary>> ldc, Dictionary>> ldInit) { + var dataFields = new HashSet(); + var fieldRefs = new HashSet(); foreach (MethodDef method in parameters.Targets.OfType().WithProgress(context.Logger)) { if (!method.HasBody) continue; @@ -284,8 +299,9 @@ void ExtractConstants( ldc.Remove(arrLen); } - if(dataField.DeclaringType!=null) - dataField.DeclaringType.Fields.Remove(dataField); + dataFields.Add(dataField); + fieldRefs.Add(instrs[i - 1]); + var value = new byte[dataField.InitialValue.Length + 4]; value[0] = (byte)(arrLen >> 0); value[1] = (byte)(arrLen >> 8); @@ -328,6 +344,7 @@ void ExtractConstants( context.CheckCancellation(); } + RemoveDataFieldRefs(context, dataFields, fieldRefs); } class ByteArrayComparer : IEqualityComparer {