[Freeassociation-devel] [libical] using snprintf for converting float values to strings

Dumez, Christophe christophe.dumez at intel.com
Fri Feb 24 05:05:19 PST 2012


Hi,

While testing evolution-data-server / libical on a Linux system with the
locale set to "fr_FR.utf8", I noticed that the floating point values were
being saved incorrectly in the the VEVENT.
They would look like "GEO:10\,000000\;11\,000000" instead of
"GEO:10.000000\;11.000000".

After looking at the code of libical, I identified the root cause in
libical/icalvalue.c:
icalvalue_float_as_ical_string_r() and icalvalue_geo_as_ical_string_r() are
using snprintf to convert the float values into strings.

However, according to the snprintf man page:
"""
For some numeric conversions a radix character ("decimal point") or
thousands' grouping character is used. The actual character used depends on
the LC_NUMERIC part of the locale. The POSIX locale uses '.' as radix
character, and does not have a grouping character. Thus,

printf("%'.2f", 1234567.89);

results in "1234567.89" in the POSIX locale, in "1234567,89" in the nl_NL
locale, and in "1.234.567,89" in the da_DK locale.
"""

I have libical 0.46 on my test system but this part of the code seems to be
the same in svn trunk.

I believe that another function than snprintf should be used to convert the
values, one that is not locale-aware. Alternatively, it is possible to
change the locale to "C" before the snprintf() call and restore the locale
after (see attached patch).
The second solution seems to be used in inkscape for example (
http://inkscape.sourcearchive.com/documentation/0.46-9ubuntu1/util_2units_8cpp-source.html).
This, however, is probably not thread-safe.

Bug report:
https://sourceforge.net/tracker/index.php?func=detail&aid=3493034&group_id=16077&atid=116077

Kr,
-- 
Christophe Dumez
Linux Software Engineer, PhD
Intel Finland Oy - Open Source Technology Center
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/libical-devel/attachments/20120224/c7a92613/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: snprintf-fix.patch
Type: text/x-patch
Size: 1448 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/libical-devel/attachments/20120224/c7a92613/attachment.bin>


More information about the libical-devel mailing list