Skip to content

Commit

Permalink
Merge pull request #7 from adamkaplan/fix-infinite-loop
Browse files Browse the repository at this point in the history
Fix infinite loop with empty or nil string
  • Loading branch information
dbgrandi authored Jan 2, 2019
2 parents 69f5271 + 7daec9a commit 09f71ab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 69 deletions.
9 changes: 9 additions & 0 deletions Classes/DBGHTMLEntityEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ - (void)encodeStringInPlace:(NSMutableString *)mutableString withFormats:(DBGHTM
}

- (BOOL)stringNeedsEncoding:(NSString *)string {
// If the string is empty it does not need encoding
if ([string length] < 1) {
return NO;
}

// if the string contains ANY basic entities, it DOES need encoding
NSCharacterSet *basicEntities = [self basicEntitiesCharacterSet];
NSRange basicEntityRange = [string rangeOfCharacterFromSet:basicEntities];
Expand Down Expand Up @@ -121,6 +126,10 @@ - (NSCharacterSet *)nonASCIICharacterSet {
}

- (NSArray *)rangesOfCharacters:(NSCharacterSet *)characterSet string:(NSString *)str {
if ([str length] < 1) {
return @[];
}

NSMutableArray *results = [NSMutableArray array];
NSRange searchRange = NSMakeRange(0, [str length]);
NSRange range;
Expand Down
66 changes: 1 addition & 65 deletions Example/DBGHTMLEntitiesExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@
554BF51F18F7217A00AC2DE0 /* DecoderSpec.m */,
55513FB718FC83C000EAF893 /* EncoderMapSpec.m */,
55513FB118FC613F00EAF893 /* EncoderSpec.m */,
554BF50E18F718E700AC2DE0 /* Supporting Files */,
55632C31190370C500AE50F1 /* RoundTripSpec.m */,
554BF50E18F718E700AC2DE0 /* Supporting Files */,
);
path = DBGHTMLEntitiesExampleTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -239,8 +239,6 @@
554BF4E718F718E600AC2DE0 /* Sources */,
554BF4E818F718E600AC2DE0 /* Frameworks */,
554BF4E918F718E600AC2DE0 /* Resources */,
C2873BF83903EDC3FC095F97 /* [CP] Embed Pods Frameworks */,
3F8DD1A5B13EE786E6EDE05B /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand All @@ -259,8 +257,6 @@
554BF50218F718E700AC2DE0 /* Sources */,
554BF50318F718E700AC2DE0 /* Frameworks */,
554BF50418F718E700AC2DE0 /* Resources */,
6ECE73C34575B19B42A1137C /* [CP] Embed Pods Frameworks */,
27EF0C016D35C54E8ABB3B0D /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -344,66 +340,6 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
27EF0C016D35C54E8ABB3B0D /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-DBGHTMLEntitiesExampleTests/Pods-DBGHTMLEntitiesExampleTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
3F8DD1A5B13EE786E6EDE05B /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-DBGHTMLEntitiesExample/Pods-DBGHTMLEntitiesExample-resources.sh\"\n";
showEnvVarsInLog = 0;
};
6ECE73C34575B19B42A1137C /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-DBGHTMLEntitiesExampleTests/Pods-DBGHTMLEntitiesExampleTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
C2873BF83903EDC3FC095F97 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-DBGHTMLEntitiesExample/Pods-DBGHTMLEntitiesExample-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
C6F05643E77C09667C653366 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
15 changes: 11 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- DBGHTMLEntities (1.0.0)
- DBGHTMLEntities (1.1.0)
- Expecta (1.0.6)
- FLKAutoLayout (0.2.1)
- ORStackView (3.0.1):
Expand All @@ -12,17 +12,24 @@ DEPENDENCIES:
- ORStackView
- Specta (~> 1.0)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Expecta
- FLKAutoLayout
- ORStackView
- Specta

EXTERNAL SOURCES:
DBGHTMLEntities:
:path: ../DBGHTMLEntities.podspec
:path: "../DBGHTMLEntities.podspec"

SPEC CHECKSUMS:
DBGHTMLEntities: 78b2edd50b83f34cc6283fecb762dd3446a1496b
DBGHTMLEntities: 5b601f2bcb5d67bccca753456516260b7324aadf
Expecta: 3b6bd90a64b9a1dcb0b70aa0e10a7f8f631667d5
FLKAutoLayout: 9db6b30c2008d230da608e62c607b11c23b942e6
ORStackView: a1bb52748cd0ae29891c140baf22ff8972fb363c
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66

PODFILE CHECKSUM: 5eae54b8384f6881d54ff208d7a80d9c42b40843

COCOAPODS: 1.4.0
COCOAPODS: 1.5.3
10 changes: 10 additions & 0 deletions Tests/EncoderSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
});

it(@"should detect if a string needs encoding", ^{
expect([encoder stringNeedsEncoding:nil]).to.beFalsy();
expect([encoder stringNeedsEncoding:@""]).to.beFalsy();
expect([encoder stringNeedsEncoding:@"asdf"]).to.beFalsy();
expect([encoder stringNeedsEncoding:@"`"]).to.beFalsy();

Expand All @@ -40,6 +42,14 @@
expect([encoder encode:@"Nothing to encode here."]).to.equal(@"Nothing to encode here.");
});

it(@"handle nil and empty strings", ^{
expect([encoder encode:nil]).to.beNil();
expect([encoder encode:@""]).to.equal(@"");

expect([[encoder rangesOfCharacters:NSCharacterSet.symbolCharacterSet string:nil] count]).to.equal(0);
expect([[encoder rangesOfCharacters:NSCharacterSet.symbolCharacterSet string:@""] count]).to.equal(0);
});

it(@"should parse the correct number of ranges", ^{
NSCharacterSet *gtltCharacters = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
expect([[encoder rangesOfCharacters:gtltCharacters string:@"<html>"] count]).to.equal(2);
Expand Down

0 comments on commit 09f71ab

Please sign in to comment.