Email List: Xaustin-group-lX
[All Lists]

Re: [1003.1(2008)/Issue 7 0000177]: mismatch between exampledescription

To: Austin Group Bug Tracker <noreply@xxxxxxxxxxxxx>
Subject: Re: [1003.1(2008)/Issue 7 0000177]: mismatch between exampledescription and code
From: Bruce Evans <brde@xxxxxxxxxxxxxxx>
Date: Fri, 6 Nov 2009 21:11:50 +1100 (EST)
Cc: austin-group-l@xxxxxxxxxxxxx
References: <6c9b21a4c8c44bc4f08776c66a9a3c8c@xxxxxx>
On Thu, 5 Nov 2009, Austin Group Bug Tracker wrote:

dynamic allocation example of fgets(), per discussion:
...
int f(FILE *fp)
{
 long line_max; /* used to avoid multiple sysconf() calls */
 char *line; /* the buffer */
/* declare other variables function needs */

/* get implementation instance's max expectable line length */
 line_max = sysconf(_SC_LINE_MAX);
/* account as practical for fgets() appending a NULL char */
 if (line_max = LONG_MAX) return EOVERFLOW;
This assigns to line_max instead of testing it, and thus always returns
EOVERFLOW.

 line_max ++;

 line = malloc(line_max); /* get the buffer space */
Overflow can still occur here (iff LONG_MAX > SIZE_MAX).  So the above
should be something like

	if (line_max == LONG_MAX || line_max >= SIZE_MAX)
		return EOVERFLOW;

if you want the example to be technically correct.  It still wouldn't
actually work on systems that set {LINE_MAX} + 1 to huge value (too large
to malloc()) in an attempt to force applications to support arbitrarily
long lines.

Bruce

<Prev in Thread] Current Thread [Next in Thread>