Date: Wed, 28 Nov 2001 21:48:58 -0700 From: Wes Peters <wes@softweyr.com> To: Bill Fenner <fenner@research.att.com> Cc: mike@FreeBSD.org, freebsd-standards@bostonradio.org Subject: Re: strerror_r() implementation Message-ID: <3C05BE3A.1DF1A539@softweyr.com> References: <20011125014216.A84711@espresso.q9media.com> <3C00A43B.9929E9C7@softweyr.com> <200111281854.KAA13574@windsor.research.att.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Bill Fenner wrote: > > I'm still confused by why you need ERR_LEN; strerror() does: > > if (uerr < sys_nerr) > return (char *)sys_errlist[uerr]; > > so the only possible return value from strerror_r() is stored in > strerror_r()'s tmp[40] -- so strerror() can stick with ebuf[40]. No. The array tmp[] is used only to collect the ASCII version of the errno *in reverse*. The strlcpy seeds strerrbuf with the string "Unknown error: ". The loop following strlcpy un-reverses the ascii error number onto the end of the string. Ah, I see the confusion. The size of ebuf in strerror doesn't need to be big enough to hold messages from sys_errlist, it only needs to be large enough to hold "Unknown error: " plus the length of a 64-bit number in ascii. How does this look? --- strerror.c.nxt Wed Nov 28 20:32:54 2001 +++ strerror.c Wed Nov 28 20:39:30 2001 @@ -86,18 +86,17 @@ } -/* - * NOTE: the following length should be enough to hold the longest defined - * error message in sys_errlist, defined in ../gen/errlst.c. This is a WAG - * that is better than the previous value. - */ -#define ERR_LEN 64 - char * strerror(num) int num; { unsigned int uerr; + + /* + * NOTE: the following length should be enough to hold the + * longest "Unknown error: " message above. + */ +#define ERR_LEN 55 static char ebuf[ERR_LEN]; uerr = num; /* convert to unsigned */ @@ -106,7 +105,7 @@ /* strerror can't fail so handle truncation semi-elegantly */ if (strerror_r(num, ebuf, (size_t) ERR_LEN) != 0) - ebuf[ERR_LEN - 1] = '\0'; + ebuf[ERR_LEN - 1] = '\0'; return ebuf; } The last change is a whitespace error. Mustn't have any style(9) nits, right? ;^) -- "Where am I, and what am I doing in this handbasket?" Wes Peters Softweyr LLC wes@softweyr.com http://softweyr.com/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3C05BE3A.1DF1A539>