From 1168596fe505411e1fd3a28a5aa710a75d68500d Mon Sep 17 00:00:00 2001 From: Arch Librarian Date: Mon, 20 Sep 2004 17:00:31 +0000 Subject: Add safe_snprintf Author: jgg Date: 2003-02-02 22:20:27 GMT Add safe_snprintf --- apt-pkg/contrib/strutl.cc | 28 +++++++++++++++++++++++++--- apt-pkg/contrib/strutl.h | 5 ++++- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index b37cdd1bd..4987307be 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.cc,v 1.46 2002/11/22 07:15:23 doogie Exp $ +// $Id: strutl.cc,v 1.47 2003/02/02 22:20:27 jgg Exp $ /* ###################################################################### String Util - Some useful string functions. @@ -946,8 +946,8 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, /*}}}*/ // ioprintf - C format string outputter to C++ iostreams /*{{{*/ // --------------------------------------------------------------------- -/* This is used to make the internationalization strinc easier to translate - and to allow reordering of parameters */ +/* This is used to make the internationalization strings easier to translate + and to allow reordering of parameters */ void ioprintf(ostream &out,const char *format,...) { va_list args; @@ -959,6 +959,28 @@ void ioprintf(ostream &out,const char *format,...) out << S; } /*}}}*/ +// safe_snprintf - Safer snprintf /*{{{*/ +// --------------------------------------------------------------------- +/* This is a snprintf that will never (ever) go past 'End' and returns a + pointer to the end of the new string. The returned string is always null + terminated unless Buffer == end. This is a better alterantive to using + consecutive snprintfs. */ +char *safe_snprintf(char *Buffer,char *End,const char *Format,...) +{ + va_list args; + unsigned long Did; + + va_start(args,Format); + + if (End <= Buffer) + return End; + + Did = vsnprintf(Buffer,End - Buffer,Format,args); + if (Did < 0 || Buffer + Did > End) + return End; + return Buffer + Did; +} + /*}}}*/ // CheckDomainList - See if Host is in a , seperate list /*{{{*/ // --------------------------------------------------------------------- diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index b51a17590..353e78ac9 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.h,v 1.21 2001/05/29 05:09:44 jgg Exp $ +// $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $ /* ###################################################################### String Util - These are some useful string functions @@ -33,8 +33,10 @@ using std::ostream; #ifdef __GNUG__ // Methods have a hidden this parameter that is visible to this attribute #define APT_FORMAT2 __attribute__ ((format (printf, 2, 3))) +#define APT_FORMAT3 __attribute__ ((format (printf, 3, 4))) #else #define APT_FORMAT2 +#define APT_FORMAT3 #endif char *_strstrip(char *String); @@ -57,6 +59,7 @@ bool Hex2Num(string Str,unsigned char *Num,unsigned int Length); bool TokSplitString(char Tok,char *Input,char **List, unsigned long ListMax); void ioprintf(ostream &out,const char *format,...) APT_FORMAT2; +char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_FORMAT3; bool CheckDomainList(string Host,string List); #define APT_MKSTRCMP(name,func) \ -- cgit v1.2.3