Reputation: 517
Basically I need help with fixing a stack overflow error I keep getting in my ipad/iphone app. I have been trying to fix it for the last 10 days but with no avail. There is no recursions since
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
gets called once whenever a character/string is changed in a UITextField.
Basically here is the part I have a problem with:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == m_curValueField)
{
if([string rangeOfCharacterFromSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFabcdef"] invertedSet]].location != NSNotFound)
{
return NO;
}
NSString *addedString = m_curValueField.text;
addedString = [addedString stringByReplacingCharactersInRange:range withString:string];
if(m_instructionType == TYPE_1)
{
if(addedString.length > 4)
return NO;
}
else if(m_instructionType == TYPE_2)
{
if(addedString.length > 8)
return NO;
}
NSString *result = @"UNDEFINED";
if([m_curValueField.text isEqualToString:addedString])
{
return NO;
}
if(m_instructionType == TYPE_THUMB)
{
if(addedString.length == 4)
{
NSString *temp = @"";
temp = [temp stringByAppendingString:m_curValueField.text];
temp = [temp stringByAppendingString:string];
NSString *firstByte = [temp substringWithRange:NSMakeRange(0, 2)];
NSString *secondByte = [temp substringWithRange:NSMakeRange(2, 2)];
temp = [NSString stringWithFormat:@"%@%@",secondByte,firstByte];
uint16_t bytes;
memcpy(&bytes, [temp cStringUsingEncoding:NSASCIIStringEncoding], sizeof(uint16_t));
char bits [16];
sprintf (bits,BYTETOBINARYPATTERN,BYTETOBINARY(bytes));
result = [m_thumbconverter HexToThumb:bits];
}
}
else if(m_instructionType == TYPE_2)
{
if(addedString.length == 8)
{
NSString *temp = @"";
temp = [temp stringByAppendingString:m_curValueField.text];
temp = [temp stringByAppendingString:string];
NSString *firstByte = [temp substringWithRange:NSMakeRange(0, 2)];
NSString *secondByte = [temp substringWithRange:NSMakeRange(2, 2)];
NSString *thirdByte = [temp substringWithRange:NSMakeRange(4, 2)];
NSString *fourthByte = [temp substringWithRange:NSMakeRange(6, 2)];
temp = [NSString stringWithFormat:@"%@%@%@%@",fourthByte,thirdByte,secondByte,firstByte];
uint32_t bytes;
memcpy(&bytes, [temp cStringUsingEncoding:NSASCIIStringEncoding], sizeof(uint32_t));
char bits[32];
sprintf (bits,BYTETOBINARYPATTERN,BYTETOBINARY(bytes));
result = [m_armconverter HexToARM:bits];
}
}
m_curNewValueField.text = [result copy];
}
return YES;
}
It basically converts hex to ARM/THUMB. The HexToThumb/HexToARM functions work and they return a value. Basically the problem is that when m_instructionType is equal to TYPE_1, the app crashes with stack overflow. But when it is equal to TYPE_2, the new field's value is changed accordingly. When I NSLog the code at various positions, it seems that the code runs fine and the HexToThumb function returns a correct value. It even continues and a log after m_curNewValueField = [result copy]; actually gets shown in the syslog. Btw, here is the definitions of BYTETOBITPATTERN and BYTETOBINARY:
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(bytes) \
(bytes & 0x80000000 ? 1 : 0), \
(bytes & 0x40000000 ? 1 : 0), \
(bytes & 0x20000000 ? 1 : 0), \
(bytes & 0x10000000 ? 1 : 0), \
(bytes & 0x8000000 ? 1 : 0), \
(bytes & 0x4000000 ? 1 : 0), \
(bytes & 0x2000000 ? 1 : 0), \
(bytes & 0x1000000 ? 1 : 0), \
(bytes & 0x800000 ? 1 : 0), \
(bytes & 0x400000 ? 1 : 0), \
(bytes & 0x200000 ? 1 : 0), \
(bytes & 0x100000 ? 1 : 0), \
(bytes & 0x80000 ? 1 : 0), \
(bytes & 0x40000 ? 1 : 0), \
(bytes & 0x20000 ? 1 : 0), \
(bytes & 0x10000 ? 1 : 0), \
(bytes & 0x8000 ? 1 : 0), \
(bytes & 0x4000 ? 1 : 0), \
(bytes & 0x2000 ? 1 : 0), \
(bytes & 0x1000 ? 1 : 0), \
(bytes & 0x800 ? 1 : 0), \
(bytes & 0x400 ? 1 : 0), \
(bytes & 0x200 ? 1 : 0), \
(bytes & 0x100 ? 1 : 0), \
(bytes & 0x80 ? 1 : 0), \
(bytes & 0x40 ? 1 : 0), \
(bytes & 0x20 ? 1 : 0), \
(bytes & 0x10 ? 1 : 0), \
(bytes & 0x08 ? 1 : 0), \
(bytes & 0x04 ? 1 : 0), \
(bytes & 0x02 ? 1 : 0), \
(bytes & 0x01 ? 1 : 0)
Thank you very much.
Upvotes: 0
Views: 143
Reputation: 11880
I'd try using snprintf
with the appropriate limit (32?) over sprintf
.
Upvotes: 1
Reputation: 2993
Your char bits [n];
definitions fail to take the terminating zero byte sprintf
will produce into account, should be 17 and 33 instead of 16 and 32.
Upvotes: 0