From 298c4065cae4ab2fa1cf1c5a20eb5ee52530bc5a Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 27 Feb 2010 21:25:05 +0000 Subject: Only look for the needle when it fits in the haystack. [Bug 2960021] --- ChangeLog | 10 ++++++++-- generic/tclCmdMZ.c | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7a9fbae..73c15ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-02-27 Donal K. Fellows + + * generic/tclCmdMZ.c (StringFirstCmd, StringLastCmd): [Bug 2960021]: + Only search for the needle in the haystack when the needle isn't + larger than the haystack. Prevents an odd crash from sometimes + happening when things get mixed up (a common programming error). + 2010-02-21 Jan Nijtmans * generic/tclBasic.c: Fix [Bug 2954959] expr abs(0.0) is -0.0 @@ -5,8 +12,7 @@ 2010-02-19 Stuart Cassoff - * tcl.m4: Correct compiler/linker flags - for threaded builds on OpenBSD. + * tcl.m4: Correct compiler/linker flags for threaded builds on OpenBSD * configure: (regenerated). 2010-02-19 Donal K. Fellows diff --git a/generic/tclCmdMZ.c b/generic/tclCmdMZ.c index 1ff2138..70002b5 100644 --- a/generic/tclCmdMZ.c +++ b/generic/tclCmdMZ.c @@ -15,7 +15,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclCmdMZ.c,v 1.163.2.5 2009/08/25 21:01:05 andreas_kupries Exp $ + * RCS: @(#) $Id: tclCmdMZ.c,v 1.163.2.6 2010/02/27 21:25:05 dkf Exp $ */ #include "tclInt.h" @@ -1166,7 +1166,12 @@ StringFirstCmd( } } - if (length1 > 0) { + /* + * If the length of the needle is more than the length of the haystack, it + * cannot be contained in there so we can avoid searching. [Bug 2960021] + */ + + if (length1 > 0 && length1 <= length2) { register Tcl_UniChar *p, *end; end = ustring2 + length2 - length1 + 1; @@ -1269,7 +1274,12 @@ StringLastCmd( p = ustring2 + length2 - length1; } - if (length1 > 0) { + /* + * If the length of the needle is more than the length of the haystack, it + * cannot be contained in there so we can avoid searching. [Bug 2960021] + */ + + if (length1 > 0 && length1 <= length2) { for (; p >= ustring2; p--) { /* * Scan backwards to find the first character. -- cgit v0.12