From 72b6a65b2fab7325767ce72fc71b9cf45514764a Mon Sep 17 00:00:00 2001
From: Denis Vlasenko <vda.linux@googlemail.com>
Date: Tue, 21 Aug 2007 11:18:25 +0000
Subject: httpd: fix buglet in hex conversion. Remove alloca NULL checks (never
 happens, app just crashes if stack overflows) svlogd: cosmetic messages and
 style fixes

---
 networking/httpd.c | 36 ++++++++++++++++--------------------
 runit/svlogd.c     | 24 ++++++++++++++----------
 2 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/networking/httpd.c b/networking/httpd.c
index 7e60fc252..a888c2424 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -163,7 +163,7 @@ enum {
 #endif
 };
 
-static const uint16_t http_response_type[] = {
+static const uint16_t http_response_type[] ALIGN2 = {
 	HTTP_OK,
 	HTTP_MOVED_TEMPORARILY,
 	HTTP_REQUEST_TIMEOUT,
@@ -287,8 +287,6 @@ struct globals {
 } while (0)
 
 
-
-
 #define STRNCASECMP(a, str) strncasecmp((a), (str), sizeof(str)-1)
 
 /* Prototypes */
@@ -467,11 +465,6 @@ static void parse_conf(const char *path, int flag)
 
 	if (flag == SUBDIR_PARSE || cf == NULL) {
 		cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
-		if (cf == NULL) {
-			if (flag == FIRST_PARSE)
-				bb_error_msg_and_die(bb_msg_memory_exhausted);
-			return;
-		}
 		sprintf((char *)cf, "%s/%s", path, httpd_conf);
 	}
 
@@ -554,7 +547,7 @@ static void parse_conf(const char *path, int flag)
 			/* error status code */
 			int status = atoi(++p0);
 			/* c already points at the character following ':' in parse loop */
-			// c = strchr(p0, ':'); c++;
+			/* c = strchr(p0, ':'); c++; */
 			if (status < HTTP_CONTINUE) {
 				bb_error_msg("config error '%s' in '%s'", buf, cf);
 				continue;
@@ -575,9 +568,7 @@ static void parse_conf(const char *path, int flag)
 		if (*p0 == '/') {
 			/* make full path from httpd root / current_path / config_line_path */
 			cf = (flag == SUBDIR_PARSE ? path : "");
-			p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
-			if (p0 == NULL)
-				continue;
+			p0 = xmalloc(strlen(cf) + (c - buf) + 2 + strlen(c));
 			c[-1] = '\0';
 			sprintf(p0, "/%s%s", cf, buf);
 
@@ -694,9 +685,11 @@ static char *encodeString(const char *string)
 	char ch;
 
 	while ((ch = *string++)) {
-		// very simple check for what to encode
-		if (isalnum(ch)) *p++ = ch;
-		else p += sprintf(p, "&#%d;", (unsigned char) ch);
+		/* very simple check for what to encode */
+		if (isalnum(ch))
+			*p++ = ch;
+		else
+			p += sprintf(p, "&#%d;", (unsigned char) ch);
 	}
 	*p = '\0';
 	return out;
@@ -717,18 +710,21 @@ static char *encodeString(const char *string)
  */
 static unsigned hex_to_bin(unsigned char c)
 {
-	unsigned v = c | 0x20; /* lowercase */
-	v = v - '0';
+	unsigned v;
+
+	v = c - '0';
 	if (v <= 9)
 		return v;
-	v = v + ('0' - 'a');
+	/* c | 0x20: letters to lower case, non-letters
+	 * to (potentially different) non-letters */
+	v = (unsigned)(c | 0x20) - 'a';
 	if (v <= 5)
 		return v + 10;
 	return ~0;
 }
 /* For testing:
-void t(char c) { printf("'%c' %u\n", c, hex_to_bin(c)); }
-int main() { t('0'); t('9'); t('A'); t('F'); t('a'); t('f');
+void t(char c) { printf("'%c'(%u) %u\n", c, c, hex_to_bin(c)); }
+int main() { t(0x10); t(0x20); t('0'); t('9'); t('A'); t('F'); t('a'); t('f');
 t('0'-1); t('9'+1); t('A'-1); t('F'+1); t('a'-1); t('f'+1); return 0; }
 */
 static char *decodeString(char *orig, int option_d)
diff --git a/runit/svlogd.c b/runit/svlogd.c
index b8fa5645b..6c8747e96 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -513,22 +513,25 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
 	/* read config */
 	i = open_read_close("config", buf, sizeof(buf));
 	if (i < 0 && errno != ENOENT)
-		bb_perror_msg(WARNING": %s/config", ld->name);
+		bb_perror_msg(WARNING"%s/config", ld->name);
 	if (i > 0) {
 		if (verbose)
 			bb_error_msg(INFO"read: %s/config", ld->name);
 		s = buf;
 		while (s) {
 			np = strchr(s, '\n');
-			if (np) *np++ = '\0';
+			if (np)
+				*np++ = '\0';
 			switch (s[0]) {
 			case '+':
 			case '-':
 			case 'e':
 			case 'E':
+				/* Add '\n'-terminated line to ld->inst */
 				while (1) {
-					int l = asprintf(&new, "%s%s\n", ld->inst?:"", s);
-					if (l >= 0 && new) break;
+					int l = asprintf(&new, "%s%s\n", ld->inst ? : "", s);
+					if (l >= 0 && new)
+						break;
 					pause_nomem();
 				}
 				free(ld->inst);
@@ -578,7 +581,8 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
 		s = ld->inst;
 		while (s) {
 			np = strchr(s, '\n');
-			if (np) *np++ = '\0';
+			if (np)
+				*np++ = '\0';
 			s = np;
 		}
 	}
@@ -586,7 +590,7 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
 	/* open current */
 	i = stat("current", &st);
 	if (i != -1) {
-		if (st.st_size && ! (st.st_mode & S_IXUSR)) {
+		if (st.st_size && !(st.st_mode & S_IXUSR)) {
 			ld->fnsave[25] = '.';
 			ld->fnsave[26] = 'u';
 			ld->fnsave[27] = '\0';
@@ -600,8 +604,9 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
 			rmoldest(ld);
 			i = -1;
 		} else {
-			/* Be paranoid: st.st_size can be not just bigger, but WIDER! */
-			/* (bug in original svlogd. remove this comment when fixed there) */
+			/* st.st_size can be not just bigger, but WIDER!
+			 * This code is safe: if st.st_size > 4GB, we select
+			 * ld->sizemax (because it's "unsigned") */
 			ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size;
 		}
 	} else {
@@ -667,7 +672,7 @@ static int buffer_pread(int fd, char *s, unsigned len)
 	int i;
 
 	input.fd = 0;
-        input.events = POLLIN|POLLHUP|POLLERR;
+	input.events = POLLIN;
 
 	do {
 		if (rotateasap) {
@@ -744,7 +749,6 @@ static int buffer_pread(int fd, char *s, unsigned len)
 	return i;
 }
 
-
 static void sig_term_handler(int sig_no)
 {
 	if (verbose)
-- 
cgit v1.2.3-55-g6feb