View Issue Details

IDProjectCategoryView StatusLast Update
00020141003.1(2024)/Issue8System Interfacespublic2026-11-30 10:47
Reporterromanzilka Assigned To 
PrioritylowSeverityEditorialTypeEnhancement Request
Status NewResolutionOpen 
NameRoman Žilka
Organization
User Reference
Sectionstrerror_r, string.h
Page Number0
Line Number0
Interp Status
Final Accepted Text
Summary0002014: strerror_r(): Error message length limit
DescriptionAs shown in issue 2013, strerror_r() may be the only feasible means of obtaining an error string. The string may have an arbitrary size, however, so if an application wants to ensure that the full string is obtained, a loop is required, which re-reallocates the target buffer, if it isn't large enough. And there's no guarantee that any particular size will be sufficient. Moreover, the function is extremely useful when combined with a buffer on the stack.

The proposal doesn't impose a direct length cap on the error string, instead allowing the implementation to space-optimize by setting the length cap itself, within bounds. For consistency, the limit is imposed on all the strerror*() functions.
Desired Action1. In the Description in the section on strerror_r(), just after the sentence segment:

"otherwise, if these functions complete successfully, the message string shall indicate that an unknown error occurred."

add the sentence (inside [CX]):

"The message string provided by these functions shall be at most STRERR_SIZE bytes large."

2. In the Description in the section on string.h add the sentence:

"The <string.h> header shall define the macro STRERR_SIZE, which expands to a positive integer denoting the maximum size in bytes of the error string returned by strerror(), strerror_l() and strerror_r(). STRERR_SIZE shall not be greater than 256 * MB_LEN_MAX, as defined in <limits.h>."
TagsNo tags attached.

Activities

jscott0

2026-11-29 23:09

reporter   bugnote:0007570

Last edited: 2026-11-29 23:11

The submitter's description reads
> The string may have an arbitrary size
and
> there's no guarantee that any particular size will be sufficient.

I believe the current text of the standard disagrees with these statements. In Issue 8, lines 69377-69378 read
> NAME
> strerror, strerror_l, strerror_r — get error message string

This affirms that any string returned by any of these functions is a "message string", and lines 10242-10243 ["XBD Headers — <limits.h>"] reads as
> # Other Invariant Values
> The <limits.h> header shall define the following symbolic constants:
> {NL_TEXTMAX}
> Maximum number of bytes in a message string.

Therefore, I believe the NL_TEXTMAX constant already offers the functionality described by the submitter's STRERR_SIZE, as an upper bound on the length of a string returned by strerror() and friends. Defining this constant with a meaningful value is required, and it must be at least _POSIX2_LINE_MAX. As it is an invariant value, applications never need to use sysconf() to obtain this constant's value.

However, the submitter is correct to point out that this may not be appropriate to allocate a buffer automatically ("on the stack"), as NL_TEXTMAX can be arbitrarily large (in the GNU C Library, for example, it's equivalent to INT32_MAX). Although I don't speak for any vendor or implementation, I am concerned that imposing a fixed absolute bound on a string that can be returned by strerror() and friends will adversely affect internationalization, including an implementation's ability to accommodate additional languages or charmaps that may be configured by an administrator. The reality that NL_TEXTMAX may be very large is something applications, perhaps, need to tolerate: dynamic allocation in some fashion may be inevitable to accommodate these systems.

romanzilka

2026-11-30 10:47

reporter   bugnote:0007571

Interesting, I wasn't aware of the fact that "message string" was a term. Taking NL_TEXTMAX into consideration, it is true that the string returned by strerror*() may not, in fact, have an arbitrarily large size, but because there's no limit set on NL_TEXTMAX itself, it may, in a practical sense, have an arbitrarily large size. If an implementation of the standard chooses not to limit itself realistically, the constant loses meaning.

It is most unfortunate if dynamic allocation needs to be involved in error reporting. Points given in the Description of this issue aside, the lack of available heap memory may well be the reason the reporting is happening in the first place. A programmer may very concievably want to exclude dynamic allocation from error reporting as a matter of principle. Still, if the reporting isn't possible because of ENOMEM, the application can resort to 1. a message that may have been cut short; 2. avoiding strerror*() (using its own vector of strings for some of the errnos, using generic strings such as "error nr. 15", ...); 3. strerror(), tolerating its issues; or another compromise yet. Admittedly, option nr. 1 is probably acceptable for most, but it is still unpleasant. Note that it is acceptable for most precisely because the error messages tend to really be short.

For the record, I thought about involving an exception for the POSIX locale in one way or another, but couldn't reach an acceptable solution.

Issue History

Date Modified Username Field Change
2026-11-29 15:18 romanzilka New Issue
2026-11-29 23:09 jscott0 Note Added: 0007570
2026-11-29 23:11 jscott0 Note Edited: 0007570
2026-11-30 10:47 romanzilka Note Added: 0007571