View Issue Details

IDProjectCategoryView StatusLast Update
00020131003.1(2024)/Issue8System Interfacespublic2026-11-29 14:16
Reporterromanzilka Assigned To 
PrioritynormalSeverityEditorialTypeEnhancement Request
Status NewResolutionOpen 
NameRoman Žilka
Organization
User Reference
Sectionstrerror_r
Page Number0
Line Number0
Interp Status
Final Accepted Text
Summary0002013: Necessity of strerror_r()
DescriptionConsider the simple programming task: in a multi-threaded environment, obtain the error string corresponding to an error number using any of the standard strerror*() function(s). The string is to follow the current locale (thread-specific one, if set, global, if not). Assumptions: 1. strerror() is thread-safe, 2. strerror_l()'s output buffer can never become ovewritten or invalidated by strerror() (see issue 2011), 3. strerror_l()'s output buffer in a thread can never become ovewritten or invalidated by strerror_l() in another thread (though this is not what thread-safety is defined as). These complications would not change the meaning of this issue.

The informative part of the section on strerror*() of the standard declares strerror_r() unnecessary and recommends strerror_l().

const locale_t curloc = uselocale(0);
if (curloc == LC_GLOBAL_LOCALE) {
    curloc = duplocale(curloc);
    if (! curloc) fatal();
}
result = strerror_l(errnr, curloc);
freelocale(curloc);


The possibility of ENOMEM in duplocale() makes this path unusable. Let's try to involve strerror() then. It vaguely appears, after all, that the purpose of the *_l() functions in general is not the simple case when the current locale is called for.

const locale_t curloc = uselocale(0);
if (curloc != LC_GLOBAL_LOCALE)
    result = strerror_l(errnr, curloc);
else result = strerror(errnr);
// at this point, because of strerror(), result may point to an invalid string [CX]or even be an invalid pointer[/CX]
fatal();


Again, this path is unusable. Hence, strerror_r() is necessary, or at least useful. its target buffer can concievably be allocated on the stack.
Desired Action1. Change the sentence:

"Applications should use strerror_l() rather than strerror() or strerror_r() to avoid thread safety and possible alternative (non-conforming) versions of these functions in some implementations."

to

"Application developers are advised that implementations exist which provide a non-conforming strerror_r() by default and a conforming one after implementation-specific configuration."

Alternatively, remove the sentence altogether. Also see issue 2012.

2. Remove the sentence

"The strerror_l() function is required to be thread-safe, thereby eliminating the need for an equivalent to the strerror_r() function."

Also note that it may be slightly incorrect either way: the intention, I suppose, was "... thereby eliminating the need for the strerror_r() function."
TagsNo tags attached.

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2026-11-29 14:16 romanzilka New Issue