Просмотр исходного кода

Add safe_snprintf
Author: jgg
Date: 2003-02-02 22:20:27 GMT
Add safe_snprintf

Arch Librarian лет назад: 22
Родитель
Сommit
1168596fe5
2 измененных файлов с 29 добавлено и 4 удалено
  1. 25 3
      apt-pkg/contrib/strutl.cc
  2. 4 1
      apt-pkg/contrib/strutl.h

+ 25 - 3
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		/*{{{*/
 // ---------------------------------------------------------------------

+ 4 - 1
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) \