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