aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-12-20 15:25:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-12-20 15:25:36 -0200
commit8980c630bf40e05dad71ded377e3d0f0a17b076c (patch)
tree15727bc9e64747ae859d1a8fa09b0eb1fb77db21
parent8d579c5bc567ad92aadd59dbb8371bd961f42aa4 (diff)
downloadlua-8980c630bf40e05dad71ded377e3d0f0a17b076c.tar.gz
lua-8980c630bf40e05dad71ded377e3d0f0a17b076c.tar.bz2
lua-8980c630bf40e05dad71ded377e3d0f0a17b076c.zip
error when indexing strings with invalid keys
-rw-r--r--lstrlib.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 3acaf875..379fd20f 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.159 2010/11/19 16:25:51 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.160 2010/12/10 19:03:46 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -903,6 +903,15 @@ static int str_format (lua_State *L) {
903/* }====================================================== */ 903/* }====================================================== */
904 904
905 905
906static int noindex (lua_State *L) {
907 const char *f = lua_tostring(L, 2);
908 if (f)
909 return luaL_error(L, "no field '%s' in strings", f);
910 else
911 return luaL_error(L, "no such field in strings");
912}
913
914
906static const luaL_Reg strlib[] = { 915static const luaL_Reg strlib[] = {
907 {"byte", str_byte}, 916 {"byte", str_byte},
908 {"char", str_char}, 917 {"char", str_char},
@@ -918,19 +927,22 @@ static const luaL_Reg strlib[] = {
918 {"reverse", str_reverse}, 927 {"reverse", str_reverse},
919 {"sub", str_sub}, 928 {"sub", str_sub},
920 {"upper", str_upper}, 929 {"upper", str_upper},
930 {"__index", noindex},
921 {NULL, NULL} 931 {NULL, NULL}
922}; 932};
923 933
924 934
925static void createmetatable (lua_State *L) { 935static void createmetatable (lua_State *L) {
926 lua_createtable(L, 0, 1); /* create metatable for strings */ 936 /* setmetatable("", {__index = string}) */
927 lua_pushliteral(L, ""); /* dummy string */ 937 lua_pushliteral(L, ""); /* dummy string */
928 lua_pushvalue(L, -2); 938 lua_createtable(L, 0, 1); /* create metatable for strings */
929 lua_setmetatable(L, -2); /* set string metatable */ 939 lua_pushvalue(L, -3); /* set the string library... */
940 lua_setfield(L, -2, "__index"); /* ...as the __index metamethod */
941 lua_setmetatable(L, -2); /* set metatable for strings */
930 lua_pop(L, 1); /* pop dummy string */ 942 lua_pop(L, 1); /* pop dummy string */
931 lua_pushvalue(L, -2); /* string library... */ 943 /* setmetatable(string, string) */
932 lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ 944 lua_pushvalue(L, -1); /* push string library */
933 lua_pop(L, 1); /* pop metatable */ 945 lua_setmetatable(L, -2); /* set it as its own metatable */
934} 946}
935 947
936 948