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