Browse Source

'string.find' cannot find things after subject's end

Roberto Ierusalimschy 17 years ago
parent
commit
24359ae294
1 changed files with 5 additions and 2 deletions
  1. 5 2
      lstrlib.c

+ 5 - 2
lstrlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstrlib.c,v 1.138 2007/10/29 15:51:10 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.139 2007/11/12 14:10:09 roberto Exp roberto $
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 */
@@ -499,7 +499,10 @@ static int str_find_aux (lua_State *L, int find) {
   const char *p = luaL_checklstring(L, 2, &l2);
   size_t init = posrelat(luaL_optinteger(L, 3, 1), l1);
   if (init < 1) init = 1;
-  else if (init > l1) init = l1 + 1;
+  else if (init > l1 + 1) {  /* start after string's end? */
+    lua_pushnil(L);  /* cannot find anything */
+    return 1;
+  }
   if (find && (lua_toboolean(L, 4) ||  /* explicit request? */
       strpbrk(p, SPECIALS) == NULL)) {  /* or no special characters? */
     /* do a plain search */