Skip to content

Commit

Permalink
Merge pull request #4 from vokal-isaac/master
Browse files Browse the repository at this point in the history
One last tweak before tagging, I think
  • Loading branch information
Sean Wolter committed Dec 29, 2014
2 parents a0d5b1c + 512cfd3 commit 566251b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Pod/Classes/VOKBenkode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum {
VOKBenkodeErrorStringMissingColon,
/// Read an apparently-negative string length.
VOKBenkodeErrorStringLengthNegative,
/// Read a string length that exceeded NSUIntegerMax.
VOKBenkodeErrorStringLengthExceedesNSUIntegerMax,
/// Read a malformed string length.
VOKBenkodeErrorStringLengthMalformed,
/// The length of the string indicates more data than was passed in.
Expand Down
18 changes: 15 additions & 3 deletions Pod/Classes/VOKBenkode.m
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,19 @@ + (NSString *)decodeString:(NSData *)data
}
return nil;
}
long long stringLength = [buffer longLongValue];
long long llStringLength = [buffer longLongValue];
// Is the string length beyond what can be represented in an NSUInteger? (NSMakeRange takes NSUInteger inputs, so...)
if (llStringLength > NSUIntegerMax) {
if (error) {
*error = [NSError errorWithDomain:VOKBenkodeErrorDomain
code:VOKBenkodeErrorStringLengthExceedesNSUIntegerMax
userInfo:nil];
}
return nil;
}

// Is the string length negative?
if (stringLength < 0) {
if (llStringLength < 0) {
if (error) {
*error = [NSError errorWithDomain:VOKBenkodeErrorDomain
code:VOKBenkodeErrorStringLengthNegative
Expand All @@ -435,8 +444,11 @@ + (NSString *)decodeString:(NSData *)data
return nil;
}

// Safe to cast to NSUInteger (in the interval [0, NSUIntegerMax]).
NSUInteger stringLength = (NSUInteger)llStringLength;

// Is the string length properly formatted (no leading 0s, etc.)?
if (![buffer isEqualToString:[NSString stringWithFormat:@"%lld", stringLength]]) {
if (![buffer isEqualToString:[NSString stringWithFormat:@"%@", @(stringLength)]]) {
if (error) {
*error = [NSError errorWithDomain:VOKBenkodeErrorDomain
code:VOKBenkodeErrorStringLengthMalformed
Expand Down

0 comments on commit 566251b

Please sign in to comment.