Add esnprintf() and refactor some code
The (size_t) discards the case where the return value of snprintf is < 0. This is rather unlikely, but we'll keep it in mind anyway.
This commit is contained in:
parent
1879e14e79
commit
c8401c591f
3 changed files with 20 additions and 6 deletions
12
http.c
12
http.c
|
@ -346,8 +346,8 @@ http_send_response(int fd, struct request *r)
|
|||
|
||||
/* if we have a vhost prefix, prepend it to the target */
|
||||
if (s.vhost[i].prefix) {
|
||||
if ((size_t)snprintf(realtarget, sizeof(realtarget), "%s%s",
|
||||
s.vhost[i].prefix, realtarget) >= sizeof(realtarget)) {
|
||||
if (esnprintf(realtarget, sizeof(realtarget), "%s%s",
|
||||
s.vhost[i].prefix, realtarget)) {
|
||||
return http_send_status(fd, S_REQUEST_TOO_LARGE);
|
||||
}
|
||||
}
|
||||
|
@ -363,8 +363,8 @@ http_send_response(int fd, struct request *r)
|
|||
}
|
||||
|
||||
/* swap out target prefix */
|
||||
if ((size_t)snprintf(tmptarget, sizeof(tmptarget), "%s%s",
|
||||
s.map[i].to, realtarget + len) >= sizeof(tmptarget)) {
|
||||
if (esnprintf(tmptarget, sizeof(tmptarget), "%s%s",
|
||||
s.map[i].to, realtarget + len)) {
|
||||
return http_send_status(fd, S_REQUEST_TOO_LARGE);
|
||||
}
|
||||
memcpy(realtarget, tmptarget, sizeof(realtarget));
|
||||
|
@ -441,8 +441,8 @@ http_send_response(int fd, struct request *r)
|
|||
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
/* append docindex to target */
|
||||
if ((size_t)snprintf(realtarget, sizeof(realtarget), "%s%s",
|
||||
r->target, s.docindex) >= sizeof(realtarget)) {
|
||||
if (esnprintf(realtarget, sizeof(realtarget), "%s%s",
|
||||
r->target, s.docindex)) {
|
||||
return http_send_status(fd, S_REQUEST_TOO_LARGE);
|
||||
}
|
||||
|
||||
|
|
13
util.c
13
util.c
|
@ -61,6 +61,19 @@ timestamp(time_t t, char buf[TIMESTAMP_LEN])
|
|||
return buf;
|
||||
}
|
||||
|
||||
int
|
||||
esnprintf(char *str, size_t size, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vsnprintf(str, size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return (ret < 0 || (size_t)ret >= size);
|
||||
}
|
||||
|
||||
#define INVALID 1
|
||||
#define TOOSMALL 2
|
||||
#define TOOLARGE 3
|
||||
|
|
1
util.h
1
util.h
|
@ -49,6 +49,7 @@ void die(const char *, ...);
|
|||
#define TIMESTAMP_LEN 30
|
||||
|
||||
char *timestamp(time_t, char buf[TIMESTAMP_LEN]);
|
||||
int esnprintf(char *, size_t, const char *, ...);
|
||||
|
||||
void *reallocarray(void *, size_t, size_t);
|
||||
long long strtonum(const char *, long long, long long, const char **);
|
||||
|
|
Loading…
Reference in a new issue