Skip to content

Commit fdf4f2c

Browse files
committed
Disallow casting null to string
1 parent 940697a commit fdf4f2c

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

ValveKeyValue/ValveKeyValue/KVObject_IConvertible.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ KVValueType.Collection or KVValueType.Array or KVValueType.BinaryBlob
190190
/// <inheritdoc cref="IConvertible.ToString"/>
191191
public string ToString(IFormatProvider provider) => ValueType switch
192192
{
193-
KVValueType.String => (string)_ref ?? string.Empty,
193+
KVValueType.String => (string)_ref,
194194
KVValueType.Boolean => _scalar != 0 ? "1" : "0",
195-
KVValueType.Null => string.Empty,
195+
KVValueType.Null => throw new NotSupportedException("Cannot convert null to String."),
196196
KVValueType.FloatingPoint => BitConverter.Int32BitsToSingle((int)_scalar).ToString(provider),
197197
KVValueType.FloatingPoint64 => BitConverter.Int64BitsToDouble(_scalar).ToString(provider),
198198
KVValueType.Int32 or KVValueType.Pointer => ((int)_scalar).ToString(provider),

ValveKeyValue/ValveKeyValue/Serialization/KeyValues1/KV1TextSerializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ void WriteKeyValuePair(string name, KVObject value)
107107
WriteText(name);
108108
writer.Write('\t');
109109

110-
if (value.ValueType == KVValueType.Boolean)
110+
if (value.IsNull)
111+
{
112+
WriteText(string.Empty);
113+
}
114+
else if (value.ValueType == KVValueType.Boolean)
111115
{
112116
WriteText(value.ToBoolean(null) ? "1" : "0");
113117
}

0 commit comments

Comments
 (0)