Bug in #! processing

Scot W. Hetzel swhetzel at gmail.com
Fri Oct 1 21:06:46 PDT 2004


The following patch changes the check for end of line, so that a comment
on the first line must be preceded by white space (' ' or '\t').

[main.c]
#include <stdio.h>

int main(int ac, char **av)
{
   int i;
   printf("Main.c test\n");
   for(i = 0; i < ac; i++) {
       printf("%s\n", av[i]);
   }
}

[tst.sh]
#! ./main -arg1 -#! # comment
echo ok

Running tst.sh now produces:

# ./tst.sh
Main.c test
./main
-arg1
-#!
./tst.sh

Index: imgact_shell.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/imgact_shell.c,v
retrieving revision 1.26
diff -u -r1.26 imgact_shell.c
--- imgact_shell.c	11 Jun 2003 00:56:54 -0000	1.26
+++ imgact_shell.c	1 Oct 2004 12:15:48 -0000
@@ -49,8 +49,11 @@
 	struct image_params *imgp;
 {
 	const char *image_header = imgp->image_header;
-	const char *ihp, *line_endp;
+	const char *ihp, *prev, *line_endp;
 	char *interp;
+	boolean_t comment = FALSE;
+
+	prev = NULL;
 
 	/* a shell script? */
 	if (((const short *) image_header)[0] != SHELLMAGIC)
@@ -73,7 +76,16 @@
 	/*
 	 * Find end of line; return if the line > MAXSHELLCMDLEN long.
 	 */
-	for (ihp = &image_header[2]; *ihp != '\n' && *ihp != '#'; ++ihp) {
+	for (ihp = &image_header[2]; *ihp != '\n' && !comment; ++ihp) {
+		if ( prev != NULL && 
+		   (( *prev == ' ' || *prev == '\t' ) && *ihp == '#' )) {
+			ihp = prev;
+			comment = TRUE;
+			/* Skip over trailing spaces */
+			while ((*ihp == ' ') || (*ihp == '\t')) ihp--;
+		} else
+			prev = ihp;
+
 		if (ihp >= &image_header[MAXSHELLCMDLEN])
 			return(ENAMETOOLONG);
 	}


More information about the freebsd-current mailing list