29e444
#
29e444
# Backported from upstream.
29e444
#
29e444
# commit a445af0bc722d620afed7683cd320c0e4c7c6059
29e444
# Author: Andreas Schwab <schwab@suse.de>
29e444
# Date:   Tue Jan 29 14:45:15 2013 +0100
29e444
#
29e444
#    Fix buffer overrun in regexp matcher
29e444
#
29e444
# ChangeLog/
29e444
# 2013-02-12  Andreas Schwab  <schwab@suse.de>
29e444
# 
29e444
#	[BZ #15078]
29e444
#	* posix/regexec.c (extend_buffers): Add parameter min_len.
29e444
#	(check_matching): Pass minimum needed length.
29e444
#	(clean_state_log_if_needed): Likewise.
29e444
#	(get_subexp): Likewise.
29e444
#	* posix/Makefile (tests): Add bug-regex34.
29e444
#	(bug-regex34-ENV): Define.
29e444
#	* posix/bug-regex34.c: New file.
29e444
#
29e444
--- glibc-2.17-c758a686/posix/Makefile	2012-12-24 22:02:13.000000000 -0500
29e444
+++ glibc-2.17-c758a686/posix/Makefile	2013-03-17 15:30:13.121068666 -0400
29e444
@@ -86,7 +86,7 @@
29e444
 		   tst-rfc3484-3 \
29e444
 		   tst-getaddrinfo3 tst-fnmatch2 tst-cpucount tst-cpuset \
29e444
 		   bug-getopt1 bug-getopt2 bug-getopt3 bug-getopt4 \
29e444
-		   bug-getopt5 tst-getopt_long1
29e444
+		   bug-getopt5 tst-getopt_long1 bug-regex34
29e444
 xtests		:= bug-ga2
29e444
 ifeq (yes,$(build-shared))
29e444
 test-srcs	:= globtest
29e444
@@ -195,6 +195,7 @@
29e444
 bug-regex30-ENV = LOCPATH=$(common-objpfx)localedata
29e444
 bug-regex32-ENV = LOCPATH=$(common-objpfx)localedata
29e444
 bug-regex33-ENV = LOCPATH=$(common-objpfx)localedata
29e444
+bug-regex34-ENV = LOCPATH=$(common-objpfx)localedata
29e444
 tst-rxspencer-ARGS = --utf8 rxspencer/tests
29e444
 tst-rxspencer-ENV = LOCPATH=$(common-objpfx)localedata
29e444
 tst-pcre-ARGS = PCRE.tests
12745e
diff --git glibc-2.17-c758a686/posix/bug-regex34.c glibc-2.17-c758a686/posix/bug-regex34.c
29e444
new file mode 100644
29e444
index 0000000..bb3b613
29e444
--- /dev/null
12745e
+++ glibc-2.17-c758a686/posix/bug-regex34.c
29e444
@@ -0,0 +1,46 @@
29e444
+/* Test re_search with multi-byte characters in UTF-8.
29e444
+   Copyright (C) 2013 Free Software Foundation, Inc.
29e444
+   This file is part of the GNU C Library.
29e444
+
29e444
+   The GNU C Library is free software; you can redistribute it and/or
29e444
+   modify it under the terms of the GNU Lesser General Public
29e444
+   License as published by the Free Software Foundation; either
29e444
+   version 2.1 of the License, or (at your option) any later version.
29e444
+
29e444
+   The GNU C Library is distributed in the hope that it will be useful,
29e444
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
29e444
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29e444
+   Lesser General Public License for more details.
29e444
+
29e444
+   You should have received a copy of the GNU Lesser General Public
29e444
+   License along with the GNU C Library; if not, see
29e444
+   <http://www.gnu.org/licenses/>.  */
29e444
+
29e444
+#define _GNU_SOURCE 1
29e444
+#include <stdio.h>
29e444
+#include <string.h>
29e444
+#include <locale.h>
29e444
+#include <regex.h>
29e444
+
29e444
+static int
29e444
+do_test (void)
29e444
+{
29e444
+  struct re_pattern_buffer r;
29e444
+  /* ကျွန်ုပ်x */
29e444
+  const char *s = "\xe1\x80\x80\xe1\x80\xbb\xe1\x80\xbd\xe1\x80\x94\xe1\x80\xba\xe1\x80\xaf\xe1\x80\x95\xe1\x80\xbax";
29e444
+
29e444
+  if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
29e444
+    {
29e444
+      puts ("setlocale failed");
29e444
+      return 1;
29e444
+    }
29e444
+  memset (&r, 0, sizeof (r));
29e444
+
29e444
+  re_compile_pattern ("[^x]x", 5, &r);
29e444
+  /* This was triggering a buffer overflow.  */
29e444
+  re_search (&r, s, strlen (s), 0, strlen (s), 0);
29e444
+  return 0;
29e444
+}
29e444
+
29e444
+#define TEST_FUNCTION do_test ()
29e444
+#include "../test-skeleton.c"
12745e
diff --git glibc-2.17-c758a686/posix/regexec.c glibc-2.17-c758a686/posix/regexec.c
29e444
index 7f2de85..5ca2bf6 100644
12745e
--- glibc-2.17-c758a686/posix/regexec.c
12745e
+++ glibc-2.17-c758a686/posix/regexec.c
29e444
@@ -197,7 +197,7 @@ static int group_nodes_into_DFAstates (const re_dfa_t *dfa,
29e444
 static int check_node_accept (const re_match_context_t *mctx,
29e444
 			      const re_token_t *node, int idx)
29e444
      internal_function;
29e444
-static reg_errcode_t extend_buffers (re_match_context_t *mctx)
29e444
+static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len)
29e444
      internal_function;
