From 6881efcf6cff35ca2d52a2ca75b4625e9bce3d7a Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Mon, 22 Dec 2008 16:27:39 +0000 Subject: [PATCH] null terminating snprintf git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10907 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- libs/esl/src/esl.c | 18 ++++++++++++++++++ libs/esl/src/esl_config.c | 4 ++-- libs/esl/src/esl_event.c | 2 +- libs/esl/src/include/esl.h | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/libs/esl/src/esl.c b/libs/esl/src/esl.c index 5616b2003a..2c37f246f0 100644 --- a/libs/esl/src/esl.c +++ b/libs/esl/src/esl.c @@ -175,6 +175,24 @@ const char *esl_stristr(const char *instr, const char *str) return NULL; } +#ifdef WIN32 +#ifndef vsnprintf +#define vsnprintf _vsnprintf +#endif +#endif + +int esl_snprintf(char *buffer, size_t count, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vsnprintf(buffer, count-1, fmt, ap); + if (ret < 0) + buffer[count-1] = '\0'; + va_end(ap); + return ret; +} static void null_logger(const char *file, const char *func, int line, int level, const char *fmt, ...) { diff --git a/libs/esl/src/esl_config.c b/libs/esl/src/esl_config.c index cdc8836d06..9773559b08 100644 --- a/libs/esl/src/esl_config.c +++ b/libs/esl/src/esl_config.c @@ -43,7 +43,7 @@ int esl_config_open_file(esl_config_t *cfg, const char *file_path) if (file_path[0] == '/') { path = file_path; } else { - snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path); + esl_snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path); path = path_buf; } @@ -61,7 +61,7 @@ int esl_config_open_file(esl_config_t *cfg, const char *file_path) int last = -1; char *var, *val; - snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR); + esl_snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR); path = path_buf; if ((f = fopen(path, "r")) == 0) { diff --git a/libs/esl/src/esl_event.c b/libs/esl/src/esl_event.c index a901c52a61..d60c87ed0a 100644 --- a/libs/esl/src/esl_event.c +++ b/libs/esl/src/esl_event.c @@ -481,7 +481,7 @@ esl_status_t esl_event_serialize(esl_event_t *event, char **str, esl_bool_t enco if (encode) { esl_url_encode(hp->value, encode_buf, encode_len); } else { - snprintf(encode_buf, encode_len, "[%s]", hp->value); + esl_snprintf(encode_buf, encode_len, "[%s]", hp->value); } llen = strlen(hp->name) + strlen(encode_buf) + 8; diff --git a/libs/esl/src/include/esl.h b/libs/esl/src/include/esl.h index 1093820247..806d6bc2f1 100644 --- a/libs/esl/src/include/esl.h +++ b/libs/esl/src/include/esl.h @@ -284,6 +284,8 @@ char *esl_url_decode(char *s); const char *esl_stristr(const char *instr, const char *str); int esl_toupper(int c); int esl_tolower(int c); +int esl_snprintf(char *buffer, size_t count, const char *fmt, ...); + typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr);