Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reinterpret_cast strangeness ( comma separated expression )

Tags:

c++

While debugging some of our code (C++) I found this:

inline std::string BufferToStr(
    const unsigned char* buffer,
    int index,
    size_t length)
{
   std::string retValue(reinterpret_cast<const char*>(&buffer[index], length));
   return retValue;
}

The issue with this code (overlooking the lack of pointer and string length checks) is that the closing parenthesis of the reinterpret_cast has been placed after length when it should have been after &buffer[index]. At first I thought that this was an issue with the compiler (using VS2013) but after successfully compiling it using both VS2012 and gcc 4.6.3, I've come to the conclusion that this is for some reason allowed. The code won't run on either Windows or Linux as the length parameter is used as the pointer.

So my question is why does this compile? Looking at the documentation of reinterpret_cast I can't find any documentation on it saying that you can pass a comma separated list of values to it and what it will do with it.

like image 265
william Avatar asked Aug 16 '13 09:08

william


People also ask

What is reinterpret_cast in C?

reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different. It does not check if the pointer type and data pointed by the pointer is same or not.

Is reinterpret_cast undefined Behaviour?

Although the reinterpret_cast itself might be unspecified behaviour, attempting to access the parameters once you've done the cast is undefined behaviour.

What is the use of reinterpret_cast?

The reinterpret_cast allows the pointer to be treated as an integral type. The result is then bit-shifted and XORed with itself to produce a unique index (unique to a high degree of probability). The index is then truncated by a standard C-style cast to the return type of the function.

Is reinterpret_cast safe?

The reinterpret_cast operator performs potentially unsafe type casts. It is most often used to cast a pointer to a different pointer type. Casting a pointer to a different pointer and back is usually safe and yields the original value.


2 Answers

That is due to the comma operator in c/c++. The code(an expression):

(&buffer[index], length)

is equivalent to (&buffer[index] take no effect):

(length)

so your code is equivalent to:

inline std::string BufferToStr(const unsigned char* buffer, int index, size_t length)
{
   std::string retValue(reinterpret_cast<const char*>(length));
   return retValue;
}
like image 21
lulyon Avatar answered Nov 15 '22 23:11

lulyon


reinterpret_cast accepts an expression. What you have in the parenthesis is an expression - the "," operator with two sub-expressions, which will evaluate to the result of last sub-expression.

like image 138
BartoszKP Avatar answered Nov 15 '22 23:11

BartoszKP