Explorar o código

better way to test overflows with string indices

Roberto Ierusalimschy %!s(int64=14) %!d(string=hai) anos
pai
achega
c9d1d9f9c6
Modificáronse 1 ficheiros con 2 adicións e 2 borrados
  1. 2 2
      lstrlib.c

+ 2 - 2
lstrlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstrlib.c,v 1.166 2011/04/20 16:36:28 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.167 2011/05/03 16:01:57 roberto Exp roberto $
 ** Standard library for string operations and pattern-matching
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -45,7 +45,7 @@ static int str_len (lua_State *L) {
 /* translate a relative string position: negative means back from end */
 /* translate a relative string position: negative means back from end */
 static size_t posrelat (ptrdiff_t pos, size_t len) {
 static size_t posrelat (ptrdiff_t pos, size_t len) {
   if (pos >= 0) return (size_t)pos;
   if (pos >= 0) return (size_t)pos;
-  else if (pos == -pos || (size_t)-pos > len) return 0;
+  else if (-(size_t)pos > len) return 0;
   else return len - ((size_t)-pos) + 1;
   else return len - ((size_t)-pos) + 1;
 }
 }