Commit 311e3ff0 by Zack Weinberg Committed by Zack Weinberg

gengtype.c (oprintf): Mostly revert changes from 2007-03-26...

	* gengtype.c (oprintf): Mostly revert changes from 2007-03-26;
	add comment explaining why vsnprintf cannot be used.

From-SVN: r123332
parent e89886a0
2007-03-29 Zack Weinberg <zack@mrtock.ucsd.edu>
* gengtype.c (oprintf): Mostly revert changes from 2007-03-26;
add comment explaining why vsnprintf cannot be used.
2007-03-29 Douglas Gregor <doug.gregor@gmail.com> 2007-03-29 Douglas Gregor <doug.gregor@gmail.com>
PR tree-optimization/30666 PR tree-optimization/30666
......
...@@ -1475,24 +1475,26 @@ create_file (const char *name, const char *oname) ...@@ -1475,24 +1475,26 @@ create_file (const char *name, const char *oname)
return f; return f;
} }
/* Print, like fprintf, to O. */ /* Print, like fprintf, to O.
N.B. You might think this could be implemented more efficiently
with vsnprintf(). Unfortunately, there are C libraries that
provide that function but without the C99 semantics for its return
value, making it impossible to know how much space is required. */
void void
oprintf (outf_p o, const char *format, ...) oprintf (outf_p o, const char *format, ...)
{ {
char *s;
size_t slength; size_t slength;
va_list ap;
/* Try first with the assumption that there is enough space. */ va_start (ap, format);
{ slength = vasprintf (&s, format, ap);
va_list ap; if (s == NULL || (int)slength < 0)
va_start (ap, format); fatal ("out of memory");
slength = vsnprintf (o->buf + o->bufused, o->buflength - o->bufused, va_end (ap);
format, ap);
va_end (ap);
}
if (o->bufused + slength >= o->buflength) if (o->bufused + slength > o->buflength)
{ {
/* There wasn't enough space. */
size_t new_len = o->buflength; size_t new_len = o->buflength;
if (new_len == 0) if (new_len == 0)
new_len = 1024; new_len = 1024;
...@@ -1501,21 +1503,10 @@ oprintf (outf_p o, const char *format, ...) ...@@ -1501,21 +1503,10 @@ oprintf (outf_p o, const char *format, ...)
} while (o->bufused + slength >= new_len); } while (o->bufused + slength >= new_len);
o->buf = XRESIZEVEC (char, o->buf, new_len); o->buf = XRESIZEVEC (char, o->buf, new_len);
o->buflength = new_len; o->buflength = new_len;
/* We now know that there is enough space. */
{
size_t slen2;
va_list ap;
va_start (ap, format);
slen2 = vsnprintf (o->buf + o->bufused, o->buflength - o->bufused,
format, ap);
va_end (ap);
gcc_assert (slen2 == slength);
gcc_assert (o->bufused + slen2 < o->buflength);
}
} }
memcpy (o->buf + o->bufused, s, slength);
o->bufused += slength; o->bufused += slength;
free (s);
} }
/* Open the global header file and the language-specific header files. */ /* Open the global header file and the language-specific header files. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment