summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-10-27 06:26:23 +0000
committerNick Mathewson <nickm@torproject.org>2004-10-27 06:26:23 +0000
commit22dc19b5908f73807e160c185156cb16a849049f (patch)
treec254cb9870af480e3f8e905f3dca088e7f67cfda /src/common/util.c
parent06fa8fc05fe36d7154e5c321a080607a21690808 (diff)
downloadtor-22dc19b5908f73807e160c185156cb16a849049f.tar.gz
tor-22dc19b5908f73807e160c185156cb16a849049f.zip
snprintf wrapper with consistant (though not C99) overflow behavior
svn:r2606
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 4084fe1bb8..2d09a32942 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2185,7 +2185,7 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
* "mask" is the Mask|Maskbits part...
* and "port" is the *|port|min-max part.
*/
-
+
if (strcmp(address,"*")==0) {
*addr_out = 0;
} else if (tor_inet_aton(address, &in) != 0) {
@@ -2318,6 +2318,41 @@ tor_parse_ulong(const char *s, int base, unsigned long min,
return 0;
}
+/** Replacement for snprintf. Differs from platform snprintf in two
+ * ways: First, always NUL-terminates its output. Second, always
+ * returns -1 if the result is truncated. (Note that this return
+ * behavior does <i>not</i> conform to C99; it just happens to be the
+ * easiest to emulate "return -1" with conformant implementations than
+ * it is to emulate "return number that would be written" with
+ * non-conformant implementations.) */
+int tor_snprintf(char *str, size_t size, const char *format, ...)
+{
+ va_list ap;
+ int r;
+ va_start(ap,format);
+ r = tor_vsnprintf(str,size,format,ap);
+ va_end(ap);
+ return r;
+}
+
+/** Replacement for vsnpritnf; behavior differs as tor_snprintf differs from
+ * snprintf.
+ */
+int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
+{
+ int r;
+#ifdef MS_WINDOWS
+ r = _vsnprintf(str, size, format, args);
+#else
+ r = vsnprintf(str, size, format, args);
+#endif
+ str[size-1] = '\0';
+ if (r < 0 || r >= size)
+ return -1;
+ return r;
+}
+
+
#ifndef MS_WINDOWS
struct tor_mutex_t {
};