Fix crash calling RINDEX with a zero needle length
authorJohn Darrington <john@darrington.wattle.id.au>
Mon, 14 Dec 2015 10:42:26 +0000 (11:42 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 15 Dec 2015 18:32:32 +0000 (19:32 +0100)
Also add a test and correct the documentation.

doc/expressions.texi
src/language/expressions/operations.def
tests/language/expressions/evaluate.at

index 7180bf8793ea24a3c0907c13d70eb3ce1e7ef87e..7226116bd13e8897c29c2dbf120acf913ca17857 100644 (file)
@@ -621,7 +621,7 @@ format for @var{format}, system-missing is returned.
 @end deftypefn
 
 @cindex strings, searching backwards
-@deftypefn {Function} {} RINDEX (@var{string}, @var{format})
+@deftypefn {Function} {} RINDEX (@var{haystack}, @var{needle})
 Returns a positive integer indicating the position of the last
 occurrence of @var{needle} in @var{haystack}.  Returns 0 if
 @var{haystack} does not contain @var{needle}.  Returns system-missing if
@@ -634,7 +634,7 @@ Searches @var{haystack} for the last occurrence of each part, and
 returns the largest value.  Returns 0 if @var{haystack} does not contain
 any part in @var{needle}.  It is an error if @var{needle_len} does not
 evenly divide the length of @var{needle}.  Returns system-missing
-if @var{needle} is an empty string.
+if @var{needle} is an empty string or if needle_len is less than 1.
 @end deftypefn
 
 @cindex padding strings
index 60ed2eac2022b863af4ef05dbf34f22452687aa5..471fa1d30d331c4cb6e924b6c6822dcbe4570bde 100644 (file)
@@ -417,7 +417,7 @@ function RINDEX (string haystack, string needle)
 
 function RINDEX (string haystack, string needles, needle_len_d)
 {
-  if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
+  if (needle_len_d <= 0 || needle_len_d >= INT_MAX
       || (int) needle_len_d != needle_len_d
       || needles.length == 0)
     return SYSMIS;
index 846eee482b858a3f359df1bfa9685fa7f2050080..43d6b0b877a642aea6b2731a128878ff49457c19 100644 (file)
@@ -770,6 +770,7 @@ CHECK_EXPR_EVAL([concat index rindex length lower],
   [[rindex('abcbcde', 'abc', 1)], [5.00]],
   [[rindex('abcbcde', 'bccb', 2)], [4.00]],
   [[rindex('abcbcde', 'bcbc', 2)], [4.00]],
+  [[rindex('abcbcde', 'bcbc', 0)], [sysmis]],
   [[rindex('abcbcde', 'bcbc', $sysmis)], [sysmis]],
   [[rindex('abcbcde', 'bcbcg', 2)], [sysmis]],
   [[rindex('abcbcde', 'bcbcg', $sysmis)], [sysmis]],