changeset 13253:84e1c76098c5

413 Memory corruption in regcmp(3c) Reviewed by: garrett@nexenta.com Approved by: garrett@nexenta.com
author Jason King <jason@ansipunx.net>
date Tue, 07 Dec 2010 18:02:05 -0600
parents 660c760d3375
children 65ac809db5a8
files usr/src/lib/libc/port/regex/regcmp.c
diffstat 1 files changed, 30 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/lib/libc/port/regex/regcmp.c	Mon Dec 06 05:27:25 2010 +0000
+++ b/usr/src/lib/libc/port/regex/regcmp.c	Tue Dec 07 18:02:05 2010 -0600
@@ -27,8 +27,6 @@
 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
 /*	  All Rights Reserved	*/
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 /*
  * IMPORTANT NOTE:
  *
@@ -170,7 +168,7 @@
 
 	if (arg_strlen == 0)
 		return ((char *)0);
-	compile_startp = (char *)malloc(3 * arg_strlen);
+	compile_startp = (char *)malloc(3 * arg_strlen + 1);
 	if (compile_startp == (char *)0)
 		return ((char *)0);
 
@@ -455,7 +453,7 @@
 					compilep += expr_length;
 
 				} else if /* (current_char == DASH) && */
-					(dash_indicates_range == B_FALSE) {
+				    (dash_indicates_range == B_FALSE) {
 					/*
 					 * current_char is a DASH, but
 					 * either begins the entire
@@ -477,8 +475,12 @@
 					dash_indicates_range = B_TRUE;
 					first_char_in_range = current_char;
 
-				} else /* ((current_char == DASH && */
-				/* (dash_indicates_range == B_TRUE)) */ {
+				} else {
+					/*
+					 * ((current_char == DASH &&/
+					 * (dash_indicates_range == B_TRUE))
+					 */
+
 					/*
 					 * the DASH appears after a single
 					 * character that isn't
@@ -496,7 +498,7 @@
 						    arg_listp, compile_startp);
 
 					} else if (current_char ==
-						RIGHT_SQUARE_BRACKET) {
+					    RIGHT_SQUARE_BRACKET) {
 						/*
 						 * the preceding DASH is
 						 * the last character in the
@@ -509,10 +511,8 @@
 					} else if (valid_range(
 					    first_char_in_range,
 					    current_char) == B_FALSE) {
-
 						ERROR_EXIT(&regcmp_lock,
-						arg_listp, compile_startp);
-
+						    arg_listp, compile_startp);
 					} else {
 						/*
 						 * the DASH is part of a
@@ -687,10 +687,10 @@
 				}
 				if (*regex_typep != SIMPLE_GROUP) {
 					group_length = (unsigned int)
-						(compilep - regex_typep);
+					    (compilep - regex_typep);
 					if (group_length >= 1024) {
 						ERROR_EXIT(&regcmp_lock,
-						arg_listp, compile_startp);
+						    arg_listp, compile_startp);
 					}
 					high_bits = group_length >>
 					    TIMES_256_SHIFT;
@@ -699,7 +699,7 @@
 					*regex_typep =
 					    (unsigned char)
 					    ((unsigned int)
-						*regex_typep | high_bits);
+					    *regex_typep | high_bits);
 					regex_typep++;
 					*regex_typep =
 					    (unsigned char)low_bits;
@@ -733,7 +733,7 @@
 			} else {
 				can_repeat = B_FALSE;
 				*regex_typep = (unsigned char)
-				((unsigned int)*regex_typep | ZERO_OR_MORE);
+				    ((unsigned int)*regex_typep | ZERO_OR_MORE);
 			}
 			break; /* end case '*' */
 
@@ -751,12 +751,12 @@
 
 			if (can_repeat == B_FALSE) {
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			} else {
 				can_repeat = B_FALSE;
 				*regex_typep =
-					(unsigned char)((unsigned int)*
-					regex_typep | ONE_OR_MORE);
+				    (unsigned char)((unsigned int)*
+				    regex_typep | ONE_OR_MORE);
 			}
 			break; /* end case '+' */
 
@@ -787,15 +787,15 @@
 
 			if (can_repeat == B_FALSE) {
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			}
 			can_repeat = B_FALSE;
 			*regex_typep = (unsigned char)((unsigned int)*
-					regex_typep | COUNT);
+			    regex_typep | COUNT);
 			count_length = get_count(&min_count, regexp);
 			if (count_length <= 0) {
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			}
 			regexp += count_length;
 
@@ -810,28 +810,28 @@
 					max_count = UNLIMITED;
 				} else { /* {min_count,max_count} */
 					count_length = get_count(
-						&max_count, regexp);
+					    &max_count, regexp);
 					if (count_length <= 0) {
 						ERROR_EXIT(&regcmp_lock,
-						arg_listp, compile_startp);
+						    arg_listp, compile_startp);
 					}
 					regexp += count_length;
 					if (*regexp != RIGHT_CURLY_BRACE) {
 						ERROR_EXIT(&regcmp_lock,
-						arg_listp, compile_startp);
+						    arg_listp, compile_startp);
 					}
 					regexp++;
 				}
 			} else { /* invalid expression */
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			}
 
 			if ((min_count > MAX_SINGLE_BYTE_INT) ||
-				((max_count != UNLIMITED) &&
-				(min_count > max_count))) {
+			    ((max_count != UNLIMITED) &&
+			    (min_count > max_count))) {
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			} else {
 				*compilep = (unsigned char)min_count;
 				compilep++;
@@ -850,7 +850,7 @@
 			can_repeat = B_TRUE;
 			regex_typep = compilep;
 			expr_length = add_single_char_expr(compilep,
-					current_char);
+			    current_char);
 			compilep += expr_length;
 
 		} /* end switch (current_char) */
@@ -868,7 +868,7 @@
 			char_size = get_wchar(&current_char, regexp);
 			if (char_size <= 0) {
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			} else {
 				regexp += char_size;
 			}
@@ -876,7 +876,7 @@
 			if (pop_compilep() != (char *)0) {
 				/* unmatched parentheses */
 				ERROR_EXIT(&regcmp_lock, arg_listp,
-					compile_startp);
+				    compile_startp);
 			}
 			*compilep = (unsigned char)END_REGEX;
 			compilep++;