public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 07/12] term-utils: avoid error message string length couting errors
Date: Sun, 11 May 2014 20:26:44 +0100	[thread overview]
Message-ID: <1399836409-7769-7-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1399836409-7769-1-git-send-email-kerolasa@iki.fi>

Remove various magic numbers with either a string lenght count, or a
symbolic variable that is recognized by gdb.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/ttymsg.c | 57 ++++++++++++++++++++++++++---------------------------
 1 file changed, 28 insertions(+), 29 deletions(-)

diff --git a/term-utils/ttymsg.c b/term-utils/ttymsg.c
index fb2614f..3a2565d 100644
--- a/term-utils/ttymsg.c
+++ b/term-utils/ttymsg.c
@@ -58,6 +58,8 @@
 #include "pathnames.h"
 #include "ttymsg.h"
 
+enum { ERR_BUFLEN = MAXNAMLEN + 1024 };
+
 /*
  * Display the contents of a uio structure on a terminal.  Used by wall(1),
  * syslogd(8), and talkd(8).  Forks and finishes in child if write would block,
@@ -68,14 +70,16 @@
 char *
 ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) {
 	static char device[MAXNAMLEN];
-	static char errbuf[MAXNAMLEN+1024];
+	static char errbuf[ERR_BUFLEN];
 	size_t cnt, left;
 	ssize_t wret;
 	struct iovec localiov[6];
-	int fd, forked = 0, errsv;
+	int fd, forked = 0;
 
-	if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
-		return (_("internal error: too many iov's"));
+	if (iovcnt > sizeof(localiov) / sizeof(localiov[0])) {
+		sprintf(errbuf, _("internal error: too many iov's"));
+		return errbuf;
+	}
 
 	/* The old code here rejected the line argument when it contained a '/',
 	   saying: "A slash may be an attempt to break security...".
@@ -86,7 +90,7 @@ ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) {
 
 	if (strlen(line) + sizeof(_PATH_DEV) + 1 > sizeof(device)) {
 		sprintf(errbuf, _("excessively long line arg"));
-		return (errbuf);
+		return errbuf;
 	}
 	sprintf(device, "%s%s", _PATH_DEV, line);
 
@@ -96,12 +100,13 @@ ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) {
 	 */
 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
 		if (errno == EBUSY || errno == EACCES)
-			return (NULL);
-		if (strlen(strerror(errno)) > 1000)
-			return (NULL);
-		sprintf(errbuf, "%s: %m", device);
-		errbuf[1024] = 0;
-		return (errbuf);
+			return NULL;
+		if (strlen(device) + 2 + strlen(strerror(errno)) + 1 < ERR_BUFLEN)
+			sprintf(errbuf, "%s: %s", device, strerror(errno));
+		else
+			sprintf(errbuf, _("device open error message exceeded maximum length"));
+		errbuf[ERR_BUFLEN] = 0;
+		return errbuf;
 	}
 
 	for (cnt = left = 0; cnt < iovcnt; ++cnt)
@@ -139,19 +144,16 @@ ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) {
 			}
 			cpid = fork();
 			if (cpid < 0) {
-				if (strlen(strerror(errno)) > 1000)
-					sprintf(errbuf, _("cannot fork"));
-				else {
-					errsv = errno;
-					sprintf(errbuf,
-						 _("fork: %s"), strerror(errsv));
-				}
+				if (6 + strlen(strerror(errno)) + 1 < ERR_BUFLEN)
+					sprintf(errbuf, _("fork: %s"), strerror(errno));
+				else
+					sprintf(errbuf, _("cannot fork and error message length exceeded"));
 				close(fd);
-				return (errbuf);
+				return errbuf;
 			}
 			if (cpid) {	/* parent */
 				close(fd);
-				return (NULL);
+				return NULL;
 			}
 			forked++;
 			/* wait at most tmout seconds */
@@ -174,19 +176,16 @@ ttymsg(struct iovec *iov, size_t iovcnt, char *line, int tmout) {
 			warn(_("write failed: %s"), device);
 		if (forked)
 			_exit(EXIT_FAILURE);
-		if (strlen(strerror(errno)) > 1000)
+		if (strlen(device) + 2 + strlen(strerror(errno)) + 1 < ERR_BUFLEN)
+			sprintf(errbuf, "%s: %s", device, strerror(errno));
+		else
 			sprintf(errbuf, _("%s: BAD ERROR, message is "
 						 "far too long"), device);
-		else {
-			errsv = errno;
-			sprintf(errbuf, "%s: %s", device,
-				       strerror(errsv));
-		}
-		errbuf[1024] = 0;
-		return (errbuf);
+		errbuf[ERR_BUFLEN] = 0;
+		return errbuf;
 	}
 
 	if (forked)
 		_exit(EXIT_SUCCESS);
-	return (NULL);
+	return NULL;
 }
-- 
1.9.2


  parent reply	other threads:[~2014-05-11 19:27 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-11 19:26 [PATCH 01/12] tests: check /proc availability, and go-around if it is incomplete Sami Kerola
2014-05-11 19:26 ` [PATCH 02/12] cytune: remove from util-linux Sami Kerola
2014-05-12 12:11   ` Karel Zak
2014-05-11 19:26 ` [PATCH 03/12] blkid: remove unused variable Sami Kerola
2014-05-11 19:26 ` [PATCH 04/12] logger: fail when io vector number exceeds maximum Sami Kerola
2014-05-11 19:26 ` [PATCH 05/12] logger: check numeric priority and facility input values Sami Kerola
2014-05-11 19:26 ` [PATCH 06/12] build-sys: remove unnecessary void casts Sami Kerola
2014-05-11 19:26 ` Sami Kerola [this message]
2014-05-12 12:05   ` [PATCH 07/12] term-utils: avoid error message string length couting errors Karel Zak
2014-05-11 19:26 ` [PATCH 08/12] wall: replace magic number by named value Sami Kerola
2014-05-11 19:26 ` [PATCH 09/12] setterm: remove unused code Sami Kerola
2014-05-11 19:26 ` [PATCH 10/12] setterm: use string constant rather than #define Sami Kerola
2014-05-12 12:07   ` Karel Zak
2014-05-11 19:26 ` [PATCH 11/12] setterm: convert various constant number definitions to enums Sami Kerola
2014-05-11 19:26 ` [PATCH 12/12] setterm: convert remaining magic values to symbolic references Sami Kerola
2014-05-12 12:10   ` Karel Zak
2014-05-12 14:53     ` Sami Kerola
2014-05-12  7:05 ` [PATCH 01/12] tests: check /proc availability, and go-around if it is incomplete Bernhard Voelker
2014-05-12  9:13   ` Sami Kerola
2014-05-12 11:06   ` Karel Zak
2014-05-12 12:01 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1399836409-7769-7-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox