Skip to content

Commit

Permalink
fix problematic API usage (strncpy, etc).
Browse files Browse the repository at this point in the history
  • Loading branch information
adanmayer committed Nov 27, 2023
1 parent 27fe32d commit 9edf2b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 119 deletions.
14 changes: 7 additions & 7 deletions SVGKit-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -1079,7 +1079,7 @@
6636CD89175F54680072AAEF /* CALayer+RecursiveClone.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CALayer+RecursiveClone.m"; sourceTree = "<group>"; };
6636CD8C175F54970072AAEF /* ConverterSVGToCALayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConverterSVGToCALayer.h; sourceTree = "<group>"; };
66372F5916960D4E008C6C56 /* SVGRect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGRect.m; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */ = {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
76 changes: 14 additions & 62 deletions Source/DOM classes/Core DOM/CSSStyleDeclaration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 16 additions & 50 deletions Source/Parsers/SVGKParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9edf2b6

Please sign in to comment.