diff options
| author | Peter Melnichenko <mpeterval@gmail.com> | 2016-08-25 18:24:57 +0300 |
|---|---|---|
| committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-08-25 18:31:09 +0300 |
| commit | 50919ed69ff64df51d8d586d00834fde3e901785 (patch) | |
| tree | 165f2d1bb241fd0448992d9f1b1e9e5630db64df | |
| parent | 4cf702d85069db13f04b06dbe1ce4b1a0314dafc (diff) | |
| download | luafilesystem-50919ed69ff64df51d8d586d00834fde3e901785.tar.gz luafilesystem-50919ed69ff64df51d8d586d00834fde3e901785.tar.bz2 luafilesystem-50919ed69ff64df51d8d586d00834fde3e901785.zip | |
Fix lfs.attributes and lfs.symlinkattributes extra argument handling
When the second argument is not a string, _file_info() wants to
ensure that there is a table on top of the stack: the second argument
or a new table. If a new table is pushed it's created on top immediately,
but if a table is passed as the second argument it can be followed
by extra arguments, with the last one ending up being used as a table,
causing a crash. The fix is to remove any potential extra arguments
using `lua_settop(L, 2)`.
Also added a few tests for this case. Ref #80.
| -rw-r--r-- | src/lfs.c | 3 | ||||
| -rw-r--r-- | tests/test.lua | 11 |
2 files changed, 13 insertions, 1 deletions
| @@ -827,7 +827,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { | |||
| 827 | /* member not found */ | 827 | /* member not found */ |
| 828 | return luaL_error(L, "invalid attribute name '%s'", member); | 828 | return luaL_error(L, "invalid attribute name '%s'", member); |
| 829 | } | 829 | } |
| 830 | /* creates a table if none is given */ | 830 | /* creates a table if none is given, removes extra arguments */ |
| 831 | lua_settop(L, 2); | ||
| 831 | if (!lua_istable (L, 2)) { | 832 | if (!lua_istable (L, 2)) { |
| 832 | lua_newtable (L); | 833 | lua_newtable (L); |
| 833 | } | 834 | } |
diff --git a/tests/test.lua b/tests/test.lua index 2331eec..10810fe 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -132,6 +132,17 @@ for key, value in pairs(attr) do | |||
| 132 | "lfs.attributes values not consistent") | 132 | "lfs.attributes values not consistent") |
| 133 | end | 133 | end |
| 134 | 134 | ||
| 135 | -- Check that lfs.attributes accepts a table as second argument | ||
| 136 | local attr2 = {} | ||
| 137 | lfs.attributes(tmpfile, attr2) | ||
| 138 | for key, value in pairs(attr2) do | ||
| 139 | assert (value == lfs.attributes (tmpfile, key), | ||
| 140 | "lfs.attributes values with table argument not consistent") | ||
| 141 | end | ||
| 142 | |||
| 143 | -- Check that extra arguments are ignored | ||
| 144 | lfs.attributes(tmpfile, attr2, nil) | ||
| 145 | |||
| 135 | -- Remove new file and directory | 146 | -- Remove new file and directory |
| 136 | assert (os.remove (tmpfile), "could not remove new file") | 147 | assert (os.remove (tmpfile), "could not remove new file") |
| 137 | assert (lfs.rmdir (tmpdir), "could not remove new directory") | 148 | assert (lfs.rmdir (tmpdir), "could not remove new directory") |
