Question

Here is an example of one of the headaches I mean:

We have a multiplatform project that uses mostly Unicode strings for rendering text to the screen. On windows in VC++ the line:

swprintf(swWideDest, LEN, L"%s is a wide string", swSomeWideString);

compiles fine and prints the wide string into the other wide string. However, this should really be:

swprintf(swWideDest, LEN, L"%ls is a wide string", swSomeWideString);

Without replacing the '%s' with a '%ls' this will not work on other platforms. As testing in our environment on Windows is easier, quicker, and far simpler to debug. These kind of bugs can easily go unnoticed.

I know that the best solution is to write correct code in the first place, but under pressure simple mistakes are made, and in this particular case, the mistake can easily go unnoticed for a long time.

I suspect there are many variations on this sort of bug, that we are yet to enjoy.

Does anyone have a nice and neat way of finding these kind of bugs?

: D

Was it helpful?

Solution

As none of the functions of *printf family are typesafe you either

  • search for probable errors via regular expressions and fix them manually
  • use another approach that is typesafe, maybe based on stringstreams or boost.format

OTHER TIPS

You might want to have a look at FastFormat in case Boost.Format is too slow for your needs.

Compared to stringstreams and Boost.Format:

  • IOStreams: FastFormat.Format is faster than IOStreams, by between ~100-900%, in all cases
  • Boost.Format: FastFormat.Format is faster than Boost.Format, by between ~400-1650%, in all cases
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top