* Using vsnprintf or vasprintf in sid @ 2003-05-23 18:17 Dave Brolley 2003-05-26 4:47 ` Ben Elliston 2003-05-26 17:28 ` Dave Brolley 0 siblings, 2 replies; 4+ messages in thread From: Dave Brolley @ 2003-05-23 18:17 UTC (permalink / raw) To: sid Hi, I have a need for printf-like printing into a buffer, however I have no control over the input and so I don't know the size of the buffer which will be required. It looks like vsnprintf (_ISOC99_SOURCE) or vasprintf (_GNU_SOURCE) are what I need. Is it ok to use these function in sid and, if so, how do I get configure to add the necessary -Dxxxx to the sid compilations and (if necessary) specify the library to look in? Thanks, Dave ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Using vsnprintf or vasprintf in sid 2003-05-23 18:17 Using vsnprintf or vasprintf in sid Dave Brolley @ 2003-05-26 4:47 ` Ben Elliston 2003-05-26 5:18 ` Scott Dattalo 2003-05-26 17:28 ` Dave Brolley 1 sibling, 1 reply; 4+ messages in thread From: Ben Elliston @ 2003-05-26 4:47 UTC (permalink / raw) To: sid Hi Dave! Dave Brolley <brolley@redhat.com> writes: > I have a need for printf-like printing into a buffer, however I have > no control over the input and so I don't know the size of the buffer > which will be required. It looks like vsnprintf (_ISOC99_SOURCE) or > vasprintf (_GNU_SOURCE) are what I need. Is it ok to use these > function in sid and, if so, how do I get configure to add the > necessary -Dxxxx to the sid compilations and (if necessary) specify > the library to look in? I'm not aware of any policy (the problem with SID is that it lacks the strict policies and exciting politics of other free software projects!) that would prevent you from using either of these functions in SID, provided that you use Autoconf tests as you suggest. I think it's important to have a fall-back implementation, though, in the case that neither is available. Take a look at using AC_CHECK_FUNC to do the test and set a #define in config.h to indicate the function(s) presence. If you can, try and isolate the test to the configure script used in the part of the source tree you're working in rather than running the test in the top-level configure script. Cheers, Ben ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Using vsnprintf or vasprintf in sid 2003-05-26 4:47 ` Ben Elliston @ 2003-05-26 5:18 ` Scott Dattalo 0 siblings, 0 replies; 4+ messages in thread From: Scott Dattalo @ 2003-05-26 5:18 UTC (permalink / raw) Cc: sid On 26 May 2003, Ben Elliston wrote: > Hi Dave! > > Dave Brolley <brolley@redhat.com> writes: > > > I have a need for printf-like printing into a buffer, however I have > > no control over the input and so I don't know the size of the buffer > > which will be required. It looks like vsnprintf (_ISOC99_SOURCE) or > > vasprintf (_GNU_SOURCE) are what I need. Is it ok to use these > > function in sid and, if so, how do I get configure to add the > > necessary -Dxxxx to the sid compilations and (if necessary) specify > > the library to look in? > > I'm not aware of any policy (the problem with SID is that it lacks the > strict policies and exciting politics of other free software > projects!) that would prevent you from using either of these functions > in SID, provided that you use Autoconf tests as you suggest. I think > it's important to have a fall-back implementation, though, in the case > that neither is available. > > Take a look at using AC_CHECK_FUNC to do the test and set a #define in > config.h to indicate the function(s) presence. If you can, try and > isolate the test to the configure script used in the part of the > source tree you're working in rather than running the test in the > top-level configure script. IIRC, when I used vnsprintf in SDCC the Solaris users were affected. Here's the code in question: #ifdef USE_VSNPRINTF // Alas, vsnprintf is not ANSI standard, and does not exist // on Solaris (and probably other non-Gnu flavored Unixes). /*-----------------------------------------------------------------*/ /* SAFE_snprintf - like snprintf except the string pointer is */ /* after the string has been printed to. This is */ /* useful for printing to string as though if it */ /* were a stream. */ /*-----------------------------------------------------------------*/ void SAFE_snprintf(char **str, size_t *size, const char *format, ...) { va_list val; int len; if(!str || !*str) return; va_start(val, format); vsnprintf(*str, *size, format, val); va_end (val); len = strlen(*str); if(len > *size) { fprintf(stderr,"WARNING, it looks like %s has overflowed\n",__FUNCTION__); fprintf(stderr,"len = %d is > str size %d\n",len,*size); } *str += len; *size -= len; } #else // USE_VSNPRINTF // This version is *not* safe, despite the name. void SAFE_snprintf(char **str, size_t *size, const char *format, ...) { va_list val; int len; static char buffer[1024]; /* grossly conservative, but still not inherently safe */ if(!str || !*str) return; va_start(val, format); vsprintf(buffer, format, val); va_end (val); len = strlen(buffer); if(len > *size) { fprintf(stderr,"WARNING, it looks like %s has overflowed\n",__FUNCTION__); fprintf(stderr,"len = %d is > str size %d\n",len,*size); } strcpy(*str, buffer); *str += len; *size -= len; } #endif // USE_VSNPRINTF So the fix is a hack that really isn't fool-proof. Scott ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Using vsnprintf or vasprintf in sid 2003-05-23 18:17 Using vsnprintf or vasprintf in sid Dave Brolley 2003-05-26 4:47 ` Ben Elliston @ 2003-05-26 17:28 ` Dave Brolley 1 sibling, 0 replies; 4+ messages in thread From: Dave Brolley @ 2003-05-26 17:28 UTC (permalink / raw) To: Dave Brolley; +Cc: sid Thanks Ben and Scott for your suggestions. I do need Solaris support and, as it turns out, I chose an implementation using a large buffer with overflow checking, like Scott suggested, until I can figure out how the autoconf magic works. Dave Dave Brolley wrote: > Hi, > > I have a need for printf-like printing into a buffer, however I have > no control over the input and so I don't know the size of the buffer > which will be required. It looks like vsnprintf (_ISOC99_SOURCE) or > vasprintf (_GNU_SOURCE) are what I need. Is it ok to use these > function in sid and, if so, how do I get configure to add the > necessary -Dxxxx to the sid compilations and (if necessary) specify > the library to look in? > > Thanks, > Dave > ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-05-26 17:28 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-05-23 18:17 Using vsnprintf or vasprintf in sid Dave Brolley 2003-05-26 4:47 ` Ben Elliston 2003-05-26 5:18 ` Scott Dattalo 2003-05-26 17:28 ` Dave Brolley
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).