-1

I'm aiming to be able to return unsigned char as a hex values, this is so I can pass the value from my client to server side.

How my data is generated before trying to be converted into hex:

unsigned char *TestClass::GetKey()
{
    // Generate a key of length 32 bytes
    memset(this->szKey, 0, 32);
    RAND_bytes(this->szKey, 32);

    return this->szKey;
}

This what i've currently got so far:

TestClass myTestClass;

void CJSCallDoc::OnDocumentComplete(LPCTSTR strURL,LPDISPATCH pDisp)
{
    unsigned char szKey = hex_print(myTestClass.GetKey());
}

unsigned char CJSCallDoc::hex_print(unsigned char* pv)
{
    unsigned char *p = pv;
    if (NULL == pv)
    {
        return NULL;
    }
    else
    {
        char storedString[256];

        size_t len = strlen((const char*)pv);
        size_t i = 0;

        for (; i < len; ++i)
        {
            strcpy_s(storedString, 256, "Test");
            strcat_s(storedString, reinterpret_cast<char*>(*p++));
        }

        return *storedString;
    }
}

The problem I'm having is with this line:

strcat_s(storedString, reinterpret_cast<char*>(*p++));

This line causes my application to crash and this is the following error I get:

Unhandled exception at 0x01664467 in TestApp.exe: 0xC0000005: Access violation reading location 0x000000FE.

and the error takes me to tcscat_s.inl:

---> while ((*p++ = *_SRC++) != 0 && --available > 0)
{
}

However when I try and do the following it works fine:

unsigned char CJSCallDoc::hex_print(unsigned char* pv)
{
    unsigned char *p = pv;
    if (NULL == pv)
    {
        return NULL;
    }
    else
    {
        char storedString[256];

        size_t len = strlen((const char*)pv);
        size_t i = 0;

        for (; i < len; ++i)
        {
            strcpy_s(storedString, 256, "Test");
            strcat_s(storedString, "WORKS");
        }

        return *storedString;
    }
}

Could someone explain to me, what I'm doing wrong and give me some advice in the right direction?

YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • There should be a comment added for reasoning to the downvote. So why the downvote? – YaBCK Nov 26 '15 at 13:06
  • 2
    This code is a mess. Why are you doing `reinterpret_cast(*p++)` ? You are casting a `char` value to a `char*` pointer? No wonder this doesn't work. – πάντα ῥεῖ Nov 26 '15 at 13:10
  • 1
    It's not a [MCVE](http://stackoverflow.com/help/mcve), to begin with. – DevSolar Nov 26 '15 at 13:11
  • 1
    The undefined behaviour is more than that, Bathseba. Returning the address of a local (auto) variable causes the caller to exhibit undefined behaviour if it dereferences that pointer, since the variable will no longer exist. The type or the value stored has nothing to do with it. – Peter Nov 26 '15 at 13:11
  • @πάνταῥεῖ But that's not reason to downvote, i'm still learning. Everyone has had messy code at some point. – YaBCK Nov 26 '15 at 13:12
  • @Bathsheba signed->unsigned should be defined. See conv.integral.2 – deviantfan Nov 26 '15 at 13:12
  • 1
    @Peter The statement doesn't return an address. – deviantfan Nov 26 '15 at 13:13
  • @ChrisBeckett Also it's completely unclear what you're trying to do. Getting the hex representation of an ASCII character? There are way simpler methods to achieve this. – πάντα ῥεῖ Nov 26 '15 at 13:15
  • @πάνταῥεῖ Sorry about that, If you re check my question. You can see how my data is generated before trying to change it to hex. – YaBCK Nov 26 '15 at 13:20
  • @ChrisBeckett The simplest way to get a hex representation is to use `ostringstream` and the `hex` manipulator, char must be casted to `int` to do this. – πάντα ῥεῖ Nov 26 '15 at 13:26
  • @πάνταῥεῖ Could you give any brief example of this? – YaBCK Nov 26 '15 at 13:27
  • 1
    Much like done [here](http://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout/19562163#19562163) but with an additional [`hex`](http://en.cppreference.com/w/cpp/io/manip/hex) manipulator before outputting. – πάντα ῥεῖ Nov 26 '15 at 13:30

1 Answers1

3

The problem is that you are casting your data to address, in:

strcat_s(storedString, reinterpret_cast<char*>(*p++));

*p++ is equal to whatever your string contains - first element is p from your example, so you are casting this p - decimal value 112, to char*. strcat_s will try to read string from this location which immediately ends with segfault.

marcinj
  • 48,511
  • 9
  • 79
  • 100