堆栈溢出错误,没有递归

时间:2014-02-12 15:18:31

标签: ios objective-c stack-overflow

基本上我需要帮助修复堆栈溢出错误我一直在我的ipad / iphone应用程序中。我一直试图在过去10天修复它,但没有用。

之后没有递归
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
只要在UITextField中更改了字符/字符串,就会调用

基本上这是我遇到问题的部分:

- (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;
}

它基本上将十六进制转换为ARM / THUMB。 HexToThumb / HexToARM函数有效,它们返回一个值。基本上问题是当m_instructionType等于TYPE_1时,应用程序崩溃并发生堆栈溢出。但是当它等于TYPE_2时,新字段的值会相应地改变。当我在各个位置NSLog代码时,似乎代码运行正常并且HexToThumb函数返回正确的值。它甚至在m_curNewValueField = [结果副本]之后继续并记录日志;实际上显示在syslog中。 顺便说一句,这是BYTETOBITPATTERN和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)

非常感谢。

2 个答案:

答案 0 :(得分:1)

我尝试使用snprintfsprintf相应的限制(32?)。

答案 1 :(得分:0)

您的char bits [n];定义未能将终止零字节sprintf考虑在内,应该是17和33而不是16和32。