pgpool2.git / summary / log / commit / refs

commit    d1035b634e42dc10d9abb77d87fa86c014a861f4
Author:   Yugo Nagata <nagata@sraoss.co.jp>
Date:     Mon Sep 24 09:26:09 2012 +0000

    Replace some unsafe functions.
     - Replace sprintf with snprintf.
     - Replace strncpy with stnlcpy.


watchdog/watchdog.c | 6 +++--- watchdog/watchdog.h | 1 + watchdog/wd_if.c | 15 +++++++++------ watchdog/wd_list.c | 6 +++--- watchdog/wd_ping.c | 10 +++++----- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/watchdog/watchdog.c b/watchdog/watchdog.c index 1de6aae85..67b1053ed 100644 --- a/watchdog/watchdog.c +++ b/watchdog/watchdog.c @@ -179,7 +179,7 @@ wd_chk_sticky(void) /* check sticky bit of ifup command */ wd_get_cmd(cmd, pool_config->if_up_cmd); - sprintf(path,"%s/%s",pool_config->ifconfig_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->ifconfig_path,cmd); if (! has_sticky_bit(path)) { fprintf(stderr,"ifup[%s] doesn't have sticky bit\n",path); @@ -187,7 +187,7 @@ wd_chk_sticky(void) } /* check sticky bit of ifdown command */ wd_get_cmd(cmd, pool_config->if_down_cmd); - sprintf(path,"%s/%s",pool_config->ifconfig_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->ifconfig_path,cmd); if (! has_sticky_bit(path)) { fprintf(stderr,"ifdown[%s] doesn't have sticky bit\n",path); @@ -195,7 +195,7 @@ wd_chk_sticky(void) } /* check sticky bit of arping command */ wd_get_cmd(cmd, pool_config->arping_cmd); - sprintf(path,"%s/%s",pool_config->arping_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->arping_path,cmd); if (! has_sticky_bit(path)) { fprintf(stderr,"arping[%s] doesn't have sticky bit\n",path); diff --git a/watchdog/watchdog.h b/watchdog/watchdog.h index cff31f5de..52f6bf892 100644 --- a/watchdog/watchdog.h +++ b/watchdog/watchdog.h @@ -30,6 +30,7 @@ #include "libpq-fe.h" #define WD_MAX_HOST_NAMELEN (128) +#define WD_MAX_PATH_LEN (128) #define MAX_WATCHDOG_NUM (128) #define WD_SEND_TIMEOUT (1) diff --git a/watchdog/wd_if.c b/watchdog/wd_if.c index bfaf51ec9..654c42dd7 100644 --- a/watchdog/wd_if.c +++ b/watchdog/wd_if.c @@ -45,17 +45,19 @@ int wd_IP_up(void) { int rtn = WD_OK; - char path[128]; + char path[WD_MAX_PATH_LEN]; char cmd[128]; + if (WD_List->delegate_ip_flag == 0) { WD_List->delegate_ip_flag = 1; + wd_get_cmd(cmd,pool_config->if_up_cmd); - sprintf(path,"%s/%s",pool_config->ifconfig_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->ifconfig_path,cmd); rtn = exec_ifconfig(path,pool_config->if_up_cmd); wd_get_cmd(cmd,pool_config->arping_cmd); - sprintf(path,"%s/%s",pool_config->arping_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->arping_path,cmd); rtn = exec_ifconfig(path,pool_config->arping_cmd); } return rtn; @@ -64,13 +66,14 @@ int wd_IP_down(void) { int rtn = WD_OK; - char path[128]; + char path[WD_MAX_PATH_LEN]; char cmd[128]; + if (WD_List->delegate_ip_flag == 1) { WD_List->delegate_ip_flag = 0; wd_get_cmd(cmd,pool_config->if_down_cmd); - sprintf(path,"%s/%s",pool_config->ifconfig_path,cmd); + snprintf(path,sizeof(path),"%s/%s",pool_config->ifconfig_path,cmd); rtn = exec_ifconfig(path,pool_config->if_down_cmd); } @@ -112,7 +115,7 @@ exec_ifconfig(char * path,char * command) return WD_NG; } memset(buf,0,sizeof(buf)); - strncpy(buf,command,sizeof(buf)); + strlcpy(buf,command,sizeof(buf)); bp = buf; while (*bp == ' ') { diff --git a/watchdog/wd_list.c b/watchdog/wd_list.c index bdf6ffad3..7da0bbf25 100644 --- a/watchdog/wd_list.c +++ b/watchdog/wd_list.c @@ -76,10 +76,10 @@ wd_set_wd_list(char * hostname, int pgpool_port, int wd_port, char * delegate_ip } else { - strncpy(p->hostname, hostname, sizeof(p->hostname)); + strlcpy(p->hostname, hostname, sizeof(p->hostname)); p->pgpool_port = pgpool_port; p->wd_port = wd_port; - strncpy(p->delegate_ip, delegate_ip, sizeof(p->delegate_ip)); + strlcpy(p->delegate_ip, delegate_ip, sizeof(p->delegate_ip)); if (tv != NULL) { memcpy(&(p->tv),tv,sizeof(struct timeval)); @@ -105,8 +105,8 @@ wd_add_wd_list(WdDesc * other_wd) } for ( i = 0 ; i < other_wd->num_wd ; i ++) { - strcpy(other_wd->wd_info[i].delegate_ip, pool_config->delegate_IP); p = &(other_wd->wd_info[i]); + strlcpy(p->delegate_ip, pool_config->delegate_IP, sizeof(p->delegate_ip)); wd_set_wd_info(p); } return i; diff --git a/watchdog/wd_ping.c b/watchdog/wd_ping.c index 40e28f5ca..d6f1f9553 100644 --- a/watchdog/wd_ping.c +++ b/watchdog/wd_ping.c @@ -65,7 +65,7 @@ wd_is_upper_ok(char * server_list) len = strlen(server_list)+2; buf = malloc(len); memset(buf,0,len); - strncpy(buf,server_list,len); + strlcpy(buf,server_list,len); /* thread init */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); @@ -79,7 +79,7 @@ wd_is_upper_ok(char * server_list) { *ep = '\0'; } - strncpy(thread_arg[cnt].hostname,bp,sizeof(thread_arg[cnt].hostname)); + strlcpy(thread_arg[cnt].hostname,bp,sizeof(thread_arg[cnt].hostname)); rc = pthread_create(&thread[cnt], &attr, exec_ping, (void*)&thread_arg[cnt]); cnt ++; @@ -142,7 +142,7 @@ wd_is_unused_ip(char * ip) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); /* set hostname as a thread_arg */ - strncpy(thread_arg.hostname,ip,sizeof(thread_arg.hostname)); + strlcpy(thread_arg.hostname,ip,sizeof(thread_arg.hostname)); rc = pthread_create(&thread, &attr, exec_ping, (void*)&thread_arg); pthread_attr_destroy(&attr); @@ -173,9 +173,9 @@ exec_ping(void * arg) int pid, i = 0; int r_size = 0; char result[256]; - char ping_path[128]; + char ping_path[WD_MAX_PATH_LEN]; - sprintf(ping_path,"%s/ping",pool_config->ping_path); + snprintf(ping_path,sizeof(ping_path),"%s/ping",pool_config->ping_path); thread_arg = (WdInfo *)arg; memset(result,0,sizeof(result)); if (pipe(pfd) == -1) [parent: 54d20cb1adb5]