From 5d7324692670c451e989ab50f743a6b4d43b572a Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Mon, 10 Mar 2025 14:39:38 -0700 Subject: [PATCH] Bug Fix: Handle invalid UTF-8 decoding errors Apparently, stringWithUTF8String: just returns nil when the encoding is not UTF-8, rather than throwing an exception. Signed-off-by: Christopher Snowhill --- Audio/Plugin.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Audio/Plugin.h b/Audio/Plugin.h index b8ac14b3b..61e47ab5a 100644 --- a/Audio/Plugin.h +++ b/Audio/Plugin.h @@ -112,13 +112,21 @@ static NSString *guess_encoding_of_string(const char *input) { NSString *ret = @""; - @try { - ret = [NSString stringWithUTF8String:input]; - } - @catch(NSException *e) { - // This method is incredibly slow - NSData *stringData = [NSData dataWithBytes:input length:strlen(input)]; - [NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil]; + if(input && *input) { + @try { + ret = [NSString stringWithUTF8String:input]; + } + @catch(NSException *e) { + ret = nil; + } + if(!ret) { + // This method is incredibly slow + NSData *stringData = [NSData dataWithBytes:input length:strlen(input)]; + [NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil]; + if(!ret) { + ret = @""; + } + } } return ret; }