Skip to content

Commit 1742dd8

Browse files
chore(audience-sdk): nullable annotations on Json + Log; range-check ConsentStore (SDK-256)
- #nullable enable on Json.cs and Log.cs surfaces the real nullability concerns: HashSet<object>? for the Json visited-set first call from Serialize, and Action<string>? for Log.Writer (null until AudienceUnityHooks.Install runs). - Replaces reflective Enum.IsDefined in ConsentStore.Load with an explicit range check (raw >= ConsentLevel.None && raw <= ConsentLevel.Full); IL2CPP-safe under aggressive stripping where enum metadata may be dropped even with link.xml preserve="all".
1 parent afa86ee commit 1742dd8

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/Packages/Audience/Runtime/Core/ConsentStore.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ internal static void Save(string persistentDataPath, ConsentLevel level)
3636
if (!File.Exists(filePath)) return null;
3737

3838
var text = File.ReadAllText(filePath).Trim();
39-
if (int.TryParse(text, out var raw) && Enum.IsDefined(typeof(ConsentLevel), raw))
39+
// Range check is reflection-free (Enum.IsDefined uses reflection; under
40+
// Unity 6's tighter IL2CPP stripping the metadata it walks may be
41+
// stripped even with link.xml preserve="all" on the assembly).
42+
if (int.TryParse(text, out var raw)
43+
&& raw >= (int)ConsentLevel.None && raw <= (int)ConsentLevel.Full)
4044
return (ConsentLevel)raw;
4145
}
4246
catch (IOException)

src/Packages/Audience/Runtime/Utility/Json.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections;
35
using System.Collections.Generic;
@@ -31,7 +33,7 @@ internal static string Serialize(Dictionary<string, object> data, int indent)
3133
return sb.ToString();
3234
}
3335

34-
private static void WriteValue(StringBuilder sb, object value, int indent, int depth, HashSet<object> visited)
36+
private static void WriteValue(StringBuilder sb, object? value, int indent, int depth, HashSet<object>? visited)
3537
{
3638
if (value == null)
3739
{
@@ -81,11 +83,11 @@ private static void WriteValue(StringBuilder sb, object value, int indent, int d
8183
}
8284
else
8385
{
84-
WriteString(sb, value.ToString());
86+
WriteString(sb, value.ToString() ?? string.Empty);
8587
}
8688
}
8789

88-
private static void WriteObject(StringBuilder sb, Dictionary<string, object> dict, int indent, int depth, HashSet<object> visited)
90+
private static void WriteObject(StringBuilder sb, Dictionary<string, object> dict, int indent, int depth, HashSet<object>? visited)
8991
{
9092
GuardDepth(depth);
9193
visited = EnterContainer(dict, visited);
@@ -109,7 +111,7 @@ private static void WriteObject(StringBuilder sb, Dictionary<string, object> dic
109111
visited.Remove(dict);
110112
}
111113

112-
private static void WriteArray(StringBuilder sb, IList list, int indent, int depth, HashSet<object> visited)
114+
private static void WriteArray(StringBuilder sb, IList list, int indent, int depth, HashSet<object>? visited)
113115
{
114116
GuardDepth(depth);
115117
visited = EnterContainer(list, visited);
@@ -143,7 +145,7 @@ private static void GuardDepth(int depth)
143145
"Check for a cyclic or excessively deep dictionary/list.");
144146
}
145147

146-
private static HashSet<object> EnterContainer(object container, HashSet<object> visited)
148+
private static HashSet<object> EnterContainer(object container, HashSet<object>? visited)
147149
{
148150
visited ??= new HashSet<object>(ReferenceEqualityComparer.Instance);
149151
if (!visited.Add(container))

src/Packages/Audience/Runtime/Utility/Log.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24

35
namespace Immutable.Audience
@@ -9,7 +11,7 @@ internal static class Log
911
internal static bool Enabled { get; set; }
1012

1113
// Tests set this to capture output; AudienceUnityHooks sets it to Debug.Log.
12-
internal static Action<string> Writer { get; set; }
14+
internal static Action<string>? Writer { get; set; }
1315

1416
internal static void Debug(string message)
1517
{

0 commit comments

Comments
 (0)