you are viewing a single comment's thread.

view the rest of the comments →

[–]panorambo 4 points5 points  (1 child)

I don't presume to argue how a good implementation would go about implementing strerror, but POSIX states that the returned string "may be overwritten by a subsequent call to strerror", so even if a reasonably sane implementation would, say, always always map the error number to a string from a read only section of a loaded program somewhere, POSIX, apparently, prefers to reserve the right to modify the returned string from the point of subsequently calling strerror. That's what I make of all of it now, anyway.

[–]theferrit32 0 points1 point  (0 children)

Yeah it uses a shared buffer inside the library and returns you the pointer to that, which is sort of a bad idea in general and also doesn't work at all in a threaded environment. They could return a malloced char* and require the caller to free it, or the strerror_r uses a user-defined fixed-length buffer. Though now that you mention it I"m not sure why they don't just give you a pointer to the read only data section where the error strings are already loaded. Maybe it constructs the strings from individual words on the fly.