[fixed]bug in os::Printer::print() with % on windows systems

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

[fixed]bug in os::Printer::print() with % on windows systems

Post by vitek »

The os::Printer::print() function passes a string as the format string to printf(), which is potentially dangerous and can result in undefined behavior if that string contains a format specifier. It is also unnecessarily inefficient as it hits the heap to copy a string just to append a newline.

Here is a quick testcase to show the problem.

Code: Select all

#include <irrlicht.h>
using namespace irr;

#ifdef _MSC_VER
#  pragma comment(lib, "Irrlicht.lib")
#endif

int main()
{
    IrrlichtDevice* device = createDevice(video::EDT_NULL);
    if (device == 0)
        return 1; // could not create selected driver.

    irr::ILogger* logger = device->getLogger();
    logger->setLogLevel(irr::ELL_INFORMATION);
    logger->log("code %is %not %safe%");

    device->drop();
    return 0;
}
It seems it would be better to write the method like this...

Code: Select all

void Printer::print(const c8* message)
{
#if !defined (_WIN32_WCE )
    OutputDebugString(message);
    OutputDebugString("\n");

    printf("%s\n", message);
#endif
}
Travis
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If OutputDebugString can handle multiple calls properly it looks much better, indeed. Therefore going to commit it now.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The only issue that I could think of would if some other thread managed to call OutputDebugString() between the two calls.

Travis
Post Reply