Skip to content

Commit f07ce7f

Browse files
committed
safe print_r/var_dump when resolving property values
ref #1162
1 parent 59abe71 commit f07ce7f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/Peachpie.Library/Variables.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,11 @@ public override void AcceptObject(object obj)
10271027
_indent++;
10281028

10291029
// object members
1030-
var flds = (obj is IPhpPrintable printable ? printable.Properties : TypeMembersUtils.EnumerateInstanceFieldsForPrint(obj)).ToList();
1030+
var flds = obj is IPhpPrintable printable
1031+
? printable.Properties
1032+
: TypeMembersUtils.EnumerateInstanceFieldsForPrint(obj)
1033+
;
1034+
10311035
foreach (var fld in flds)
10321036
{
10331037
// [name] => value

src/Peachpie.Runtime/Reflection/TypeMembersUtils.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static IEnumerable<KeyValuePair<IntStringKey, PhpValue>> EnumerateVisible
8181
return EnumerateInstanceFields(instance,
8282
(p) => new IntStringKey(p.PropertyName),
8383
FuncExtensions.Identity<IntStringKey>(),
84-
(m) => m.IsVisible(caller));
84+
(m) => m.IsVisible(caller)
85+
);
8586
}
8687

8788
/// <summary>
@@ -93,9 +94,23 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
9394
{
9495
return EnumerateInstanceFields(instance,
9596
s_formatPropertyNameForPrint,
96-
s_keyToString);
97+
s_keyToString,
98+
getValue: s_getValueWithTryCatch
99+
);
97100
}
98101

102+
static readonly Func<PhpPropertyInfo, object, PhpValue> s_getValueWithTryCatch = (p, instance) =>
103+
{
104+
try
105+
{
106+
return p.GetValue(null, instance);
107+
}
108+
catch (Exception e)
109+
{
110+
return $"Property '{p.PropertyName}' threw an exception of type {e.GetType().Name}: {e.Message}";
111+
}
112+
};
113+
99114
public static readonly Func<IntStringKey, string> s_keyToString = k => k.ToString();
100115

101116
public static readonly Func<PhpPropertyInfo, string> s_propertyName = p => p.PropertyName;
@@ -127,7 +142,9 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
127142
{
128143
return EnumerateInstanceFields(instance,
129144
s_formatPropertyNameForDump,
130-
s_keyToString);
145+
s_keyToString,
146+
getValue: s_getValueWithTryCatch
147+
);
131148
}
132149

133150
/// <summary>
@@ -170,9 +187,10 @@ public static IEnumerable<KeyValuePair<string, PhpValue>> EnumerateInstanceField
170187
/// <param name="keyFormatter2">Function converting </param>
171188
/// <param name="predicate">Optional. Predicate filtering fields.</param>
172189
/// <param name="ignoreRuntimeFields">Whether to ignore listing runtime fields.</param>
190+
/// <param name="getValue">Function getting property value on given <paramref name="instance"/>.</param>
173191
/// <returns>Enumeration of fields and their values, including runtime fields.</returns>
174192
/// <typeparam name="TKey">Enumerated pairs key. Usually <see cref="IntStringKey"/>.</typeparam>
175-
public static IEnumerable<KeyValuePair<TKey, PhpValue>> EnumerateInstanceFields<TKey>(object instance, Func<PhpPropertyInfo, TKey> keyFormatter, Func<IntStringKey, TKey> keyFormatter2, Func<PhpPropertyInfo, bool> predicate = null, bool ignoreRuntimeFields = false)
193+
public static IEnumerable<KeyValuePair<TKey, PhpValue>> EnumerateInstanceFields<TKey>(object instance, Func<PhpPropertyInfo, TKey> keyFormatter, Func<IntStringKey, TKey> keyFormatter2, Func<PhpPropertyInfo, bool> predicate = null, bool ignoreRuntimeFields = false, Func<PhpPropertyInfo, object, PhpValue> getValue = null)
176194
{
177195
Debug.Assert(instance != null);
178196

@@ -189,7 +207,8 @@ public static IEnumerable<KeyValuePair<TKey, PhpValue>> EnumerateInstanceFields<
189207
{
190208
yield return new KeyValuePair<TKey, PhpValue>(
191209
keyFormatter(p),
192-
p.GetValue(null, instance));
210+
getValue != null ? getValue(p, instance) : p.GetValue(null, instance)
211+
);
193212
}
194213
}
195214
}

0 commit comments

Comments
 (0)