Browse Source

[script] Remove arbitrary limit on script line lengths

tags/v0.9.4
Michael Brown 16 years ago
parent
commit
0ea821c7b7
2 changed files with 36 additions and 28 deletions
  1. 17
    0
      src/arch/i386/include/librm.h
  2. 19
    28
      src/image/script.c

+ 17
- 0
src/arch/i386/include/librm.h View File

210
 	return strlen ( ( void * ) buffer + offset );
210
 	return strlen ( ( void * ) buffer + offset );
211
 }
211
 }
212
 
212
 
213
+/**
214
+ * Find character in user buffer
215
+ *
216
+ * @v buffer		User buffer
217
+ * @v offset		Starting offset within buffer
218
+ * @v c			Character to search for
219
+ * @v len		Length of user buffer
220
+ * @ret offset		Offset of character, or <0 if not found
221
+ */
222
+static inline __attribute__ (( always_inline )) off_t
223
+memchr_user ( userptr_t buffer, off_t offset, int c, size_t len ) {
224
+	void *found;
225
+
226
+	found = memchr ( ( ( void * ) buffer + offset ), c, len );
227
+	return ( found ? ( found - ( void * ) buffer ) : -1 );
228
+}
229
+
213
 /**
230
 /**
214
  * Convert virtual address to user buffer
231
  * Convert virtual address to user buffer
215
  *
232
  *

+ 19
- 28
src/image/script.c View File

37
  * @ret rc		Return status code
37
  * @ret rc		Return status code
38
  */
38
  */
39
 static int script_exec ( struct image *image ) {
39
 static int script_exec ( struct image *image ) {
40
-	char cmdbuf[256];
41
 	size_t offset = 0;
40
 	size_t offset = 0;
42
-	size_t remaining;
41
+	off_t eol;
43
 	size_t len;
42
 	size_t len;
44
-	char *eol;
45
 	int rc;
43
 	int rc;
46
 
44
 
47
 	/* Temporarily de-register image, so that a "boot" command
45
 	/* Temporarily de-register image, so that a "boot" command
53
 
51
 
54
 	while ( offset < image->len ) {
52
 	while ( offset < image->len ) {
55
 	
53
 	
56
-		/* Read up to cmdbuf bytes from script into buffer */
57
-		remaining = ( image->len - offset );
58
-		len = sizeof ( cmdbuf );
59
-		if ( len > remaining )
60
-			len = remaining;
61
-		memset ( cmdbuf, 0, sizeof ( cmdbuf ) );
62
-		copy_from_user ( cmdbuf, image->data, offset, len );
54
+		/* Find length of next line, excluding any terminating '\n' */
55
+		eol = memchr_user ( image->data, offset, '\n',
56
+				    ( image->len - offset ) );
57
+		if ( eol < 0 )
58
+			eol = image->len;
59
+		len = ( eol - offset );
63
 
60
 
64
-		/* Find end of line */
65
-		eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
66
-		if ( ! eol )
67
-			eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
68
-		if ( ! eol ) {
69
-			DBG ( "Script line too long (max %zd bytes)\n",
70
-			      sizeof ( cmdbuf ) );
71
-			rc = -ENOEXEC;
72
-			goto done;
73
-		}
61
+		/* Copy line, terminate with NUL, and execute command */
62
+		{
63
+			char cmdbuf[ len + 1 ];
74
 
64
 
75
-		/* Mark end of line and execute command */
76
-		*eol = '\0';
77
-		DBG ( "$ %s\n", cmdbuf );
78
-		if ( ( rc = system ( cmdbuf ) ) != 0 ) {
79
-			DBG ( "Command \"%s\" failed: %s\n",
80
-			      cmdbuf, strerror ( rc ) );
81
-			goto done;
65
+			copy_from_user ( cmdbuf, image->data, offset, len );
66
+			cmdbuf[len] = '\0';
67
+			DBG ( "$ %s\n", cmdbuf );
68
+			if ( ( rc = system ( cmdbuf ) ) != 0 ) {
69
+				DBG ( "Command \"%s\" failed: %s\n",
70
+				      cmdbuf, strerror ( rc ) );
71
+				goto done;
72
+			}
82
 		}
73
 		}
83
 		
74
 		
84
 		/* Move to next line */
75
 		/* Move to next line */
85
-		offset += ( ( eol - cmdbuf ) + 1 );
76
+		offset += ( len + 1 );
86
 	}
77
 	}
87
 
78
 
88
 	rc = 0;
79
 	rc = 0;

Loading…
Cancel
Save