qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] QEMU produces invalid JSON due to locale-dependent code


From: Markus Armbruster
Subject: Re: [Qemu-devel] QEMU produces invalid JSON due to locale-dependent code
Date: Mon, 24 Aug 2015 12:05:02 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Copying maintainer.

Alberto Garcia <address@hidden> writes:

> We have this code in qjson.c to produce JSON from a QFloat:
>
>         QFloat *val = qobject_to_qfloat(obj);
>         char buffer[1024];
>         int len;
>
>         len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
>         while (len > 0 && buffer[len - 1] == '0') {
>             len--;
>         }
>
>
> The problem here is that the output of snprintf() is locale-dependent,
> so depending on the locale we might get a ',' as decimal separator.
> That's not allowed by JSON and it actually breaks scripts/qmp/qmp.
>
> This seems to happen because of GTK+ calling setlocale(). The easiest
> solution is probably to call setlocale(LC_NUMERIC, "C") before
> snprintf() (or at start-up ui/gtk.c), but opinions are welcome.

A library calling setlocale() is a big no-no in my book.

Overriding LC_NUMERIC as you propose should fix this particular bug.
However, exposing unprepared code to locale is not a good idea for other
categories as well.  LC_COLLATE and LC_CTYPE are even sneakier sources
of bugs in my experience.  I'd really, really prefer to stay in the "C"
locale *completely*.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]