Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 Apr 2020 05:35:26 +0000 (UTC)
From:      "Jason A. Harmening" <jah@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r360368 - stable/12/usr.sbin/config
Message-ID:  <202004270535.03R5ZQdj091339@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jah
Date: Mon Apr 27 05:35:26 2020
New Revision: 360368
URL: https://svnweb.freebsd.org/changeset/base/360368

Log:
  MFC r359815: config(8): use sbuf to manage line buffers

Modified:
  stable/12/usr.sbin/config/main.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/usr.sbin/config/main.c
==============================================================================
--- stable/12/usr.sbin/config/main.c	Mon Apr 27 05:27:39 2020	(r360367)
+++ stable/12/usr.sbin/config/main.c	Mon Apr 27 05:35:26 2020	(r360368)
@@ -113,6 +113,8 @@ struct hdr_list {
 	struct hdr_list *h_next;
 } *htab;
 
+static struct sbuf *line_buf = NULL;
+
 /*
  * Config builds a set of files for building a UNIX
  * system given a description of the desired system.
@@ -304,6 +306,29 @@ usage(void)
 	exit(EX_USAGE);
 }
 
+static void
+init_line_buf(void)
+{
+	if (line_buf == NULL) {
+		line_buf = sbuf_new(NULL, NULL, 80, SBUF_AUTOEXTEND);
+		if (line_buf == NULL) {
+			errx(EXIT_FAILURE, "failed to allocate line buffer");
+		}
+	} else {
+		sbuf_clear(line_buf);
+	}
+}
+
+static char *
+get_line_buf(void)
+{
+	if (sbuf_finish(line_buf) != 0) {
+		errx(EXIT_FAILURE, "failed to generate line buffer, "
+		    "partial line = %s", sbuf_data(line_buf));
+	}
+	return sbuf_data(line_buf);
+}
+
 /*
  * get_word
  *	returns EOF on end of file
@@ -313,11 +338,10 @@ usage(void)
 char *
 get_word(FILE *fp)
 {
-	static char line[160];
 	int ch;
-	char *cp;
 	int escaped_nl = 0;
 
+	init_line_buf();
 begin:
 	while ((ch = getc(fp)) != EOF)
 		if (ch != ' ' && ch != '\t')
@@ -336,29 +360,20 @@ begin:
 		else
 			return (NULL);
 	}
-	cp = line;
-	*cp++ = ch;
+	sbuf_putc(line_buf, ch);
 	/* Negation operator is a word by itself. */
 	if (ch == '!') {
-		*cp = 0;
-		return (line);
+		return get_line_buf();
 	}
-	while ((ch = getc(fp)) != EOF && cp < line + sizeof(line)) {
+	while ((ch = getc(fp)) != EOF) {
 		if (isspace(ch))
 			break;
-		*cp++ = ch;
+		sbuf_putc(line_buf, ch);
 	}
-	if (cp >= line + sizeof(line)) {
-		line[sizeof(line) - 1] = '\0';
-		fprintf(stderr, "config: attempted overflow, partial line: `%s'",
-		    line);
-		exit(2);
-	}
-	*cp = 0;
 	if (ch == EOF)
 		return ((char *)EOF);
 	(void) ungetc(ch, fp);
-	return (line);
+	return (get_line_buf());
 }
 
 /*
@@ -369,11 +384,10 @@ begin:
 char *
 get_quoted_word(FILE *fp)
 {
-	static char line[512];
 	int ch;
-	char *cp;
 	int escaped_nl = 0;
 
+	init_line_buf();
 begin:
 	while ((ch = getc(fp)) != EOF)
 		if (ch != ' ' && ch != '\t')
@@ -392,7 +406,6 @@ begin:
 		else
 			return (NULL);
 	}
-	cp = line;
 	if (ch == '"' || ch == '\'') {
 		int quote = ch;
 
@@ -401,9 +414,8 @@ begin:
 			if (ch == quote && !escaped_nl)
 				break;
 			if (ch == '\n' && !escaped_nl) {
-				*cp = 0;
 				printf("config: missing quote reading `%s'\n",
-					line);
+					get_line_buf());
 				exit(2);
 			}
 			if (ch == '\\' && !escaped_nl) {
@@ -411,38 +423,23 @@ begin:
 				continue;
 			}
 			if (ch != quote && escaped_nl)
-				*cp++ = '\\';
-			if (cp >= line + sizeof(line)) {
-				line[sizeof(line) - 1] = '\0';
-				printf(
-				    "config: line buffer overflow reading partial line `%s'\n",
-				    line);
-				exit(2);
-			}
-			*cp++ = ch;
+				sbuf_putc(line_buf, '\\');
+			sbuf_putc(line_buf, ch);
 			escaped_nl = 0;
 		}
 	} else {
-		*cp++ = ch;
-		while ((ch = getc(fp)) != EOF && cp < line + sizeof(line)) {
+		sbuf_putc(line_buf, ch);
+		while ((ch = getc(fp)) != EOF) {
 			if (isspace(ch))
 				break;
-			*cp++ = ch;
+			sbuf_putc(line_buf, ch);
 		}
-		if (cp >= line + sizeof(line)) {
-			line[sizeof(line) - 1] = '\0';
-			printf(
-			    "config: line buffer overflow reading partial line `%s'\n",
-			    line);
-			exit(2);
-		}
 		if (ch != EOF)
 			(void) ungetc(ch, fp);
 	}
-	*cp = 0;
 	if (ch == EOF)
 		return ((char *)EOF);
-	return (line);
+	return (get_line_buf());
 }
 
 /*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202004270535.03R5ZQdj091339>