[Top][All Lists]

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

c-strtod threading and documentation problems

From: Michael Gold
Subject: c-strtod threading and documentation problems
Date: Tue, 13 Jan 2009 15:41:42 -0500
User-agent: Mutt/1.5.18 (2008-05-17)


I was planning to use the c-strtod module in a library, but noticed some
problems when looking at its implementation.

On platforms without strtod_l, it saves the current locale, switches to
"C" using setlocale, calls strtod, and restores the old locale. This is
not threadsafe because setlocale affects all threads in a program (and
also because setlocale doesn't seem to be threadsafe on all platforms).
The attached program demonstrates the problem -- if some thread calls
strtod while another is calling c_strtod, the call to strtod may return
an incorrect result in locales where the decimal point isn't '.'.

c_strtod will also call abort() on these platforms if there's no memory
to copy the old locale string, which makes it inappropriate for use in
libraries. This should be documented in the function description.

Finally, the documentation states c_strtod "operates as if the locale
encoding was ASCII", but the code doesn't actually convert the argument
from ASCII to the execution character set.

The first problem could probably be fixed by calling localeconv and
replacing '.' with the proper decimal point, which may require a
temporary memory allocation. Maybe another strtod function should be
defined that can return ENOMEM -- c_strtod could call this, and abort if
it gets that error (actually, c_strtod should be renamed xc_strtod, but
this would break compatibility with existing apps that use it).

-- Michael

Attachment: c-strtod-threadtest.c
Description: Text Data

Attachment: signature.asc
Description: Digital signature

reply via email to

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