[BusyBox] [PATCH] Ignore extra empty lines in fgets_str

Matt Kraai kraai at alumni.carnegiemellon.edu
Thu Oct 18 10:37:51 UTC 2001


On Thu, Oct 18, 2001 at 09:12:13AM -0700, Stefan Soucek wrote:
> > I can't imagine that this is the correct solution.  If the first
> > character of the string we are returning is a newline, then return
> > NULL?  This bug should be fixed in dpkg, not fgets_str.
> 
> I think the fgets_str() was supposed to read a buffer until two following
> newlines. In the case of an extra newline after the *last* entry (this might
> happen when you edit the file and don't care about extra newlines at the
> end) the loop exits because of the EOF condition. But really it did not read
> until two newlines, thus I think the function should return NULL.
> 
> I agree this is probably not the cleanest fix. Maybe a check for
> strlen(linebuf)==1 && linebuf[0]=='\n' would be better. But this adds
> slightly more code and has the same effect.
> 
> I am not sure if this type of thing should be checked in the application
> since it would duplicate code across several applets that use this function.
> This is the reason why I proposed the patch for fgets_str().

Yes, the correct solution would be to have fgets_str behave like
getdelim, and return NULL if it reaches the end of the file before
reading the delimiter.

Could you please test the following patch?

Matt

Index: dpkg.c
===================================================================
RCS file: /var/cvs/busybox/dpkg.c,v
retrieving revision 1.51
diff -u -r1.51 dpkg.c
--- dpkg.c	2001/10/18 15:08:30	1.51
+++ dpkg.c	2001/10/18 15:23:48
@@ -769,7 +769,11 @@
 
 	/* Update previously known packages */
 	while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) {
-		tmp_string = strstr(control_buffer, "Package:") + 8;
+		if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) {
+			continue;
+		}
+
+		tmp_string += 8;
  		tmp_string += strspn(tmp_string, " \n\t");
 		package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
 		write_flag = FALSE;
Index: libbb/fgets_str.c
===================================================================
RCS file: /var/cvs/busybox/libbb/fgets_str.c,v
retrieving revision 1.3
diff -u -r1.3 fgets_str.c
--- libbb/fgets_str.c	2001/10/18 06:04:23	1.3
+++ libbb/fgets_str.c	2001/10/18 16:36:13
@@ -19,11 +19,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-/*
- * Continue reading from file until the terminating string is encountered.
- * Return data as string.
- * e.g. fgets_str(file, "\n"); will read till end of file
- */
+/* Read up to (and including) TERMINATING_STRING from FILE and return it.
+ * Return NULL on EOF.  */
 
 char *fgets_str(FILE *file, const char *terminating_string)
 {
@@ -37,12 +34,13 @@
 	while (1) {
 		ch = fgetc(file);
 		if (ch == EOF) {
-			break;
+			free(linebuf);
+			return NULL;
 		}
 
 		/* grow the line buffer as necessary */
 		while (idx > linebufsz - 2) {
-			linebuf = realloc(linebuf, linebufsz += 1000); /* GROWBY */
+			linebuf = xrealloc(linebuf, linebufsz += 1000);
 		}
 
 		linebuf[idx] = ch;
@@ -54,9 +52,6 @@
 			idx -= term_length;
 			break;
 		}
-	}
-	if (idx == 0) {
-		return NULL;
 	}
 	linebuf[idx] = '\0';
 	return(linebuf);





More information about the busybox mailing list