Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of the Heap-corruption error (Critical error c0000374) in C when converting hex into binary string?

Tags:

c

Part of my project, where we have to take an input file with hex numbers and convert them to MIPS code, I want to convert the hex into binary so it'd be easier for me to convert it into MIPS. However, when I run the code, it crashes and quits when it reaches the part where it calls the converter function. GDB says its a critical error c0000374. How do I fix this?

I have tried giving the target string more space and it doesn't seem to have any effect. I have also tried using malloc to no avail.

char* convertBinary (int hex)
{
    char* hexdec = calloc(9, sizeof(char));
    char* bin = calloc(SIZE+1, sizeof(char));

    snprintf(hexdec, SIZE, "%08X", hex);

    long int i; 

    for (i = 0; hexdec[i]; ++i) 
    { 
        switch (hexdec[i])
        { 
            case '0': 
                strcat(bin, "0000"); 
                break; 
            case '1': 
                strcat(bin, "0001"); 
                break; 
            case '2': 
                strcat(bin, "0010");
                break; 
            case '3': 
                strcat(bin, "0011"); 
                break; 
            case '4': 
                strcat(bin, "0100");
                break; 
            case '5':
                strcat(bin, "0101"); 
                break; 
            case '6': 
                strcat(bin, "0110");
                break; 
            case '7':
                strcat(bin, "0111"); 
                break; 
            case '8': 
                strcat(bin, "1000");
                break; 
            case '9':
                strcat(bin, "1001"); 
                break; 
            case 'A': 
            case 'a':
                strcat(bin, "1010"); 
                break; 
            case 'B': 
            case 'b':
                strcat(bin, "1011"); 
                break; 
            case 'C': 
            case 'c':
                strcat(bin, "1100"); 
                break; 
            case 'D': 
            case 'd':
                strcat(bin, "1101"); 
                break; 
            case 'E': 
            case 'e':
                strcat(bin, "1110"); 
                break; 
            case 'F': 
            case 'f':
                strcat(bin, "1111"); 
                break; 
            default: 
                printf("\nInvalid hexadecimal digit %c", 
                    hexdec[i]); 
        } 
    }

    return bin;
}

Also, in case it helps, here is the main function where I call this function

int main ()
{
    int command = 10010100; //This is in hex   

    char* binaryString = convertBinary(command);
    printf("The coverted binary is: %s\n", binaryString);
}

I expect the function to return a string of the binary numbers that have been converted from an 8 digit hex number. However, the program just quits and doesn't output anything. When debugged with GDB, it lays out a warning saying,

warning: Critical error detected c0000374
like image 895
Mohit Jha Avatar asked Sep 06 '25 02:09

Mohit Jha


1 Answers

There are multiple problems in your code:

  • You do not check the for memory allocation failure.
  • Since you allocate 9 bytes for hexdec, snprintf(hexdec, SIZE, "%08X", hex); should be

    snprintf(hexdec, 9, "%08X", hex);
    
  • The definition of SIZE is missing, as well as the #include lines. Post the complete source of the program exhibiting the offending behavior.

  • There is no need to loop until the end of the string hexdec: since you convert the hex value with %08X, just loop with:

    for (i = 0; i < 8; ++i) 
    
  • You should free(hexdec) before leaving the convertBinary function.

  • The code and comment do not agree in int command = 10010100; //This is in hex, which one is wrong? Probably both.
  • There is no need to use long type for i, int will suffice. Conversely, the argument hex should have unsigned int type.

Here is a simplified version of your code:

#include <stdio.h>
#include <stdlib.h>

char *convertBinary(unsigned int hex) {
    char *bin = calloc(33, 1);
    int i;

    if (bin) {
        for (i = 32; i-- > 0;) {
            bin[i] = '0' + (hex & 1);
            hex >>= 1;
        }
    }
    return bin;
}

int main() {
    int command = 0x10010100; //This is in hex   

    char *binaryString = convertBinary(command);
    if (binaryString == NULL) {
        printf("Memory allocation failure\n");
    } else {
        printf("The converted binary is: %s\n", binaryString);
        free(binaryString);
    }
    return 0;
}
like image 82
chqrlie Avatar answered Sep 08 '25 01:09

chqrlie