lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Aug 12, 2008 at 01:07:02PM -0300, Luiz Henrique de Figueiredo wrote:
> Lua 5.1.4-rc1 is now available at
[...]
> This is the first release candidate for Lua 5.1.4, which fixes all known
> bugs in Lua 5.1.3. Like all minor releases, this is strictly a bug-fix
> release; no new features or improvements have been added, except for
> an updated reference manual.

I'd like to resubmit a fix that seems to not have made it into
5.1.4. I'm quite sure it does the right thing. Some code in
auxsort clearly doesn't do what it's intended to do because of
the way the loops are written with preincrementation. Instead of
detecting an invalid order function the order function is passed
a (likely null) value from an out-of-bound index, which is of
course not a bug, but it makes the detection code a bit silly.

To clarify the fix: It might actually be more clear to write the
tests as (i==u) and (j==l), because a[i] and a[j] are the values
that have just been "successfully" compared to the pivot, and if
a[u] < P or P < a[l] then not only is the order function
invalid, but the next index up for comparison will also be out
of bound.


diff -rN -u old-lua-5.1.3/src/ltablib.c new-lua-5.1.3/src/ltablib.c
--- old-lua-5.1.3/src/ltablib.c 2008-04-04 00:51:38.375673575 +0200
+++ new-lua-5.1.3/src/ltablib.c 2008-04-04 00:51:38.407673452 +0200
@@ -216,12 +216,12 @@
     for (;;) {  /* invariant: a[l..i] <= P <= a[j..u] */
       /* repeat ++i until a[i] >= P */                  
       while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
-        if (i>u) luaL_error(L, "invalid order function for sorting");
+        if (i>=u) luaL_error(L, "invalid order function for sorting");
         lua_pop(L, 1);  /* remove a[i] */
       }                                  
       /* repeat --j until a[j] <= P */
       while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
-        if (j<l) luaL_error(L, "invalid order function for sorting");
+        if (j<=l) luaL_error(L, "invalid order function for sorting");
         lua_pop(L, 1);  /* remove a[j] */
       }                                  
       if (j<i) {