This post describes how to implement the OpenSSF Compiler Options Hardening Guide for C and C++ in CMake.
Read more...C++
Let’s say you have the following function:
|
|
What happens if this function is exported as an ordinal function from a DLL (not an inlined piece of code inside a header) and you call it from an EXE?
It works most of the time. When it doesn’t, it corrupts your heap and causes a spectacular mess.
Read more...Let’s say you have a C++ function that takes a function object as a parameter and calls it:
|
|
Now let’s say you want to pass a class’s member function to call_functor()
above, as in:
|
|
The STL has a pointer-to-member function adapter called std::mem_fun()
which almost gets us there. Unfortunately, it doesn’t quite meet our needs because it requires us to pass a pointer to an instance of C, as in:
What’s wrong with the following code?
|
|
Answer: my_pair
cannot be used as a key for a STL map because the operator<
violates the rule of strict weak ordering. More specifically, the operator is not antisymmetric. Consider the following:
I recently wrote a piece of code that looked something like the following:
|
|
What’s wrong with this code?
Read more...Let’s say you have the following unmanaged code:
|
|
Note that StreamWriter
’s destructor uses m_pStream
(perhaps by flushing the stream). This means that the order of destruction is important — StreamWriter
must be destroyed before its underlying Stream
is.
Quick quiz: What is the behavior of the following code:
|
|
Surprisingly, it depends! As I would expect, on many operating system / compiler combinations (such as gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
as tested by Keith Garner), the dereference of the null pointer leads to a segmentation fault. However, both Microsoft Visual Studio 6.0 and Visual Studio .NET 2003 print the message Caught exception.
and exit gracefully. Strange!
Avoid atoi()
and related functions: they do not distinguish between invalid input and a valid “0″ string. Use functions which properly report errors, such as strtod()
.
I ran across a piece of code recently that was using ostrstream
to convert a double to a string. The code looked something like:
|
|
This function was used to convert doubles to strings for insertion into an XML document, which were eventually parsed in an XSLT by the XPath number()
function. Most of the time it worked fine, but for really large numbers the number()
function failed and return NaN
. Why?
This is covered by any halfway-decent C++ book, but I believe it deserves reiteration: Use the RAII idiom. I don’t think I could explain RAII any better than HackCraft does in The RAII Programming Idiom.
Let me demonstrate how to use RAII with a semi-contrived example. Here’s the pre-RAII code:
|
|
In this case, the resource wrapped is HMODULE
, the resource acquisition function is LoadLibrary()
, and the resource release function is FreeLibrary()
. Beware of resources which have multiple resource release functions, such as Win32’s HANDLE
with FindClose()
and CloseHandle()
; for these cases you will typically have to write multiple RAII classes. I will call the wrapper class HModule
. Here’s how its use will look: