-4

I'm fairly new to c/c++ programming and currently I'm working on some basic programms to get in touch with the language. My newest programm is a simple hex_xor function, that follows the instruction of Cryptopals Challenge 2. But I'm already getting errors and I'm assuming I am doing something horribly wrong with the pointers I am using.

Here is a part of my programm:

const char* hex_char_to_bin(char c)
{
     switch(toupper(c))
     {
             case '0': return "0000";
             case '1': return "0001";
             case '2': return "0010";
             case '3': return "0011";
             case '4': return "0100";
             case '5': return "0101";
             case '6': return "0110";
             case '7': return "0111";
             case '8': return "1000";
             case '9': return "1001";
             case 'A': return "1010";
             case 'B': return "1011";
             case 'C': return "1100";
             case 'D': return "1101";
             case 'E': return "1110";
             case 'F': return "1111";
     }
}

const char* hex_binary(const char* c){
     std::string result = "";
     for(int i = 0; i < strlen(c); i++){
             result += hex_char_to_bin(c[i]);
     }

     return result.c_str();
}

int main(){
    std::string s1 = "1c0111001f010100061a024b53535009181c";
    std::string s2 = "686974207468652062756c6c277320657965";

    const char* bin1 = hex_binary(s1.c_str());
    const char* bin2 = hex_binary(s2.c_str());
    std::cout << bin1 << "\n" << bin2 << std::endl;
    return 0;
}

The output is the following:

011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101
011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101

In both variables (bin1/2) is the binary conversion of the second hex-string. My aim is (obviously) to have both binary-strings saved in different variables, so I can proceed with my xor-function. Can someone point out where I am failing to achieve my goal and why? Other hints are welcome aswell!

James M
  • 18,506
  • 3
  • 48
  • 56
Ogofo
  • 356
  • 2
  • 6
  • 13
  • 4
    http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – chris Aug 15 '14 at 12:21
  • 4
    If you are going to use `C++` stop using `char*` and just use `std::string`. – Cory Kramer Aug 15 '14 at 12:21
  • You need first to decide if you want to learn C or C++, they are completely different languages, please decide which you wish to learn and then rephrase this question to refer to a single language. – Vality Aug 15 '14 at 12:21
  • The title of your question is terribly vague. – dandan78 Aug 15 '14 at 12:22
  • @chris' comment re: hex_binary return should suffice – polarysekt Aug 15 '14 at 12:22
  • @polarysekt no it doesnt. – UmNyobe Aug 15 '14 at 12:23
  • You are returning `return result.c_str();` (i.e. returning a local variable), the `result` string goes out of scope when the function returns, just return the whole `string` object. – Niall Aug 15 '14 at 12:26
  • @UmNyobe I suppose a reference to the offending function hex_binary as well as the analogy required to see an out of scope int variable referenced by pointer vs. a local std::string.c_str() return referenced by a pointer may have been just a bit vague – polarysekt Aug 15 '14 at 12:38

1 Answers1

4

You can't use result of c_str() when main string object is no longer alive. So, you're referencing already freed resources and facing undefined behavior.

If I were you, I'd change hex_binary() to return std::string and just return result back without using c_str()

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35