Received: from eggs.gnu.org ([2001:4830:134:3::10]:39826) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZToco-0005AO-Ox for qemu-devel@nongnu.org; Mon, 24 Aug 2015 06:05:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZTocj-0003Bo-Qf for qemu-devel@nongnu.org; Mon, 24 Aug 2015 06:05:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48346) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTocj-0003A8-MC for qemu-devel@nongnu.org; Mon, 24 Aug 2015 06:05:05 -0400 From: Markus Armbruster References: <20150818235754.GA31625@igalia.com> Date: Mon, 24 Aug 2015 12:05:02 +0200 In-Reply-To: <20150818235754.GA31625@igalia.com> (Alberto Garcia's message of "Wed, 19 Aug 2015 01:57:55 +0200") Message-ID: <87r3mtq9wx.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] QEMU produces invalid JSON due to locale-dependent code List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alberto Garcia Cc: qemu-devel@nongnu.org, Gerd Hoffmann Copying maintainer. Alberto Garcia 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*.