public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dave@treblig.org>
To: util-linux@vger.kernel.org
Cc: kzak@redhat.com, rleigh@debian.org
Subject: [PATCH] get_line fixes for wide characters and overflows
Date: Fri, 19 Jul 2013 23:35:01 +0100	[thread overview]
Message-ID: <20130719223501.GA13613@gallifrey> (raw)
In-Reply-To: <20130716232033.GD9944@gallifrey>

Hi,
  This is a fix for the bug I reported with 'more' crashing:
http://marc.info/?l=util-linux-ng&m=137401887913346&w=2

It seems to work for me, but I'd appreciate another pair of eyes on it,
especially a pair with a better understanding of wide characters.

I could also see that it might be nice to add my failing case from the bug
as a regression test, but is there a good way to get those tests to run
with a given idea of terminal width/character encoding?

Dave


commit 96c2d8eaed137cbb9d62f0f50bdf689aa8a0f2e3
Author: Dr. David Alan Gilbert <dave@treblig.org>
Date:   Fri Jul 19 23:19:39 2013 +0100

    Ensure there is space at the end of the buffer for the termination
    characters, and that there is space for a full wide character.
    
    Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>

diff --git a/text-utils/more.c b/text-utils/more.c
index 3bbeede..b81e32a 100644
--- a/text-utils/more.c
+++ b/text-utils/more.c
@@ -43,6 +43,7 @@
  *	modified mem allocation handling for util-linux
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -167,6 +168,9 @@ int shellp;			/* A previous shell command exists */
 sigjmp_buf restore;
 char *Line;			/* Line buffer */
 size_t LineLen;			/* size of Line buffer */
+const unsigned int line_end_space = 2; /* For the nl and \0 at the end */
+const unsigned int wchar_max_bytes = 4; /* I don't see a portable way to
+					determine this */
 int Lpp = LINES_PER_PAGE;	/* lines per page */
 char *Clear;			/* clear screen */
 char *eraseln;			/* erase line */
@@ -827,7 +831,7 @@ static void prompt(char *filename)
 void prepare_line_buffer(void)
 {
 	char *nline;
-	size_t nsz = Mcol * 4;
+	size_t nsz = Mcol * wchar_max_bytes + line_end_space;
 
 	if (LineLen >= nsz)
 		return;
@@ -872,7 +876,7 @@ int get_line(register FILE *f, int *length)
 		Currline++;
 		c = Getc(f);
 	}
-	while (p < &Line[LineLen - 1]) {
+	while (p <= &Line[LineLen - (line_end_space + wchar_max_bytes)]) {
 #ifdef HAVE_WIDECHAR
 		if (fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
 			use_mbc_buffer_flag = 0;
@@ -961,7 +965,7 @@ int get_line(register FILE *f, int *length)
 					my_putstring(eraseln);
 					promptlen = 0;
 				} else {
-					for (--p; p < &Line[LineLen - 1];) {
+					for (--p; p <= &Line[LineLen - (1 + line_end_space)];) {
 						*p++ = ' ';
 						if ((++column & 7) == 0)
 							break;
@@ -1039,6 +1043,7 @@ int get_line(register FILE *f, int *length)
 	if (colflg && eatnl && Wrap) {
 		*p++ = '\n';	/* simulate normal wrap */
 	}
+	assert(p < &Line[LineLen]);
 	*length = p - Line;
 	*p = 0;
 	return (column);

  reply	other threads:[~2013-07-19 22:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-16 23:20 a repeatable 'more' crash and question about wide char and 'get_line' safety Dr. David Alan Gilbert
2013-07-19 22:35 ` Dr. David Alan Gilbert [this message]
2013-08-01 11:01   ` [PATCH] get_line fixes for wide characters and overflows Karel Zak
2013-08-01 12:57     ` Dr. David Alan Gilbert
2013-08-01 14:04       ` Karel Zak
2013-08-03 12:38         ` Dr. David Alan Gilbert
2013-08-05  8:40           ` 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=20130719223501.GA13613@gallifrey \
    --to=dave@treblig.org \
    --cc=kzak@redhat.com \
    --cc=rleigh@debian.org \
    --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