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