Module: sip-router Branch: kamailio_3.0 Commit: 229496c7170bcc85f517a4985f7ab4bad553c8d3 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=229496c7...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Tue Feb 2 13:23:03 2010 +0100
core: use one buffer for int2str
- static declaration inside ut.h function results in hard copy to many places, affecting acc detection of int to str conversions in buffer - reported by Alex Balashov
---
ut.c | 3 +++ ut.h | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/ut.c b/ut.c index 7c63941..11d4fe7 100644 --- a/ut.c +++ b/ut.c @@ -42,6 +42,9 @@ #include "mem/mem.h" #include "globals.h"
+/* global buffer for ut.h int2str() */ +char ut_buf_int2str[INT2STR_MAX_LEN]; +
/* converts a username into uid:gid, * returns -1 on error & 0 on success */ diff --git a/ut.h b/ut.h index 6861af7..3e4d7a6 100644 --- a/ut.h +++ b/ut.h @@ -292,10 +292,10 @@ static inline char* int2str_base(unsigned int l, int* len, int base)
-/* returns a pointer to a static buffer containing l in asciiz & sets len */ -static inline char* int2str(unsigned int l, int* len) +/* print int to asciiz in a string buffer + * - be sure result buffer is at least INT2STR_MAX_LEN in size */ +static inline char* int2strbuf(unsigned int l, char *r, int* len) { - static char r[INT2STR_MAX_LEN]; int i; i=INT2STR_MAX_LEN-2; @@ -312,6 +312,13 @@ static inline char* int2str(unsigned int l, int* len) return &r[i+1]; }
+extern char ut_buf_int2str[INT2STR_MAX_LEN]; +/* returns a pointer to a static buffer containing l in asciiz & sets len */ +static inline char* int2str(unsigned long l, int* len) +{ + return int2strbuf(l, ut_buf_int2str, len); +} + /* Signed INTeger-TO-STRing: convers a long to a string * returns a pointer to a static buffer containing l in asciiz & sets len */ static inline char* sint2str(long l, int* len)
On Feb 02, 2010 at 13:25, Daniel-Constantin Mierla miconda@gmail.com wrote:
Module: sip-router Branch: kamailio_3.0 Commit: 229496c7170bcc85f517a4985f7ab4bad553c8d3 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=229496c7...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Tue Feb 2 13:23:03 2010 +0100
core: use one buffer for int2str
- static declaration inside ut.h function results in hard copy to many places, affecting acc detection of int to str conversions in buffer
- reported by Alex Balashov
[...]
-/* returns a pointer to a static buffer containing l in asciiz & sets len */ -static inline char* int2str(unsigned int l, int* len) +/* print int to asciiz in a string buffer
- be sure result buffer is at least INT2STR_MAX_LEN in size */
+static inline char* int2strbuf(unsigned int l, char *r, int* len)
I think adding another parameter for the buffer length would be better (read way safer) then relying on the caller providing a large enough buffer. E.g.: static inline char* int2strbuf(unsigned int l, char *r, int buf_len, int* res_len)
and if (unlikely(buf_len<INT2STR_MAX_LEN)){ *res_len=0; return 0; /* => if someone missuses it => crash */ }
{
static char r[INT2STR_MAX_LEN]; int i;
i=INT2STR_MAX_LEN-2;
@@ -312,6 +312,13 @@ static inline char* int2str(unsigned int l, int* len) return &r[i+1]; }
[...]
Andrei
On 2/2/10 2:26 PM, Andrei Pelinescu-Onciul wrote:
On Feb 02, 2010 at 13:25, Daniel-Constantin Mierlamiconda@gmail.com wrote:
Module: sip-router Branch: kamailio_3.0 Commit: 229496c7170bcc85f517a4985f7ab4bad553c8d3 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=229496c7...
Author: Daniel-Constantin Mierlamiconda@gmail.com Committer: Daniel-Constantin Mierlamiconda@gmail.com Date: Tue Feb 2 13:23:03 2010 +0100
core: use one buffer for int2str
- static declaration inside ut.h function results in hard copy to many places, affecting acc detection of int to str conversions in buffer
- reported by Alex Balashov
[...]
-/* returns a pointer to a static buffer containing l in asciiz& sets len */ -static inline char* int2str(unsigned int l, int* len) +/* print int to asciiz in a string buffer
- be sure result buffer is at least INT2STR_MAX_LEN in size */
+static inline char* int2strbuf(unsigned int l, char *r, int* len)
I think adding another parameter for the buffer length would be better (read way safer) then relying on the caller providing a large enough buffer.
thanks for suggestion, applied. Should be included in trunk? I don't think there are side effects to any ser modules.
Daniel
E.g.: static inline char* int2strbuf(unsigned int l, char *r, int buf_len, int* res_len)
and if (unlikely(buf_len<INT2STR_MAX_LEN)){ *res_len=0; return 0; /* => if someone missuses it => crash */ }
{
static char r[INT2STR_MAX_LEN]; int i;
i=INT2STR_MAX_LEN-2;
@@ -312,6 +312,13 @@ static inline char* int2str(unsigned int l, int* len) return&r[i+1]; }
[...]
Andrei
sr-dev mailing list sr-dev@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev