Clean up and make robust the file loading code
authorJoão Abecasis <joao.abecasis@nokia.com>
Tue, 27 Mar 2012 15:32:43 +0000 (17:32 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 28 Mar 2012 18:22:57 +0000 (20:22 +0200)
commitcbe8c1014621e863c95ffc856d87a3cff12ec608
tree137c3bf64b8064b96dc2e9d35bb0c760761a2619
parent4fc7474805fbcbb979d485183f336a3172e86df5
Clean up and make robust the file loading code

The used_mmap variable was set to true the first time an mmap operation
was successful, but it was never reset back to false. While that can be
a good indicator that future calls might succeed it is not a guarantee.
Not properly resetting could mean we'd unmap memory allocated with new,
instead of deleting it.

Since that variable is only used inside defined(QT_USE_MMAP) blocks, its
declaration is scoped the same way.

While mmap is still handled "by hand", use QFile for the other
operations. Calling mmap here is less than ideal, as it prevents use of
other memory mapping methods, such as native Windows APIs, but is less
intrusive as it allows QTranslator to retain control over lifetime of
the map. Using QFile for remaining operations reduces the number of
filesystem operations.

The file size is now checked to be minimally sane (<4GB), the limit of
the 32-bit variable that will hold mapping's length. Translation files
should be expected to be much smaller in practice, but there isn't a
sane hard-limit. The file format is broken down to sections, each of
which has a 32-bit length.

Finally, when loading a file fails, release resources immediately,
instead of delaying to next load attempt or the destructor.

Change-Id: I5cc1b626a99d229e8861eb0fbafc42b928b6a122
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
src/corelib/kernel/qtranslator.cpp