From 599e2fdc6c347d8fbb90a6f3c74f4f4418c4ce91 Mon Sep 17 00:00:00 2001 From: MassimilianoPinto Date: Mon, 24 Aug 2015 11:38:24 +0200 Subject: [PATCH] Small improvements for string buffers Small improvements an constants added in string buffers --- server/modules/include/blr.h | 3 +++ server/modules/routing/binlog/blr.c | 9 ++++---- server/modules/routing/binlog/blr_file.c | 26 +++++++++++++---------- server/modules/routing/binlog/blr_slave.c | 4 ++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/server/modules/include/blr.h b/server/modules/include/blr.h index 2f8ab6912..6f83f2e84 100644 --- a/server/modules/include/blr.h +++ b/server/modules/include/blr.h @@ -161,6 +161,9 @@ #define BLR_TYPE_STRING 0xf #define BLR_TYPE_INT 0x03 +/* string len for COM_STATISTICS output */ +#define BLRM_COM_STATISTICS_SIZE 1000 + /** * Some useful macros for examining the MySQL Response packets */ diff --git a/server/modules/routing/binlog/blr.c b/server/modules/routing/binlog/blr.c index ab9040fc6..a0838f5f3 100644 --- a/server/modules/routing/binlog/blr.c +++ b/server/modules/routing/binlog/blr.c @@ -1482,11 +1482,12 @@ ROUTER_SLAVE *slave; int blr_statistics(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, GWBUF *queue) { -char result[1000], *ptr; +char result[BLRM_COM_STATISTICS_SIZE + 1] = ""; +char *ptr; GWBUF *ret; unsigned long len; - snprintf(result, 1000, + snprintf(result, BLRM_COM_STATISTICS_SIZE, "Uptime: %u Threads: %u Events: %u Slaves: %u Master State: %s", (unsigned int)(time(0) - router->connect_time), (unsigned int)config_threadcount(), @@ -1907,10 +1908,10 @@ static int blr_check_binlog(ROUTER_INSTANCE *router) { "blr_read_events_all_events() ret = %i\n", n))); if (n != 0) { - char msg_err[1024 + 1] = ""; + char msg_err[BINLOG_ERROR_MSG_LEN + 1] = ""; router->master_state = BLRM_SLAVE_STOPPED; - snprintf(msg_err, 1024, "Error found in binlog %s. Safe pos is %lu", router->binlog_name, router->binlog_position); + snprintf(msg_err, BINLOG_ERROR_MSG_LEN, "Error found in binlog %s. Safe pos is %lu", router->binlog_name, router->binlog_position); /* set mysql_errno */ router->m_errno = 2032; diff --git a/server/modules/routing/binlog/blr_file.c b/server/modules/routing/binlog/blr_file.c index 35ad0c4c0..fda6ecbf2 100644 --- a/server/modules/routing/binlog/blr_file.c +++ b/server/modules/routing/binlog/blr_file.c @@ -82,7 +82,9 @@ int blr_file_write_master_config(ROUTER_INSTANCE *router, char *error); int blr_file_init(ROUTER_INSTANCE *router) { -char *ptr, path[PATH_MAX+1], filename[PATH_MAX+1]; +char *ptr; +char path[PATH_MAX+1] = ""; +char filename[PATH_MAX+1] = ""; int file_found, n = 1; int root_len, i; DIR *dirp; @@ -221,7 +223,7 @@ int fd; fsync(fd); close(router->binlog_fd); spinlock_acquire(&router->binlog_lock); - strncpy(router->binlog_name, file,BINLOG_FNAMELEN); + strncpy(router->binlog_name, file, BINLOG_FNAMELEN); router->binlog_fd = fd; spinlock_release(&router->binlog_lock); return 1; @@ -236,7 +238,7 @@ int fd; static void blr_file_append(ROUTER_INSTANCE *router, char *file) { -char path[1024]; +char path[PATH_MAX+1] = ""; int fd; strcpy(path, router->binlogdir); @@ -253,7 +255,7 @@ int fd; fsync(fd); close(router->binlog_fd); spinlock_acquire(&router->binlog_lock); - strncpy(router->binlog_name, file,BINLOG_FNAMELEN); + strncpy(router->binlog_name, file, BINLOG_FNAMELEN); router->current_pos = lseek(fd, 0L, SEEK_END); if (router->current_pos < 4) { if (router->current_pos == 0) { @@ -327,7 +329,7 @@ blr_file_flush(ROUTER_INSTANCE *router) BLFILE * blr_open_binlog(ROUTER_INSTANCE *router, char *binlog) { -char path[1025]; +char path[PATH_MAX + 1] = ""; BLFILE *file; spinlock_acquire(&router->fileslock); @@ -347,14 +349,14 @@ BLFILE *file; spinlock_release(&router->fileslock); return NULL; } - strncpy(file->binlogname, binlog,BINLOG_FNAMELEN+1); + strncpy(file->binlogname, binlog, BINLOG_FNAMELEN); file->refcnt = 1; file->cache = 0; spinlock_init(&file->lock); - strncpy(path, router->binlogdir,1024); - strncat(path, "/",1024); - strncat(path, binlog,1024); + strncpy(path, router->binlogdir, PATH_MAX); + strncat(path, "/", PATH_MAX); + strncat(path, binlog, PATH_MAX); if ((file->fd = open(path, O_RDONLY, 0666)) == -1) { @@ -670,7 +672,8 @@ struct stat statb; void blr_cache_response(ROUTER_INSTANCE *router, char *response, GWBUF *buf) { -char path[PATH_MAX+1], *ptr; +char path[PATH_MAX+1] = ""; +char *ptr; int fd; strncpy(path, router->binlogdir, PATH_MAX); @@ -706,7 +709,8 @@ GWBUF * blr_cache_read_response(ROUTER_INSTANCE *router, char *response) { struct stat statb; -char path[PATH_MAX+1], *ptr; +char path[PATH_MAX+1] = ""; +char *ptr; int fd; GWBUF *buf; diff --git a/server/modules/routing/binlog/blr_slave.c b/server/modules/routing/binlog/blr_slave.c index d681a5aed..cf6c0a476 100644 --- a/server/modules/routing/binlog/blr_slave.c +++ b/server/modules/routing/binlog/blr_slave.c @@ -955,7 +955,7 @@ static int blr_slave_send_maxscale_version(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave) { GWBUF *pkt; -char version[40]; +char version[80] = ""; uint8_t *ptr; int len, vers_len; @@ -1026,7 +1026,7 @@ static int blr_slave_send_maxscale_variables(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave) { GWBUF *pkt; -char name[40]; +char name[80]; char version[40]; uint8_t *ptr; int len, vers_len, seqno = 2;