Browse Source

[fix] correct two segfault origin:
- if putting only one cote.
- if putting nothing before or after comma.
- add corresponding unit tests.

Moul 4 years ago
parent
commit
3b1ee0d1b5
3 changed files with 17 additions and 5 deletions
  1. 3 2
      src/detect_cotes.c
  2. 4 2
      src/main.c
  3. 10 1
      tests.sh

+ 3 - 2
src/detect_cotes.c

@@ -3,7 +3,7 @@
 /*
 ** arbitrary value to 50 blocks max
 ** While not scanned all string
-** If found cote:
+** If found cote (prevent segfault if only one cote):
 ** Else: no cote found: non coted block
 ** Increment block position
 */
@@ -21,7 +21,8 @@ void	detect_blocks(t_e *e, char *line)
 		while (line[pos] != '\0' && (line[pos] == ' ' || line[pos] == '\t'))
 			pos++;
 		e->b[b_pos] = (t_block*)malloc(sizeof(t_block));
-		if (line[pos] == '\'' || line[pos] == '\"' || line[pos] == '`')
+		if ((line[pos] == '\'' || line[pos] == '\"' || line[pos] == '`')\
+		&& line[pos + 1] != '\0')
 			pos = coted_block(e, line, pos, e->b[b_pos++]);
 		else
 			pos = non_coted_block(e, line, pos, e->b[b_pos++]);

+ 4 - 2
src/main.c

@@ -52,7 +52,8 @@ void	shell_script(t_e *e, char *script_name)
 
 /*
 ** Split at commas ';'.
-** Launch redirections
+** If string not empty:
+** - Launch redirections
 ** Calculate len on the string to compare coted block detection
 */
 
@@ -67,7 +68,8 @@ void	comma_split(t_e *e, char *line)
 	if ((cmds = cmd_splitting(e, line, ';')) != NULL)
 		while (cmds[++i] != NULL)
 		{
-			parse_redirection(e, cmds, i, len);
+			if (ft_strcmp(cmds[i], "") != 0)
+				parse_redirection(e, cmds, i, len);
 			len += ft_strlen(cmds[i]);
 		}
 }

+ 10 - 1
tests.sh

@@ -45,6 +45,9 @@ tests=(
 ## Parsing ##
 ";;;;,;;;; ;;;;"
 "; ;"
+";"
+"ls;"
+";ls"
 #"    \t  " # espaces, tabulations
 
 ## Pipe
@@ -123,12 +126,18 @@ tests=(
 "echo \"6789      \"  test   \"    2345\"" # two coted blocks seperated with non coted block
 "echo nasruitersaui \"6789      \" ransuite  \"    2345\"   anruistenrasuit" # big one
 #"echo \"!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~\""
-# test all ASCII char: check they are used to split str like '|' or ';'
+# test all ASCII char: check they are used to split str like ';', '>', '<' or '|'
 "echo \"nrsatu;nrtsa\""
+"ls ; echo \"nasruit><rnasuti\" > toto ; cat < toto"
 
 ### Back cotes: pipe
 "echo \`ls\`" # back cotes
 
+### Potential segfault
+"\'"
+"\""
+"\`"
+
 ### cotes non-closed
 
 ## Mix