[xml] xmlStrPrintf function



Daniel,

I know that you would prefer not to add new functions which are not directly related
to XML parsing so the LibXML2 API which is already huge wouldn't grow.
But I would like to ask you to consider adding one more string processing
function xmlStrPrintf which is basicaly a wrapper for vsnprintf (see attached patch). The reasons why I think it would be a good idea to add this function are: - sprintf function has well known security issue. Providing a safe way to do the same thing in LibXML2 might help to reduce the number of security issues
  in the world :)
- snprintf (and friends) are still not available on all platforms. LibXML2 uses Trio to workaround these problems. However, since Trio is linked staticaly into LibXML2 it makes it hard to use Trio in libraries linked with LibXML2 (I run into this problem last night). Providing a trivial wrapper for Trio's
   functionality solves this problem.
- Using snprintf when it's available generates warnings about xmlChar*-->char*
   conversion. Instead of using BAD_CAST everywhere it's better to have it
   in one place.
- For the sake of completeness there have to be a sprintf like function for
   xmlChar * strings :)

Thanks,
Aleksey


Index: parser.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/parser.c,v
retrieving revision 1.316
diff -u -r1.316 parser.c
--- parser.c    30 Sep 2003 13:38:04 -0000      1.316
+++ parser.c    2 Oct 2003 16:47:13 -0000
@@ -41,6 +41,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/threads.h>
 #include <libxml/globals.h>
@@ -2397,6 +2398,33 @@
 
     while (*p != 0) p++; /* non input consuming */
     return(xmlStrncat(cur, add, p - add));
+}
+
+/**
+ * xmlStrPrintf:
+ * @buf:   the result buffer.
+ * @len:   the result buffer length.
+ * @msg:   the message with printf formatting.
+ * @...:   extra parameters for the message.
+ *
+ * Formats @msg and places result into @buf.
+ *
+ * Returns the number of characters written to @buf or -1 if an error occurs.
+ */
+int 
+xmlStrPrintf(xmlChar *buf, int len, const xmlChar *msg, ...) {
+    va_list args;
+    int ret;
+    
+    if((buf == NULL) || (msg == NULL)) {
+       return(-1);
+    }
+    
+    va_start(args, msg);
+    ret = vsnprintf(BAD_CAST buf, len, BAD_CAST msg, args);
+    va_end(args);
+    
+    return(ret);
 }
 
 /************************************************************************
Index: include/libxml/parser.h
===================================================================
RCS file: /cvs/gnome/gnome-xml/include/libxml/parser.h,v
retrieving revision 1.103
diff -u -r1.103 parser.h
--- include/libxml/parser.h     30 Sep 2003 12:36:01 -0000      1.103
+++ include/libxml/parser.h     2 Oct 2003 16:47:13 -0000
@@ -851,6 +851,12 @@
                                         const xmlChar *add,
                                         int len);
 
+XMLPUBFUN int XMLCALL  
+               xmlStrPrintf            (xmlChar *buf,
+                                        int len,
+                                        const xmlChar *msg,
+                                        ...);
+
 /*
  * Basic parsing Interfaces
  */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]