Skip to content

Commit

Permalink
Cope with unsupported fields in parser
Browse files Browse the repository at this point in the history
Tested by including a bad size in the table, which would cause the next
type identifier to be garbage.

Issue #5
  • Loading branch information
camelpunch committed Jun 9, 2016
1 parent 9f17bce commit 39a18ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
21 changes: 20 additions & 1 deletion RMQClient/RMQParser.m
Expand Up @@ -22,6 +22,18 @@ typedef NS_ENUM(char, RMQParserFieldValue) {
RMQParserByteArray = 'x',
};

@interface RMQValueForUnsupportedField : RMQValue <RMQFieldValue>
@end

@implementation RMQValueForUnsupportedField
- (NSData *)amqFieldValueType {
return nil;
}
- (NSData *)amqEncoded {
return [NSData data];
}
@end

@interface RMQParser ()
@property (nonatomic, readwrite) const char *cursor;
@property (nonatomic, readwrite) const char *end;
Expand Down Expand Up @@ -50,7 +62,12 @@ - (instancetype)initWithData:(NSData *)data {
NSString *key = [self parseShortString];

RMQParserFieldValue type = *(self.cursor++);
dict[key] = [self parseValueForType:type];
RMQValue<RMQFieldValue> *value = [self parseValueForType:type];

if ([value isKindOfClass:[RMQValueForUnsupportedField class]]) {
return @{};
}
dict[key] = value;
}

return dict;
Expand Down Expand Up @@ -195,6 +212,8 @@ - (NSArray *)parseFieldArray {
return [RMQVoid new];
case RMQParserByteArray:
return [[RMQByteArray alloc] init:[self parseByteArray]];
default:
return [RMQValueForUnsupportedField new];
}
}

Expand Down
17 changes: 17 additions & 0 deletions RMQClientTests/RMQParserTest.swift
Expand Up @@ -151,4 +151,21 @@ class RMQParserTest: XCTestCase {
XCTAssertEqual(dict, parser.parseFieldTable())
}

func testGetEmptyDictWhenFieldTableIncludesValueWithBadSize() {
var dict: [String: RMQValue] = [:]
dict["unsigned-16-bit"] = RMQShort(65535)
dict["bad-size"] = ValueWithBadSize(123)

let table = RMQTable(dict)
let data = table.amqEncoded()
let parser = RMQParser(data: data)
XCTAssertEqual([:], parser.parseFieldTable())
}

@objc class ValueWithBadSize : RMQSignedLong {
override func amqEncoded() -> NSData {
var longVal = CFSwapInt32HostToBig(UInt32(self.integerValue))
return NSData(bytes: &longVal, length: 1)
}
}
}

0 comments on commit 39a18ba

Please sign in to comment.