From: Willy Tarreau Date: Mon, 14 Apr 2014 13:04:54 +0000 (+0200) Subject: BUILD/MEDIUM: checks: get rid of sprintf() X-Git-Tag: v1.5-dev23~53 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a1dab55e1e325317e0264a642238e82608a14298;p=thirdparty%2Fhaproxy.git BUILD/MEDIUM: checks: get rid of sprintf() OpenBSD complains about our use of sprintf() here : src/checks.o(.text+0x44db): In function `process_chk': src/checks.c:766: warning: sprintf() is often misused, please use snprintf() This case was not really clean since the introduction of global.node BTW. Better change the API to support a size argument in the function and enforce the limit. --- diff --git a/src/checks.c b/src/checks.c index 1a3e865b87..24b763d0c6 100644 --- a/src/checks.c +++ b/src/checks.c @@ -733,7 +733,7 @@ void __health_adjust(struct server *s, short status) } } -static int httpchk_build_status_header(struct server *s, char *buffer) +static int httpchk_build_status_header(struct server *s, char *buffer, int size) { int sv_state; int ratio; @@ -763,12 +763,12 @@ static int httpchk_build_status_header(struct server *s, char *buffer) sv_state = 0; /* DOWN */ } - hlen += sprintf(buffer + hlen, + hlen += snprintf(buffer + hlen, size - hlen, srv_hlt_st[sv_state], (s->state & SRV_RUNNING) ? (s->check.health - s->check.rise + 1) : (s->check.health), (s->state & SRV_RUNNING) ? (s->check.fall) : (s->check.rise)); - hlen += sprintf(buffer + hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", + hlen += snprintf(buffer + hlen, size - hlen, "; name=%s/%s; node=%s; weight=%d/%d; scur=%d/%d; qcur=%d", s->proxy->id, s->id, global.node, (s->eweight * s->proxy->lbprm.wmult + s->proxy->lbprm.wdiv - 1) / s->proxy->lbprm.wdiv, @@ -780,7 +780,7 @@ static int httpchk_build_status_header(struct server *s, char *buffer) now.tv_sec < s->last_change + s->slowstart && now.tv_sec >= s->last_change) { ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); - hlen += sprintf(buffer + hlen, "; throttle=%d%%", ratio); + hlen += snprintf(buffer + hlen, size - hlen, "; throttle=%d%%", ratio); } buffer[hlen++] = '\r'; @@ -1558,7 +1558,7 @@ static struct task *process_chk(struct task *t) } else if ((check->type) == PR_O2_HTTP_CHK) { if (s->proxy->options2 & PR_O2_CHK_SNDST) - bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str)); + bo_putblk(check->bo, trash.str, httpchk_build_status_header(s, trash.str, trash.size)); bo_putstr(check->bo, "\r\n"); *check->bo->p = '\0'; /* to make gdb output easier to read */ }