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
|