From 7369d96f8361c523a7ae4753391a9a7336a89fb8 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Tue, 8 Oct 2013 09:08:13 +0000 Subject: Re #1703: fixing general bugs. First installment: correct handling of snprintf return value git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4613 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/errno.c | 13 +++++++++---- pjlib/src/pj/lock.c | 2 +- pjlib/src/pj/log.c | 3 +++ pjlib/src/pj/os_error_symbian.cpp | 2 +- pjlib/src/pj/ssl_sock_ossl.c | 3 ++- 5 files changed, 16 insertions(+), 7 deletions(-) (limited to 'pjlib/src/pj') diff --git a/pjlib/src/pj/errno.c b/pjlib/src/pj/errno.c index ebfeb9ed..ff54bd4b 100644 --- a/pjlib/src/pj/errno.c +++ b/pjlib/src/pj/errno.c @@ -91,6 +91,8 @@ static const struct */ static int pjlib_error(pj_status_t code, char *buf, pj_size_t size) { + int len; + #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0 unsigned i; @@ -105,7 +107,10 @@ static int pjlib_error(pj_status_t code, char *buf, pj_size_t size) } #endif - return pj_ansi_snprintf( buf, size, "Unknown pjlib error %d", code); + len = pj_ansi_snprintf( buf, size, "Unknown pjlib error %d", code); + if (len < 1 || len >= (int)size) + len = size - 1; + return len; } #define IN_RANGE(val,start,end) ((val)>=(start) && (val)<(end)) @@ -199,9 +204,9 @@ PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode, len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode); } - if (len < 1) { - *buf = '\0'; - len = 0; + if (len < 1 || len >= (int)bufsize) { + len = bufsize - 1; + buf[len] = '\0'; } errstr.ptr = buf; diff --git a/pjlib/src/pj/lock.c b/pjlib/src/pj/lock.c index a521fd49..d7f4ffaf 100644 --- a/pjlib/src/pj/lock.c +++ b/pjlib/src/pj/lock.c @@ -683,7 +683,7 @@ PJ_DEF(void) pj_grp_lock_dump(pj_grp_lock_t *grp_lock) int len; len = pj_ansi_snprintf(start, max_len, "%s:%d ", ref->file, ref->line); - if (len < 1 || len > max_len) { + if (len < 1 || len >= max_len) { len = strlen(ref->file); if (len > max_len - 1) len = max_len - 1; diff --git a/pjlib/src/pj/log.c b/pjlib/src/pj/log.c index 47b6b0fc..bb808dac 100644 --- a/pjlib/src/pj/log.c +++ b/pjlib/src/pj/log.c @@ -445,6 +445,9 @@ PJ_DEF(void) pj_log( const char *sender, int level, print_len = pj_ansi_snprintf(pre, sizeof(log_buffer)-len, ""); } + if (print_len < 1 || print_len >= (int)(sizeof(log_buffer)-len)) { + print_len = sizeof(log_buffer) - len - 1; + } len = len + print_len; if (len > 0 && len < (int)sizeof(log_buffer)-2) { if (log_decor & PJ_LOG_HAS_CR) { diff --git a/pjlib/src/pj/os_error_symbian.cpp b/pjlib/src/pj/os_error_symbian.cpp index 4af9b30e..d04907fe 100644 --- a/pjlib/src/pj/os_error_symbian.cpp +++ b/pjlib/src/pj/os_error_symbian.cpp @@ -162,7 +162,7 @@ PJ_DEF(int) platform_strerror( pj_os_err_type os_errcode, } if (!len) { - len = pj_ansi_snprintf( buf, bufsize, "Symbian native error %d", + len = pj_ansi_snprintf( buf, bufsize-1, "Symbian native error %d", os_errcode); buf[len] = '\0'; } diff --git a/pjlib/src/pj/ssl_sock_ossl.c b/pjlib/src/pj/ssl_sock_ossl.c index 281e6f8a..7c2b8461 100644 --- a/pjlib/src/pj/ssl_sock_ossl.c +++ b/pjlib/src/pj/ssl_sock_ossl.c @@ -272,7 +272,8 @@ static pj_str_t ssl_strerror(pj_status_t status, errstr.slen = pj_ansi_snprintf(buf, bufsize, "Unknown OpenSSL error %lu", ssl_err); - + if (errstr.slen < 1 || errstr.slen >= (int)bufsize) + errstr.slen = bufsize - 1; return errstr; } -- cgit v1.2.3