【问题标题】:Error: Abort trap 6 - String obfuscation错误:中止陷阱 6 - 字符串混淆
【发布时间】:2016-10-31 16:16:59
【问题描述】:

我正在尝试为字符串混淆编写代码,但它给了我 Abort 陷阱:运行时出现 6 错误。任何想法都非常感谢:

#include <stdio.h>
#include <math.h>
#include <string.h>

char* readLine_str_test_encrypt();
char* readLine_str_test_decrypt();

int main () { //body 

printf("%s\n", readLine_str_test_encrypt());

printf("%s\n", readLine_str_test_decrypt());


}

char* readLine_str_test_decrypt() {
static unsigned char string[9] = {100, 115, 119, 114, 90, 127, 120, 115, 22};

static int i = 0;

for (; i < 9; ++i) {
    string[i] ^= 22;
}

return (char*)string;
}

char* readLine_str_test_encrypt()
{
static unsigned char string[9] = "readLine";
char output[9];
char* output_start=output;
static int i =0;
for(; i < 9; ++i)
{
    //string[i] = string[i]^22;
    output_start += sprintf(output_start,"%d",string[i]^22);

}

return output_start;
}

我的解密功能运行成功。

【问题讨论】:

  • 为了便于我们人类阅读和理解:1) 一致地缩进代码。在每个左大括号“{”后缩进。在每个右大括号 '}' 之前取消缩进。 2) 通过空行分隔代码块(for、if、else、while、do...while、switch、case、default)。

标签: c string obfuscation abort


【解决方案1】:

readLine_str_test_encrypt 中,您将返回一个指向变量output 的指针。这个变量是一个局部变量,当函数退出时会超出范围。

改成

static char output[9];  

并且错误消失了。

详细了解为什么不应该返回局部变量 here

【讨论】:

    【解决方案2】:

    贴出的代码是:

    1. 包括一个头文件那些内容没有被使用
    2. 试图将一个 3 char 整数“sprintf()”转换为单个字节
    3. 返回指向局部变量的指针
    4. 没有用 NUL 字节终止要打印的字符串

    注意:size_tunsigned long int 的众所周知的定义

    以下建议的代码可以纠正上述所有问题以及其他一些问题。

    #include <stdio.h>   // printf()
    #include <stdlib.h>  // exit(), EXIT_FAILURE, malloc(), free()
    //#include <math.h>
    #include <string.h>  // strlen()
    
    char* readLine_str_test_encrypt( char *, size_t );
    char* readLine_str_test_decrypt( char *, size_t );
    
    
    int main ( void )
    { //body
        char string[] = "readLine";
    
        char * encryptedString = readLine_str_test_encrypt( string, strlen(string) );
        // Note: the encrypted string may not be printable
        printf("%s\n", encryptedString );
    
        char * decryptedString = readLine_str_test_decrypt( encryptedString, strlen(string) );
        printf( "%s\n", decryptedString );
    
        free( encryptedString );
        free( decryptedString );
    } // end function: main
    
    
    char* readLine_str_test_decrypt( char *encryptedString, size_t length)
    {
        char *string = NULL;
    
        if( NULL == ( string = malloc( length +1) ) )
        {// then malloc failed
            perror( "malloc for decrypted string failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        for ( size_t i=0; encryptedString[i]; ++i )
        {
            string[i] = encryptedString[i] ^22;
        }
    
        string[length] = '\0';
    
        return string;
    } // end function: readLine_str_test_decrypt
    
    
    char* readLine_str_test_encrypt( char *stringToEncrypt, size_t length )
    {
        char *output = NULL;
    
        if( NULL == (output = malloc( length+1 ) ) )
        { // then, malloc failed
            perror( "malloc for work area failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        for( size_t i=0; stringToEncrypt[i]; ++i)
        {
            output[i] = stringToEncrypt[i] ^22;
        }
    
        output[length] = '\0';
    
        return output;
    } // end function: readLine_str_test_encrypt
    

    上面代码的输出是:

    dswrZxs
    readLine
    

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 2014-12-13
      • 2017-05-11
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多