Question

I have 3 global variables in my program. For some reason when I run string2bin on pad the contents of binaryMessage get overwritten and I can't figure out why. I increased the size of my malloc's but that didn't help. What am I doing wrong?

Note that randomPad() creates a string of random "hex" values, which should be converted to a binary string representing the hex string.

char * temp;
char * binaryMessage;
char * pad;

void process_message(char *s)

int main (int argc, const char * argv[]) {

        temp = (char *)malloc(sizeof(char *) *2048);

        binaryMessage = (char *)malloc(sizeof(char *) *2048);

        pad = (char *)malloc(sizeof(char *) * 2048);

        process_message("test");

}


char * char2bin ( unsigned char c ){
      static char bin[CHAR_BIT + 1] = {0};
      int i;

       for ( i = CHAR_BIT - 1; i >= 0; i-- ) {
            bin[i] = (c % 2) + '0';
            c /= 2;
       }

       return bin;
}

char* string2bin(char* str){
       int i;
       int len = strlen(str);

       sprintf(temp,"");
       for(i=0; i< len;i++){
             sprintf(temp, "%s%s",temp,char2bin(str[i]));
       }
       return temp;
 }

 char* randomPad(){
       int i;
       const char *hex_digits = "0123456789ABCDEF";
       char * p = (char*)malloc(sizeof(char*)*242);

       // clear old pad data
       //sprintf(pad,"");

       // create random string of 242 hex chars to use as pad
       for(i = 0; i< 242; i++){
             sprintf(p,"%s%c",p,hex_digits[ ( rand() % 16 ) ]);
       }
       return p;
 }
 void process_message(char *s){
       binaryMessage = string2bin(s);
       printf("m %s\nbm %s\n",s, binaryMessage);

       //get random one time pad
       pad = string2bin(randomPad());

       printf("m %s\nbm %s\n",s, binaryMessage);
 }

My expected output is:

m test
bm 01110100011001010111001101110100
m test
bm 01110100011001010111001101110100

My actual output is:

m test
bm 01110100011001010111001101110100
m test
bm 0011011100110001001110010100000100110010001110000011100001000101001100110100010000111000001101010100001100110010001100110011011100110111001100010011100000110100010001100011000101000100001101010011100000110101010000100011011000111000001101110011100100110010001110000011001100110010001100010011001101000100001100110011010101000110001100000011000100111000010001000011000101000011010000100011001000110101001110010011000100110100001110000011010000110010010000110011010001000001001101110100000101000001001101010011100000111001001101010011000100110000001101000011010101000001001101010100001001000100001110010011011000110111001110000011000100110101001100110100010101000100001101010011010100110011001110010011010001000010010000010100001100110011001100010011001000110000010001010100001100110001001101100011100001000100001100010100011000110100001100010100010100111001010001000011011000110000001110010011011100110001010001100011001101000001010001010100011000110000010000110011010000110101010000010011100001000010001100000011100100110010010001010011001000110110001101010011001001000011010001100100010001000110001110000011011101000100010001010100011000110010001100010011000001000101001110000011011100110111010001010011010000110010001101110011011101000101001101110011000100111000001110010100010101000010001100100011010100110010001101100011011001000011001101000100010100110110010000010011001000110011001100100011010000110110001101100011011101000110001100110011100100110001001110000011001101000110001100010011100000110101010001100011010000110000001100000011011101000010001110010100010101000001010000100011011100111000010000110100011000110010010000110100001100110111010001100011000101000100001101010011000100110011001100010100000101000101001100110011000000110001001110010011000100110111010000110100001000111000010001100100011000110010001100110100001101000100001110000011000001000101001110000100001001000100010001100100011000110001001100010011000001000110
Was it helpful?

Solution 2

In addition to the errors alk pointed out, you have four more problems.

1- This:

char * p = (char*)malloc(sizeof(char*)*242);

Should be this:

char * p = malloc(sizeof(char)*243);  // Do NOT cast malloc

2- This:

sprintf(p,"%s%c", p, hex_digits[ ( rand() % 16 ) ]);

Should be this:

sprintf(p+i,"%c",hex_digits[ ( rand() % 16 ) ]);

3- You're missing this:

*(p+242) = '\0';
return p;

4- And finally, this line of code:

binaryMessage = string2bin(s);

Replaces the binaryMessage pointer with the temp pointer value.

The best advice I can offer? Get rid of all the globals. And you need to seed the random number generator with something unpredictable (current time, for example) or you'll get the same sequence of "random" numbers every time.

OTHER TIPS

These lines are critical:

sprintf(temp, "%s%s", temp, char2bin(str[i]));

...

sprintf(p, "%s%c", p, hex_digits[ ( rand() % 16 ) ]);

From sprintf()'s man page:

Some programs imprudently rely on code such as the following

       sprintf(buf, "%s some further text", buf);

to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top