29e444
 
29e444
 /* Entry point for POSIX code.  */
29e444
@@ -1160,7 +1160,7 @@ check_matching (re_match_context_t *mctx, int fl_longest_match,
29e444
 	  || (BE (next_char_idx >= mctx->input.valid_len, 0)
29e444
 	      && mctx->input.valid_len < mctx->input.len))
29e444
 	{
29e444
-	  err = extend_buffers (mctx);
29e444
+	  err = extend_buffers (mctx, next_char_idx + 1);
29e444
 	  if (BE (err != REG_NOERROR, 0))
29e444
 	    {
29e444
 	      assert (err == REG_ESPACE);
29e444
@@ -1738,7 +1738,7 @@ clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx)
29e444
 	  && mctx->input.valid_len < mctx->input.len))
29e444
     {
29e444
       reg_errcode_t err;
29e444
-      err = extend_buffers (mctx);
29e444
+      err = extend_buffers (mctx, next_state_log_idx + 1);
29e444
       if (BE (err != REG_NOERROR, 0))
29e444
 	return err;
29e444
     }
29e444
@@ -2792,7 +2792,7 @@ get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
29e444
 		  if (bkref_str_off >= mctx->input.len)
29e444
 		    break;
29e444
 
29e444
-		  err = extend_buffers (mctx);
29e444
+		  err = extend_buffers (mctx, bkref_str_off + 1);
29e444
 		  if (BE (err != REG_NOERROR, 0))
29e444
 		    return err;
29e444
 
29e444
@@ -4102,7 +4102,7 @@ check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
29e444
 
29e444
 static reg_errcode_t
29e444
 internal_function __attribute_warn_unused_result__
29e444
-extend_buffers (re_match_context_t *mctx)
29e444
+extend_buffers (re_match_context_t *mctx, int min_len)
29e444
 {
29e444
   reg_errcode_t ret;
29e444
   re_string_t *pstr = &mctx->input;
29e444
@@ -4111,8 +4111,10 @@ extend_buffers (re_match_context_t *mctx)
29e444
   if (BE (INT_MAX / 2 / sizeof (re_dfastate_t *) <= pstr->bufs_len, 0))
29e444
     return REG_ESPACE;
29e444
 
29e444
-  /* Double the lengthes of the buffers.  */
29e444
-  ret = re_string_realloc_buffers (pstr, MIN (pstr->len, pstr->bufs_len * 2));
29e444
+  /* Double the lengthes of the buffers, but allocate at least MIN_LEN.  */
29e444
+  ret = re_string_realloc_buffers (pstr,
29e444
+				   MAX (min_len,
29e444
+					MIN (pstr->len, pstr->bufs_len * 2)));
29e444
   if (BE (ret != REG_NOERROR, 0))
29e444
     return ret;
29e444