From 70da5ac6bd376b570e2ef7ca1e4ed5f55dce3339 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Wed, 27 Jun 2001 19:33:42 +0200
Subject: [PATCH] Fixed bug in replace_many().

Rev: src/builtin_functions.c:1.383
---
 src/builtin_functions.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index 3416018e78..7b9479139c 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -5,7 +5,7 @@
 \*/
 /**/
 #include "global.h"
-RCSID("$Id: builtin_functions.c,v 1.382 2001/06/25 20:03:39 grubba Exp $");
+RCSID("$Id: builtin_functions.c,v 1.383 2001/06/27 17:33:42 grubba Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "pike_macros.h"
@@ -2794,11 +2794,6 @@ struct tupel
   struct pike_string *val;
 };
 
-static int replace_sortfun(struct tupel *a,struct tupel *b)
-{
-  return DO_NOT_WARN((int)my_quick_strcmp(a->ind, b->ind));
-}
-
 /* Magic, magic and more magic */
 static int find_longest_prefix(char *str,
 			       ptrdiff_t len,
@@ -2867,7 +2862,12 @@ static int find_longest_prefix(char *str,
 }
 			       
 
-static struct pike_string * replace_many(struct pike_string *str,
+static int replace_sortfun(struct tupel *a,struct tupel *b)
+{
+  return DO_NOT_WARN((int)my_quick_strcmp(a->ind, b->ind));
+}
+
+static struct pike_string *replace_many(struct pike_string *str,
 					struct array *from,
 					struct array *to)
 {
@@ -2923,9 +2923,11 @@ static struct pike_string * replace_many(struct pike_string *str,
   {
     INT32 x;
     x=index_shared_string(v[num-1-e].ind,0);
-    if(x<(INT32)NELEM(set_start)) set_start[x]=num-e-1;
+    if((x >= 0) && (x<(INT32)NELEM(set_start)))
+      set_start[x]=num-e-1;
     x=index_shared_string(v[e].ind,0);
-    if(x<(INT32)NELEM(set_end)) set_end[x]=e+1;
+    if((x >= 0) && (x<(INT32)NELEM(set_end)))
+      set_end[x]=e+1;
   }
 
   init_string_builder(&ret,str->size_shift);
@@ -2938,11 +2940,17 @@ static struct pike_string * replace_many(struct pike_string *str,
     ptrdiff_t ch;
 
     ch=index_shared_string(str,s);
-    if(ch<(ptrdiff_t)NELEM(set_end)) b=set_end[ch]; else b=num;
+    if((ch >= 0) && (ch<(ptrdiff_t)NELEM(set_end)))
+      b=set_end[ch];
+    else
+      b=num;
 
     if(b)
     {
-      if(ch<(ptrdiff_t)NELEM(set_start)) a=set_start[ch]; else a=0;
+      if((ch >= 0) && (ch<(ptrdiff_t)NELEM(set_start)))
+	a=set_start[ch];
+      else
+	a=0;
 
       a=find_longest_prefix(str->str+(s << str->size_shift),
 			    length,
-- 
GitLab