From 9edf2b60f917aa85eb47c41d6e4ec79aa85cdf62 Mon Sep 17 00:00:00 2001 From: Alexander Danmayer Date: Mon, 27 Nov 2023 11:26:11 +0100 Subject: [PATCH] fix problematic API usage (strncpy, etc). --- SVGKit-iOS.xcodeproj/project.pbxproj | 14 ++-- .../Core DOM/CSSStyleDeclaration.m | 76 ++++--------------- Source/Parsers/SVGKParser.m | 66 ++++------------ 3 files changed, 37 insertions(+), 119 deletions(-) diff --git a/SVGKit-iOS.xcodeproj/project.pbxproj b/SVGKit-iOS.xcodeproj/project.pbxproj index c7d821c29..46aeaa032 100644 --- a/SVGKit-iOS.xcodeproj/project.pbxproj +++ b/SVGKit-iOS.xcodeproj/project.pbxproj @@ -282,7 +282,7 @@ 32F206DC217324BB00427F24 /* SVGKExporterNSImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F206DA217324BB00427F24 /* SVGKExporterNSImage.m */; }; 4FD821F01AC69F6600E419D3 /* SVGSwitchElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 4FD821EE1AC69F6600E419D3 /* SVGSwitchElement.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4FD821F11AC69F6600E419D3 /* SVGSwitchElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FD821EF1AC69F6600E419D3 /* SVGSwitchElement.m */; }; - 55452BF719EC68A200B75A30 /* libSVGKit-iOS.2.1.0.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.0.a */; }; + 55452BF719EC68A200B75A30 /* libSVGKit-iOS.2.1.1.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.1.a */; }; 55452C0E19EC6EC600B75A30 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 6639633F16145DDC00E58CCA /* libxml2.dylib */; }; 660D50011C4292A6004187D0 /* CocoaLumberjack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 660D4FF01C42927A004187D0 /* CocoaLumberjack.framework */; }; 660D50021C4292DB004187D0 /* CocoaLumberjack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 660D4FF01C42927A004187D0 /* CocoaLumberjack.framework */; }; @@ -1079,7 +1079,7 @@ 6636CD89175F54680072AAEF /* CALayer+RecursiveClone.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CALayer+RecursiveClone.m"; sourceTree = ""; }; 6636CD8C175F54970072AAEF /* ConverterSVGToCALayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConverterSVGToCALayer.h; sourceTree = ""; }; 66372F5916960D4E008C6C56 /* SVGRect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGRect.m; sourceTree = ""; }; - 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.0.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libSVGKit-iOS.2.1.0.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.1.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libSVGKit-iOS.2.1.1.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 6639619116145D0400E58CCA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 6639633F16145DDC00E58CCA /* libxml2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libxml2.dylib; path = usr/lib/libxml2.dylib; sourceTree = SDKROOT; }; 6639634116148CDF00E58CCA /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; @@ -1339,7 +1339,7 @@ files = ( 660D50021C4292DB004187D0 /* CocoaLumberjack.framework in Frameworks */, 55452C0E19EC6EC600B75A30 /* libxml2.dylib in Frameworks */, - 55452BF719EC68A200B75A30 /* libSVGKit-iOS.2.1.0.a in Frameworks */, + 55452BF719EC68A200B75A30 /* libSVGKit-iOS.2.1.1.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1522,7 +1522,7 @@ 6639618F16145D0400E58CCA /* Products */ = { isa = PBXGroup; children = ( - 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.0.a */, + 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.1.a */, 55452BF119EC68A200B75A30 /* SVGKit-iOS Tests.xctest */, 825CDAAF1BDA4BC0003C1C12 /* SVGKit.framework */, 825CDAB81BDA4BC0003C1C12 /* SVGKitFramework-iOSTests.xctest */, @@ -2594,7 +2594,7 @@ ); name = "SVGKit-iOS"; productName = "SVGKit-iOS"; - productReference = 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.0.a */; + productReference = 6639618E16145D0400E58CCA /* libSVGKit-iOS.2.1.1.a */; productType = "com.apple.product-type.library.static"; }; 825CDAAE1BDA4BC0003C1C12 /* SVGKitFramework-iOS */ = { @@ -3680,7 +3680,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; DSTROOT = /tmp/SVGKit_iOS.dst; - DYLIB_CURRENT_VERSION = 2.1.0; + DYLIB_CURRENT_VERSION = 2.1.1; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/3rd-party-frameworks/CocoaLumberjack-2.2.0/iOS", @@ -3705,7 +3705,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; DSTROOT = /tmp/SVGKit_iOS.dst; - DYLIB_CURRENT_VERSION = 2.1.0; + DYLIB_CURRENT_VERSION = 2.1.1; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/3rd-party-frameworks/CocoaLumberjack-2.2.0/iOS", diff --git a/Source/DOM classes/Core DOM/CSSStyleDeclaration.m b/Source/DOM classes/Core DOM/CSSStyleDeclaration.m index 6be5bc218..7a3ba194f 100644 --- a/Source/DOM classes/Core DOM/CSSStyleDeclaration.m +++ b/Source/DOM classes/Core DOM/CSSStyleDeclaration.m @@ -49,70 +49,22 @@ -(void)setCssText:(NSString *)newCSSText } --(NSMutableDictionary *) NSDictionaryFromCSSAttributes: (NSString *)css { +-(NSMutableDictionary *) NSDictionaryFromCSSAttributes: (NSString *) cssString { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSCharacterSet* trimChars = [NSCharacterSet whitespaceAndNewlineCharacterSet]; - - const char *cstr = [css UTF8String]; - size_t len = strlen(cstr); - - char name[MAX_NAME]; - bzero(name, MAX_NAME); - - char accum[MAX_ACCUM]; - bzero(accum, MAX_ACCUM); - - size_t accumIdx = 0; - - for (size_t n = 0; n <= len; n++) { - char c = cstr[n]; - - if (c == ':') { - strncpy(name, accum, MAX_NAME); - name[accumIdx] = '\0'; - - bzero(accum, MAX_ACCUM); - accumIdx = 0; - - continue; - } - else if (c == ';' || c == '\0') { - if( accumIdx > 0 ) //if there is a ';' and '\0' to end the style, avoid adding an empty key-value pair - { - accum[accumIdx] = '\0'; - - NSString *keyString = [[NSString stringWithUTF8String:name] - stringByTrimmingCharactersInSet:trimChars]; - NSString *cssValueString = [[NSString stringWithUTF8String:accum] - stringByTrimmingCharactersInSet:trimChars]; - - CSSValue *cssValue; - if( [cssValueString rangeOfString:@" "].length > 0 ) - cssValue = [[CSSValueList alloc] init]; - else - cssValue = [[CSSPrimitiveValue alloc] init]; - cssValue.cssText = cssValueString; // has the side-effect of parsing, if required - - [dict setObject:cssValue - forKey:keyString]; - - bzero(name, MAX_NAME); - - bzero(accum, MAX_ACCUM); - accumIdx = 0; - } - - continue; - } - - accum[accumIdx++] = c; - if (accumIdx >= MAX_ACCUM) { - SVGKitLogWarn(@"Buffer ovverun while parsing style sheet - skipping"); - return dict; - } - } - - return dict; + + NSArray *properties = [cssString componentsSeparatedByString: @";"]; + + for (NSString *property in properties) { + NSArray *keyValuePair = [property componentsSeparatedByString:@":"]; + if (keyValuePair.count == 2) { + NSString *key = [keyValuePair[0] stringByTrimmingCharactersInSet:trimChars]; + NSString *value = [keyValuePair[1] stringByTrimmingCharactersInSet:trimChars]; + [dict setObject:value forKey:key]; + } + } + + return dict; } -(NSString*) getPropertyValue:(NSString*) propertyName diff --git a/Source/Parsers/SVGKParser.m b/Source/Parsers/SVGKParser.m index 521c2beb5..954f2ec27 100644 --- a/Source/Parsers/SVGKParser.m +++ b/Source/Parsers/SVGKParser.m @@ -897,56 +897,22 @@ +(NSDictionary *) NSDictionaryFromCSSAttributes: (Attr*) styleAttribute { SVGKitLogWarn(@"[%@] WARNING: asked to convert an empty CSS string into a CSS dictionary; returning empty dictionary", [self class] ); return [NSDictionary dictionary]; } - - NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; - - const char *cstr = [styleAttribute.value UTF8String]; - size_t len = strlen(cstr); - - char name[MAX_NAME]; - bzero(name, MAX_NAME); - - char accum[MAX_ACCUM]; - bzero(accum, MAX_ACCUM); - - size_t accumIdx = 0; - - for (size_t n = 0; n <= len; n++) { - char c = cstr[n]; - - if (c == '\n' || c == '\t' || c == ' ') { - continue; - } - - if (c == ':') { - strcpy(name, accum); - name[accumIdx] = '\0'; - - bzero(accum, MAX_ACCUM); - accumIdx = 0; - - continue; - } - else if (c == ';' || c == '\0') { - accum[accumIdx] = '\0'; - - Attr* newAttribute = [[Attr alloc] initWithNamespace:styleAttribute.namespaceURI qualifiedName:[NSString stringWithUTF8String:name] value:[NSString stringWithUTF8String:accum]]; - - [dict setObject:newAttribute - forKey:newAttribute.localName]; - - bzero(name, MAX_NAME); - - bzero(accum, MAX_ACCUM); - accumIdx = 0; - - continue; - } - - accum[accumIdx++] = c; - } - - return dict; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; + NSCharacterSet* trimChars = [NSCharacterSet whitespaceAndNewlineCharacterSet]; + + NSArray *properties = [styleAttribute.value componentsSeparatedByString:@";"]; + + for (NSString *property in properties) { + NSArray *keyValuePair = [property componentsSeparatedByString:@":"]; + if (keyValuePair.count == 2) { + NSString *key = [keyValuePair[0] stringByTrimmingCharactersInSet:trimChars]; + NSString *value = [keyValuePair[1] stringByTrimmingCharactersInSet:trimChars]; + [dict setObject:value forKey:key]; + } + } + + return [dict copy]; } @end