[hackers] [quark] Add esnprintf() and refactor some code || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Mon, 5 Mar 2018 01:00:33 +0100 (CET)

commit c8401c591fac7fd98349e05e595cdbe861998a90
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Mon Mar 5 00:59:37 2018 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Mon Mar 5 00:59:37 2018 +0100

    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.

diff --git a/http.c b/http.c
index cf08ada..98dfb00 100644
--- a/http.c
+++ b/http.c
_AT_@ -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);
                         }
                 }
_AT_@ -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));
_AT_@ -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);
                 }
 
diff --git a/util.c b/util.c
index 1fe9db8..d3db040 100644
--- a/util.c
+++ b/util.c
_AT_@ -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
diff --git a/util.h b/util.h
index 1f9e107..8408ce3 100644
--- a/util.h
+++ b/util.h
_AT_@ -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 **);
Received on Mon Mar 05 2018 - 01:00:33 CET

This archive was generated by hypermail 2.3.0 : Mon Mar 05 2018 - 01:12:25 CET