Skip to content

Commit

Permalink
Changed NSString/NSData functions to return empty on error
Browse files Browse the repository at this point in the history
  • Loading branch information
ludocode committed Apr 10, 2016
1 parent 70e09f4 commit cc346e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions bindings/objc/mpack/mpack-objc.h
Expand Up @@ -60,7 +60,7 @@ static NSString* const mpack_nserror_domain = @"com.ludocode.mpack";
* This does not accept any UTF-8 variant such as Modified UTF-8, CESU-8 or
* WTF-8. Only pure UTF-8 is allowed.
*
* If an error occurs, nil is returned.
* If an error occurs, an empty NSString is returned.
*
* @param reader The MPack reader
* @param length The length of the string in bytes
Expand All @@ -77,7 +77,7 @@ NSString* mpack_read_nsstring(mpack_reader_t* reader, size_t length);
* which yielded one of these types, or by a call to an expect function
* such as mpack_expect_str() or mpack_expect_bin().
*
* If an error occurs, nil is returned.
* If an error occurs, an empty NSData is returned.
*
* This can be called multiple times for a single str, bin or ext
* to read the data in chunks. The total data read must add up
Expand All @@ -96,21 +96,21 @@ NSData* mpack_read_nsdata(mpack_reader_t* reader, size_t count);
* Reads a UTF-8 string with the given maximum byte size and returns
* it as an NSString.
*
* If an error occurs, an empty NSString is returned.
*
* @throws mpack_error_too_big If the string is larger than the given maxsize.
* @throws mpack_error_type If the value is not a string.
*
* @return An NSString, or nil if an error occurs.
*/
NSString* mpack_expect_nsstring(mpack_reader_t* reader, size_t maxsize);

/**
* Reads a binary blob with the given maximum byte size and returns it
* as an NSData.
*
* If an error occurs, an empty NSData is returned.
*
* @throws mpack_error_too_big If the data is larger than the given maxsize.
* @throws mpack_error_type If the value is not a binary blob.
*
* @return An NSData, or nil if an error occurs.
*/
NSData* mpack_expect_bin_nsdata(mpack_reader_t* reader, size_t maxsize);
#endif
Expand All @@ -122,22 +122,22 @@ NSData* mpack_expect_bin_nsdata(mpack_reader_t* reader, size_t maxsize);
* Returns an NSString representing the contents of this string node with
* the given maximum byte size, or nil if an error occurs.
*
* If an error occurs, an empty NSString is returned.
*
* @throws mpack_error_too_big If the string is larger than the given maxsize.
* @throws mpack_error_type If the value is not a string.
*
* @return An NSString, or nil if an error occurs.
*/
NSString* mpack_node_nsstring(mpack_node_t node, size_t maxsize);

/**
* Returns an NSData representing the contents of this node with the
* given maximum byte size, or nil if an error occurs.
*
* If an error occurs, an empty NSData is returned.
*
* @throws mpack_error_too_big If the data is larger than the given maxsize.
* @throws mpack_error_type If the value is not a string, binary blob or
* ext type.
*
* @return An NSData, or nil if an error occurs.
*/
NSData* mpack_node_bin_nsdata(mpack_node_t node, size_t maxsize);
#endif
Expand Down
22 changes: 11 additions & 11 deletions bindings/objc/mpack/mpack-objc.m
Expand Up @@ -76,26 +76,26 @@ - (NSString*)localizedDescription {

NSString* mpack_read_nsstring(mpack_reader_t* reader, size_t length) {
if (mpack_reader_error(reader) != mpack_ok)
return nil;
return @"";

// small string
if (mpack_should_read_bytes_inplace(reader, length)) {
const char* bytes = mpack_read_utf8_inplace(reader, length);
if (mpack_reader_error(reader) != mpack_ok)
return nil;
return @"";
return MPACK_OBJC_AUTORELEASE([[NSString alloc] initWithBytes:(const void*)bytes length:length encoding:NSUTF8StringEncoding]);
}

// long string
char* bytes = (char*)mpack_stdlib_malloc(length);
if (bytes == NULL) {
mpack_reader_flag_error(reader, mpack_error_memory);
return nil;
return @"";
}

mpack_read_utf8(reader, bytes, length);
if (mpack_reader_error(reader) != mpack_ok)
return nil;
return @"";

return MPACK_OBJC_AUTORELEASE([[NSString alloc] initWithBytesNoCopy:(void*)bytes
length:length encoding:NSUTF8StringEncoding freeWhenDone:YES]);
Expand All @@ -107,15 +107,15 @@ - (NSString*)localizedDescription {
if (mpack_should_read_bytes_inplace(reader, count)) {
const char* bytes = mpack_read_bytes_inplace(reader, count);
if (mpack_reader_error(reader) != mpack_ok)
return nil;
return [NSData data];
return [NSData dataWithBytes:(const void*)bytes length:count];
}

// big data
NSMutableData* mdata = [NSMutableData dataWithLength:count];
mpack_read_bytes(reader, (char*)[mdata mutableBytes], count);
if (mpack_reader_error(reader) != mpack_ok)
return nil;
return [NSData data];
return mdata;
}

Expand Down Expand Up @@ -148,12 +148,12 @@ - (NSString*)localizedDescription {
NSString* mpack_node_nsstring(mpack_node_t node, size_t maxsize) {
if (mpack_node_strlen(node) > maxsize) {
mpack_node_flag_error(node, mpack_error_too_big);
return nil;
return @"";
}

mpack_node_check_utf8(node);
if (mpack_node_error(node) != mpack_ok)
return nil;
return @"";

size_t count = (size_t)node.data->len;
const char* bytes = node.data->value.bytes;
Expand All @@ -164,19 +164,19 @@ - (NSString*)localizedDescription {

NSData* mpack_node_bin_nsdata(mpack_node_t node, size_t maxsize) {
if (mpack_node_error(node) != mpack_ok)
return nil;
return [NSData data];

if (node.data->type != mpack_type_bin) {
mpack_node_flag_error(node, mpack_error_type);
return nil;
return [NSData data];
}

size_t count = (size_t)node.data->len;
const char* bytes = node.data->value.bytes;

if (count > maxsize) {
mpack_node_flag_error(node, mpack_error_too_big);
return nil;
return [NSData data];
}

return [NSData dataWithBytes:(const void*)bytes length:count];
Expand Down

0 comments on commit cc346e9

Please sign in to comment.