Valgrind 错误:条件跳转或移动取决于将强制转换的 (unsigned char*) 传递给 (const char*) 时的未初始化值

问题描述 投票:0回答:0

错误信息:

==4540== 条件跳跃或移动取决于未初始化的值

==4540== 在 0x10B0B0:count_char (bmp.c:433)

==4540== by 0x10A287: bit_decrypt (bmp.c:130)

==4540== by 0x1096A3: test_bit (test_playfair.c:236)

==4540== by 0x1089DB: main (test_playfair.c:28)

char* bit_decrypt(const unsigned char* text) {
    if(text == NULL || text[0] == ' ' || !text[0]) return NULL;
    char* tmp_text = NULL;
    tmp_text = (char*)text;
    size_t text_s = count_char(tmp_text); //ERROR APPEARS HERE!
    char* result = calloc(text_s+1, sizeof(char));

    for(int i = 0; i < text_s; i++) {
        /*result = realloc(result, sizeof(char)+1);*/
        result[i] = b_decrypt(text[i]);
    }

    return result;
}

char b_decrypt(int encrypted) {
    int binary[8] = {0};
    to_binary(&encrypted, binary); //translate to binary

    reverse_binary(binary, 7);

    int xor_result[4] = {0};
    int xor_a[4] = {0};
    //Get 1 half of the target binary (changed positions)
    divide_array(binary, xor_a, 4, 8, false);
    //Get Xor result 
    divide_array(binary, xor_result, 4, 8, true);

    int xor_a_dec = to_decimal(xor_a, 4);
    int xor_result_dec = to_decimal(xor_result, 4);
    int xor_b_dec = xor_result_dec ^ xor_a_dec;
    int xor_b[4] = {0};
    to_binary(&xor_b_dec, xor_b); // get the second half of the target binary
    reverse_binary(xor_b, 3);


    //Recover the first half
    int xor_a_recovered[4] = {xor_a[1], xor_a[0], xor_a[3], xor_a[2]};

    //Combine
    int result_binary[8] = {0};
    for(int i = 0; i < 8; i++) {
        if(i < 4) result_binary[i] = xor_a_recovered[i];
        else result_binary[i] = xor_b[i-4];
    }
    int result_decimal = to_decimal(result_binary, 8);
    return (char) result_decimal;
}

void divide_array(int* base, int* destination, int length, int base_length, bool end) {
    if(!end) {
        for(int a = 0; a < length; a++) {
            
            destination[a] = base[a];
        }
    } else {
        int start_index = base_length - length;
        int point = 0;
        for(int a = start_index; a < base_length; a++) {
           destination[point++] = base[a]; 
        }
    }

}

void reverse_binary(int* binary, int length) {
    int reversed[length];
    reversed[0] = 0;
    for(int i = length; i >= 0; i--) {
       reversed[length - i] = binary[i]; 
    }
    for(int a = 0; a < length+1; a++) binary[a] = reversed[a];
}

int to_decimal(int* binary, int length) {
    int n = 0;
    int i;

    for (i = 0; i < length; ++i) {
        n <<= 1;
        n += binary[i];
    }

    return n;
}

void to_binary(int* decimal, int* binary) {
    int k;
   for(k = 0; *decimal > 0; k++) {
       binary[k] = *decimal%2;
        *decimal = *decimal/2;
   } 
}

count_char函数:

int count_char(const char* text) {
    int size = 0;

    while(true) {
        if(!text[size]) break;
        size++;
    }
    return size;
}

我也试过更换

char* tmp_text = NULL;
    tmp_text = (char*)text;
    size_t text_s = count_char(tmp_text); //ERROR APPEARS HERE!

   size_t text_s = count_char((const char*)text);

但错误依然存在

c debugging valgrind
© www.soinside.com 2019 - 2024. All rights reserved.