From bug-octave-request at bevo dot che dot wisc dot edu Mon Nov 4 15:56:04 2002 Subject: Re: sprintf prints at most 99 characters From: "John W. Eaton" To: Dirk Eddelbuettel Cc: Mirek Kwasniak , bug-octave@bevo.che.wisc.edu Date: Mon, 4 Nov 2002 15:55:51 -0600 On 3-Nov-2002, Dirk Eddelbuettel wrote: | On Sun, Nov 03, 2002 at 06:49:03PM +0100, Mirek Kwasniak wrote: | > On Sun, Nov 03, 2002 at 09:31:54AM -0800, Joseph P. Skudlarek wrote: | > > The problem also does NOT show up | > > in Octave 2.1.39 compiled with gcc 2.95.2 | > | > .. but gcc 3.0.4 and 3.2.1 (on Debian 386) suffer from this effect :( | | Might be worthwhile emailing the respective g++ maintainer (team) with a | heads, preferably with a self-contained C++ example exibiting the same | problem.... Volunteers? The libc manual says that the behavior of snprintf has changed. I think the following patch should fix the problem. Note that none of this code is used if you have ostream::vform, which is available with older versions of libstdc++. I'm not sure of the best way to handle the case when we are using a system that does not have ostream::vform and have old snprintf semantics. I guess we could use an autoconf macro. A quick search shows that there are a few other people with the same problem. Maybe a winner will emerge and we can just go with that. jwe 2002-11-04 John W. Eaton * cutils.c (octave_vsnprintf): Handle C99 snprintf semantics. Index: cutils.c =================================================================== RCS file: /usr/local/cvsroot/octave/src/cutils.c,v retrieving revision 1.11 diff -u -r1.11 cutils.c --- cutils.c 24 Oct 2002 21:49:45 -0000 1.11 +++ cutils.c 4 Nov 2002 21:39:04 -0000 at @ -122,6 +122,10 @@ // We manage storage. User should not free it, and its contents are // only valid until next call to vsnprintf. +#if defined __GNUC__ && __GNUC__ >= 3 +#define HAVE_C99_VSNPRINTF 1 +#endif + char * octave_vsnprintf (const char *fmt, va_list args) { at @ -130,21 +134,47 @@ static char *buf = 0; + int nchars; + if (! buf) buf = malloc (size); +#if defined (HAVE_C99_VSNPRINTF) + + nchars = vsnprintf (buf, size, fmt, args); + + if (nchars >= size) + { + size = nchars + 1; + buf = realloc (buf, size); + + if (buf) + vsnprintf (buf, size, fmt, args); + + return buf; + } + +#else + while (1) { - int nchars = vsnprintf (buf, size, fmt, args); + nchars = vsnprintf (buf, size, fmt, args); if (nchars > -1) return buf; else { size *= 2; + buf = realloc (buf, size); + + if (! buf) + return 0; } } + +#endif + #else return 0; #endif ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------