]> git.saurik.com Git - apt.git/commitdiff
Add safe_snprintf
authorArch Librarian <arch@canonical.com>
Mon, 20 Sep 2004 17:00:31 +0000 (17:00 +0000)
committerArch Librarian <arch@canonical.com>
Mon, 20 Sep 2004 17:00:31 +0000 (17:00 +0000)
Author: jgg
Date: 2003-02-02 22:20:27 GMT
Add safe_snprintf

apt-pkg/contrib/strutl.cc
apt-pkg/contrib/strutl.h

index b37cdd1bdedd2b3324ef09235e4d7b99827212bf..4987307be38e2a7479eec6dc465f7318ef6bae0a 100644 (file)
@@ -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               /*{{{*/
 // ---------------------------------------------------------------------
index b51a175900816aec2ef9ce41122caf57b3281fd2..353e78ac94cd2a3d9826ab31134cc392f1db5483 100644 (file)
@@ -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) \