diff options
author | rowanbeentje <rowan@beent.je> | 2013-05-08 08:40:21 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2013-05-08 08:40:21 +0000 |
commit | 989c0912635c811cc77298f415366237a7c7311d (patch) | |
tree | 66fa29380d16109e091c8e5837fa1cf97fce4f68 /Frameworks/SPMySQLFramework/Source | |
parent | 5c126c28f177ebf961905f67a0535897e74d53ef (diff) | |
download | sequelpro-release-1.0.2.tar.gz sequelpro-release-1.0.2.tar.bz2 sequelpro-release-1.0.2.zip |
- Merge r4095 from trunk to the 1.0.x release branch: Fix bit data conversion to strings to avoid memory trampling, addressing Issue #1708release-1.0.2
- Update localizable strings
Diffstat (limited to 'Frameworks/SPMySQLFramework/Source')
-rw-r--r-- | Frameworks/SPMySQLFramework/Source/SPMySQLResult.m | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLResult.m b/Frameworks/SPMySQLFramework/Source/SPMySQLResult.m index ee758bad..fdc83332 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLResult.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLResult.m @@ -346,21 +346,31 @@ static id NSNullPointer; * Provides a binary representation of the supplied bytes as a returned NSString. * The resulting binary representation will be zero-padded according to the supplied * field length. + * MySQL stores bit data as string data stored in an 8-bit wide character set. */ + (NSString *)bitStringWithBytes:(const char *)bytes length:(NSUInteger)length padToLength:(NSUInteger)padLength { - if (bytes == NULL) return nil; - NSUInteger i = 0; - length--; - padLength--; + NSUInteger bitLength = length << 3; + + if (bytes == NULL) { + return nil; + } - // Generate a C string representation of the binary data - char *cStringBuffer = malloc(length + 1); - while (i <= padLength) { - cStringBuffer[padLength - i++] = ( (bytes[length - (i >> 3)] >> (i & 0x7)) & 1 ) ? '1' : '0'; + // Ensure padLength is never lower than the length + if (padLength < bitLength) { + padLength = bitLength; + } + + // Generate a nul-terminated C string representation of the binary data + char *cStringBuffer = malloc(padLength + 1); + cStringBuffer[padLength] = '\0'; + while (i < bitLength) { + cStringBuffer[padLength - ++i] = ( (bytes[length - 1 - (i >> 3)] >> (i & 0x7)) & 1 ) ? '1' : '0'; + } + while (i++ < padLength) { + cStringBuffer[padLength - i] = '0'; } - cStringBuffer[padLength+1] = '\0'; // Convert to a string NSString *returnString = [NSString stringWithUTF8String:cStringBuffer]; |