Showing tag results for Code

Feb 4, 2004
Post comments count0
Post likes count0

Answers to exercises – mismatching new/delete

Raymond Chen
Raymond Chen

Answers to yesterday's exercises: What happens if you allocate with scalar "new" and free with vector "delete[]"? The scalar "new" will allocate a single object with no hidden counter. The vector "delete[]" will look for the hidden counter, which isn't there, so it will either crash (accessing nonexistent memory) or grab a random number and a...

Code
Feb 3, 2004
Post comments count0
Post likes count0

Mismatching scalar and vector new and delete

Raymond Chen
Raymond Chen

In a previous entry I alluded to the problems that can occur if you mismatch scalar "new" with vector "delete[]" or vice versa. There is a nice description of C++ memory management in C++ Gotchas: Avoiding Common Problems in Coding and Design on www.informit.com, and I encourage you to read at least the section titled Failure to Distinguish Sca...

Code
Jan 29, 2004
Post comments count0
Post likes count1

Integer overflow in the new[] operator

Raymond Chen
Raymond Chen

Integer overflows are becoming a new security attack vector. Mike Howard's article discusses some of the ways you can protect yourself against integer overflow attacks. One attack vector he neglects to mention is integer overflow in the new[] operator. This operator performs an implicit multiplication that is unchecked: If you study the code g...

Code
Jan 28, 2004
Post comments count0
Post likes count2

Another reason not to do anything scary in your DllMain: Inadvertent deadlock

Raymond Chen
Raymond Chen

Your DllMain function runs inside the loader lock, one of the few times the OS lets you run code while one of its internal locks is held. This means that you must be extra careful not to violate a lock hierarchy in your DllMain; otherwise, you are asking for a deadlock. (You do have a lock hierarchy in your DLL, right?) The loader lock is tak...

Code
Jan 27, 2004
Post comments count0
Post likes count1

Some reasons not to do anything scary in your DllMain

Raymond Chen
Raymond Chen

As everybody knows by now, you're not supposed to do anything even remotely interesting in your DllMain function. Oleg Lvovitch has written two very good articles about this, one about how things work, and one about what goes wrong when they don't work. Here's another reason not to do anything remotely interesting in your DllMain: It's common ...

Code
Jan 26, 2004
Post comments count0
Post likes count0

The hollow brush

Raymond Chen
Raymond Chen

What is the hollow brush for? The hollow brush is a brush that doesn't do anything. You can use it when you're forced to use a brush but you don't want to. As one example, you can use it as your class brush. Then when your program stops responding and Windows decide to do the "white flash" (see yesterday's entry), it grabs the hollow brush and en...

Code
Jan 20, 2004
Post comments count0
Post likes count1

ia64 – misdeclaring near and far data

Raymond Chen
Raymond Chen

As I mentioned yesterday, the ia64 is a very demanding architecture. Today I'll discuss another way that lying to the compiler will come back and bite you. The ia64 does not have an absolute addressing mode. Instead, you access your global variables through the r1 register, nicknamed "gp" (global pointer). This register always points to your globa...

Code
Jan 19, 2004
Post comments count0
Post likes count1

Uninitialized garbage on ia64 can be deadly

Raymond Chen
Raymond Chen

On Friday, we talked about some of the bad things that can happen if you call a function with the wrong signature. The ia64 introduces yet another possible bad consequence of a mismatched function signature which you may have thought was harmless. The CreateThread function accepts a LPTHREAD_START_ROUTINE, which has the function signature One th...

Code
Jan 16, 2004
Post comments count0
Post likes count1

How can a program survive a corrupted stack?

Raymond Chen
Raymond Chen

Continuing from yesterday: The x86 architecture traditionally uses the EBP register to establish a stack frame. A typical function prologue goes like this: This establishes a stack frame that looks like this, for, say, a __stdcall function that takes two parameters. Parameters can be accessed with positive offsets from EBP; for example, par...

Code