tags:

views:

157

answers:

5

Is it possible for a buffer overflow to occur from a cast?

If so, please explain how.

thanks.

+3  A: 

Only indirectly -- for example, if you have a buffer of char, and decide to work with Unicode so you cast the address of the buffer from char * to wchar_t *, but forget to adjust the number of "items" in that space to compensate for a wchar_t (normally) being larger than a char...

Jerry Coffin
+4  A: 

Maybe:

char p[1];
int *b = static_cast<int *>(p);
*b = 1;

Voila, buffer overrun! But only the write would overrun, doing the cast itself is not an overrun.

Matias Valdenegro
+3  A: 

Not really. A buffer overrun is caused by writing outside a buffer's boundary. So unless you do something stupid like this:

struct overrun
{
    explicit overrun(size_t pX)
    {
        char buffer[1];
        for (size_t i = 0; i < pX; ++i)
            buffer[i] = 5;
    }
};

int main()
{
    static_cast<overrun>(100); // oops
}

A cast isn't going to typically overrun a buffer. (And even here, one could argue it's not the cast that causes the overrun so much as its the construction). If you're having a real problem, ask.

GMan
is reading past memory bound considered buffer overrun?
aaa
@aaa: I don't think so. That's just undefined behavior (access violation).
GMan
+1 cause you beat me :)
Dean Harding
"Buffer overrun" isn't a well-defined term. From a security perspective, a read overrun can cause an uninteneded information exposure. Also, a static analysis tool should not even bother trying to determine what you're going to do with an out-of-bounds pointer. The mere forming of such a pointer is already UB. (Except of course the +1 end pointer)
MSalters
+3  A: 

Sort of, I suppose... say you have something like this:

class A
{
};

class B
{
public:
  operator A()
  {
    char buffer[5];
    strcpy(buffer, "1234512345"); // buffer overrun here

    A a;
    return a;
  }
};

// later...

B b;
A a = static_cast<A>(b); // triggers buffer overrun above

Technically, the cast is not required (since it's implicit) but that's one example where you could say it's possible. Of course, this is a silly example :-)

Dean Harding
Hehe, I win. :)
GMan
@GMan: Damnit !
Dean Harding
+1 since we thought alike.
GMan
+1  A: 

Not sure exactly how your analysis tool reports the culprit, but what about this?

char ra[] = "hi";
char &ref = ra[3];
std::cout << static_cast<int>(ref);

Of course it's evaluating the argument of the cast which has actually overrun, rather than the conversion as such.

GMan says that a read overrun doesn't count, but you could just as well assign the result of a cast to an out-of-bounds location, and some tool appear to report the cast as guilty.

Steve Jessop
Take what I say with a grain of salt, of course. I'm sure some people may consider an overrun *any* access to an out-of-bounds location.
GMan
@GMan: I guess the definition that matters, is the one used by Kevin's analysis tool. It would be more helpful if it did distinguish between the two cases.
Steve Jessop
@Gman: in common use, I'd argue a buffer overrun implies an out-of-bounds location reached by a bad index or excessive iteration from a buffer array, whereas a bad pointer dereference might have nothing to do with any such "buffer" (though there must have been intent to access a variable somewhere, and you could argue that any variable's a buffer). @Steve: could even distinguish as read buffer overrun and write buffer overrun.... ;-/
Tony