Skip to content

Commit 862a70e

Browse files
authored
Convert to Hex only on Encoding.UTF8.GetString possible exceptions (#2954)
* Convert to Hex only on Encoding.UTF8.GetString possible exception Has OutOfMemory exceptions can occure in native Encoding.UTF8.GetString, we should not convert result to hex values * Fix other cases where we return an Hex string on all kind of exceptions
1 parent 11a919a commit 862a70e

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/StackExchange.Redis/RedisChannel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,12 @@ public static implicit operator RedisChannel(byte[]? key)
302302
{
303303
return Encoding.UTF8.GetString(arr);
304304
}
305-
catch
305+
catch (Exception e) when // Only catch exception throwed by Encoding.UTF8.GetString
306+
(e is DecoderFallbackException
307+
|| e is ArgumentException
308+
|| e is ArgumentNullException)
306309
{
307-
return BitConverter.ToString(arr);
310+
return BitConverter.ToString(arr);
308311
}
309312
}
310313

src/StackExchange.Redis/RedisKey.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,12 @@ public static implicit operator RedisKey(byte[]? key)
299299
{
300300
return Encoding.UTF8.GetString(arr, 0, length);
301301
}
302-
catch
302+
catch (Exception e) when // Only catch exception throwed by Encoding.UTF8.GetString
303+
(e is DecoderFallbackException
304+
|| e is ArgumentException
305+
|| e is ArgumentNullException)
303306
{
304-
return BitConverter.ToString(arr, 0, length);
307+
return BitConverter.ToString(arr, 0, length);
305308
}
306309
}
307310
}

src/StackExchange.Redis/RedisValue.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,10 @@ private static bool TryParseDouble(ReadOnlySpan<byte> blob, out double value)
789789
{
790790
return Format.GetString(span);
791791
}
792-
catch
792+
catch (Exception e) when // Only catch exception throwed by Encoding.UTF8.GetString
793+
(e is DecoderFallbackException
794+
|| e is ArgumentException
795+
|| e is ArgumentNullException)
793796
{
794797
return ToHex(span);
795798
}

0 commit comments

Comments
 (0)