change the key from const char * to QByteArray

if trGettext() gets called with a *text that resides
in the stack, the QHash will return incorrect values after
the second call of trGettext() with that *text.

Example (assuming nothing has been translated):

void func(const char *text) {
        char *translated = trGettext(text);
        doSomethingWith(translated);
}

func("foo"); (1)
func("bar"); (2)

(1) *translated is "foo"
(2) *translated should be "bar" but is "foo" because
    the key (const char*) points to the value "foo"
    which has been set in the previous call (1).

Signed-off-by: Maximilian Güntner <maximilian.guentner@gmail.com>
This commit is contained in:
Maximilian Güntner 2013-11-01 18:23:06 +01:00
parent 9e63539237
commit 2ef80930ff
2 changed files with 2 additions and 2 deletions

View file

@ -4,7 +4,7 @@
const char *gettextFromC::trGettext(const char *text)
{
QByteArray &result = translationCache[text];
QByteArray &result = translationCache[QByteArray(text)];
if (result.isEmpty())
result = tr(text).toUtf8();
return result.constData();

View file

@ -12,7 +12,7 @@ public:
static gettextFromC *instance();
const char *trGettext(const char *text);
void reset(void);
QHash <const char *, QByteArray> translationCache;
QHash <QByteArray , QByteArray> translationCache;
};
#endif // GETTEXTFROMC_H