Browse Source

bug: when `read' fails it must return nil (and not no value)

v5-2
Roberto Ierusalimschy 24 years ago
parent
commit
6af005ec20
  1. 5
      bugs
  2. 21
      liolib.c

5
bugs

@ -245,3 +245,8 @@ Wed Nov 29 09:51:44 EDT 2000
>> parser does not accept a `;' after a `return' >> parser does not accept a `;' after a `return'
(by lhf; since 4.0b) (by lhf; since 4.0b)
** liolib.c
Fri Dec 22 15:30:42 EDT 2000
>> when `read' fails it must return nil (and not no value)
(by cassino; since at least 3.1)

21
liolib.c

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.94 2000/12/18 13:42:19 roberto Exp roberto $ ** $Id: liolib.c,v 1.95 2000/12/22 16:57:13 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -318,6 +318,7 @@ static int io_read (lua_State *L) {
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
int lastarg = lua_gettop(L) - 1; int lastarg = lua_gettop(L) - 1;
int firstarg = 1; int firstarg = 1;
int success;
FILE *f = gethandle(L, ctrl, firstarg); FILE *f = gethandle(L, ctrl, firstarg);
int n; int n;
if (f) firstarg++; if (f) firstarg++;
@ -330,8 +331,8 @@ static int io_read (lua_State *L) {
} }
else /* ensure stack space for all results and for auxlib's buffer */ else /* ensure stack space for all results and for auxlib's buffer */
luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments"); luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments");
for (n = firstarg; n<=lastarg; n++) { success = 1;
int success; for (n = firstarg; n<=lastarg && success; n++) {
if (lua_isnumber(L, n)) if (lua_isnumber(L, n))
success = read_chars(L, f, (size_t)lua_tonumber(L, n)); success = read_chars(L, f, (size_t)lua_tonumber(L, n));
else { else {
@ -343,8 +344,8 @@ static int io_read (lua_State *L) {
else { else {
switch (p[1]) { switch (p[1]) {
case 'n': /* number */ case 'n': /* number */
if (!read_number(L, f)) goto endloop; /* read fails */ success = read_number(L, f);
continue; /* number is already pushed; avoid the "pushstring" */ break;
case 'l': /* line */ case 'l': /* line */
success = read_line(L, f); success = read_line(L, f);
break; break;
@ -361,11 +362,11 @@ static int io_read (lua_State *L) {
} }
} }
} }
if (!success) { }
lua_pop(L, 1); /* remove last result */ if (!success) {
break; /* read fails */ lua_pop(L, 1); /* remove last result */
} lua_pushnil(L); /* push nil instead */
} endloop: }
return n - firstarg; return n - firstarg;
} }

Loading…
Cancel
Save