From 1d6c2758a664f3c42440a5eaea1db3e4ba5472da Mon Sep 17 00:00:00 2001 From: Andrew Madsen Date: Wed, 25 Feb 2015 11:12:54 -0700 Subject: [PATCH] Issue #57: Fixed exception/crash when setting properties on an empty MIKMIDIMetaTimeSignatureEvent. --- Source/MIKMIDIMetaTimeSignatureEvent.m | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Source/MIKMIDIMetaTimeSignatureEvent.m b/Source/MIKMIDIMetaTimeSignatureEvent.m index 13feb49d..739b75dd 100644 --- a/Source/MIKMIDIMetaTimeSignatureEvent.m +++ b/Source/MIKMIDIMetaTimeSignatureEvent.m @@ -21,6 +21,7 @@ + (NSArray *)supportedMIDIEventTypes { return @[@(MIKMIDIEventTypeMetaTimeSignat + (Class)immutableCounterpartClass { return [MIKMIDIMetaTimeSignatureEvent class]; } + (Class)mutableCounterpartClass { return [MIKMutableMIDIMetaTimeSignatureEvent class]; } + (BOOL)isMutable { return NO; } ++ (size_t)minimumDataSize { return [super minimumDataSize] + 4; /* Account for numerator, denominator, metronome, and 32nd note bytes */ } + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { @@ -44,8 +45,9 @@ - (void)setNumerator:(UInt8)numerator if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION; NSMutableData *mutableMetaData = self.metaData.mutableCopy; + if ([mutableMetaData length] < 1) [mutableMetaData increaseLengthBy:1]; [mutableMetaData replaceBytesInRange:NSMakeRange(0, 1) withBytes:&numerator length:1]; - [self setMetaData:[mutableMetaData copy]]; + [self setMetaData:mutableMetaData]; } - (UInt8)denominator @@ -59,9 +61,10 @@ - (void)setDenominator:(UInt8)denominator if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION; NSMutableData *mutableMetaData = self.metaData.mutableCopy; + if ([mutableMetaData length] < 2) [mutableMetaData increaseLengthBy:2-[mutableMetaData length]]; UInt8 denominatorPower = log2(denominator); [mutableMetaData replaceBytesInRange:NSMakeRange(1, 1) withBytes:&denominatorPower length:1]; - [self setMetaData:[mutableMetaData copy]]; + [self setMetaData:mutableMetaData]; } - (UInt8)metronomePulse @@ -74,8 +77,9 @@ - (void)setMetronomePulse:(UInt8)metronomePulse if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION; NSMutableData *mutableMetaData = self.metaData.mutableCopy; + if ([mutableMetaData length] < 3) [mutableMetaData increaseLengthBy:3-[mutableMetaData length]]; [mutableMetaData replaceBytesInRange:NSMakeRange(2, 1) withBytes:&metronomePulse length:1]; - [self setMetaData:[mutableMetaData copy]]; + [self setMetaData:mutableMetaData]; } - (UInt8)thirtySecondsPerQuarterNote @@ -88,8 +92,9 @@ - (void)setThirtySecondsPerQuarterNote:(UInt8)thirtySecondsPerQuarterNote if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION; NSMutableData *mutableMetaData = self.metaData.mutableCopy; + if ([mutableMetaData length] < 4) [mutableMetaData increaseLengthBy:4-[mutableMetaData length]]; [mutableMetaData replaceBytesInRange:NSMakeRange(3, 1) withBytes:&thirtySecondsPerQuarterNote length:1]; - [self setMetaData:[mutableMetaData copy]]; + [self setMetaData:mutableMetaData]; } - (NSString *)additionalEventDescription