From e182e7422cc5670dc9217ac12bba7ba12542678d Mon Sep 17 00:00:00 2001 From: jacksonmj Date: Sun, 5 Jun 2011 17:06:06 +0100 Subject: Loads of error messages for lua, and hopefully fix lua stack overflow Also fix game exiting when an error occurs in a step function. Returning -1 seems to prevent lua from restoring the stack to the state it was in before the C function call - see bottom of luaD_precall() in lua source (ldo.c) diff --git a/src/interface.c b/src/interface.c index bff2182..b6d854e 100644 --- a/src/interface.c +++ b/src/interface.c @@ -2344,7 +2344,7 @@ corrupt: int search_ui(pixel *vid_buf) { - int nmp,uih=0,nyu,nyd,b=1,bq,mx=0,my=0,mxq=0,myq=0,mmt=0,gi,gj,gx,gy,pos,i,mp,dp,dap,own,last_own=search_own,last_fav=search_fav,page_count=0,last_page=0,last_date=0,j,w,h,st=0,lv; + int nmp=-1,uih=0,nyu,nyd,b=1,bq,mx=0,my=0,mxq=0,myq=0,mmt=0,gi,gj,gx,gy,pos,i,mp,dp,dap,own,last_own=search_own,last_fav=search_fav,page_count=0,last_page=0,last_date=0,j,w,h,st=0,lv; int is_p1=0, exp_res=GRID_X*GRID_Y, tp, view_own=0; int thumb_drawn[GRID_X*GRID_Y]; pixel *v_buf = (pixel *)malloc(((YRES+MENUSIZE)*(XRES+BARSIZE))*PIXELSIZE); diff --git a/src/luaconsole.c b/src/luaconsole.c index 79227ad..8fd137b 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -38,7 +38,7 @@ void luacon_open(){ luaL_openlib(l, "tpt", tptluaapi, 0); } int luacon_step(int mx, int my, int mb, int mbq, char key){ - int tempret = 0, tempb, i; + int tempret = 0, tempb, i, callret; if(step_functions[0]){ //Set mouse globals lua_pushinteger(l, mbq); @@ -52,16 +52,20 @@ int luacon_step(int mx, int my, int mb, int mbq, char key){ for(i = 0; i<6; i++){ if(step_functions[i]){ lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); - lua_call(l, 0, 1); + callret = lua_pcall(l, 0, 1, 0); + if (!callret) + { + // failed, TODO: error reporting + } if(lua_isboolean(l, -1)){ tempb = lua_toboolean(l, -1); - lua_pop(l, 1); if(tempb){ //Mouse click has been handled, set the global for future calls lua_pushinteger(l, mb); lua_setfield(l, LUA_GLOBALSINDEX, "mouseb"); } tempret |= tempb; } + lua_pop(l, 1); } } return tempret; @@ -132,36 +136,47 @@ int luatpt_drawtext(lua_State* l) textgreen = luaL_optint(l, 5, 255); textblue = luaL_optint(l, 6, 255); textalpha = luaL_optint(l, 7, 255); + if (textx<0 || texty<0 || textx>=XRES+BARSIZE || texty>=YRES+MENUSIZE) + return luaL_error(l, "Screen coordinates out of range (%d,%d)", textx, texty); + if (textred<0) textred = 0; + if (textred>255) textred = 255; + if (textgreen<0) textgreen = 0; + if (textgreen>255) textgreen = 255; + if (textblue<0) textblue = 0; + if (textblue>255) textblue = 255; + if (textalpha<0) textalpha = 0; + if (textalpha>255) textalpha = 255; if(vid_buf!=NULL){ drawtext(vid_buf, textx, texty, string, textred, textgreen, textblue, textalpha); return 0; } - return -1; + return luaL_error(l, "Screen buffer does not exist"); } int luatpt_create(lua_State* l) { - int x, y, retid, t = 0; + int x, y, retid, t = -1; char * name; x = abs(luaL_optint(l, 1, 0)); y = abs(luaL_optint(l, 2, 0)); if(x < XRES && y < YRES){ if(lua_isnumber(l, 3)){ t = luaL_optint(l, 3, 0); - if(t >= PT_NUM) - return -1; + if (t==OLD_PT_WIND) + return 0; + if (t<0 || t >= PT_NUM) + return luaL_error(l, "Unrecognised element number '%d'", t); } else { name = luaL_optstring(l, 3, "dust"); - if (name[0]!=0) - console_parse_type(name, &t, console_error); + if (!console_parse_type(name, &t, console_error)) + return luaL_error(l,"Unrecognised element '%s'", name); } retid = create_part(-1, x, y, t); - if(retid==-1) - return -1; + // failing to create a particle often happens (e.g. if space is already occupied) and isn't usually important, so don't raise an error lua_pushinteger(l, retid); return 1; } - return -1; + return luaL_error(l, "Coordinates out of range (%d,%d)", x, y); } int luatpt_setpause(lua_State* l) @@ -329,19 +344,19 @@ int luatpt_set_property(lua_State* l) acount = lua_gettop(l); prop = luaL_optstring(l, 1, ""); if(lua_isnumber(l, 3)) - i = luaL_optint(l, 3, -1); + i = abs(luaL_optint(l, 3, -1)); else i = -1; if(lua_isnumber(l, 4)) - y = luaL_optint(l, 4, -1); + y = abs(luaL_optint(l, 4, -1)); else y = -1; if(lua_isnumber(l, 5)) - w = luaL_optint(l, 5, -1); + w = abs(luaL_optint(l, 5, -1)); else w = -1; if(lua_isnumber(l, 6)) - h = luaL_optint(l, 6, -1); + h = abs(luaL_optint(l, 6, -1)); else h = -1; if (strcmp(prop,"type")==0){ @@ -372,14 +387,13 @@ int luatpt_set_property(lua_State* l) offset = offsetof(particle, y); format = 2; } else { - lua_pushstring(l, "invalid property"); - lua_error(l); + return luaL_error(l, "Invalid property '%s'", prop); } if(acount>2){ if(!lua_isnumber(l, acount) && lua_isstring(l, acount)){ name = luaL_optstring(l, acount, "none"); - if (name[0]!=0) - console_parse_type(name, &partsel, console_error); + if (!console_parse_type(name, &partsel, console_error)) + return luaL_error(l, "Unrecognised element '%s'", name); } } if(lua_isnumber(l, 2)){ @@ -388,12 +402,14 @@ int luatpt_set_property(lua_State* l) } else { t = luaL_optint(l, 2, 0); } - if(t >= PT_NUM && format == 3) - return -1; + if (format == 3 && t==OLD_PT_WIND) + return 0; + if (format == 3 && (t<0 || t>=PT_NUM)) + return luaL_error(l, "Unrecognised element number '%d'", t); } else { name = luaL_optstring(l, 2, "dust"); - if (name[0]!=0) - console_parse_type(name, &t, console_error); + if (!console_parse_type(name, &t, console_error)) + return luaL_error(l, "Unrecognised element '%s'", name); } if(i == -1 || (w != -1 && h != -1)){ // Got a region @@ -403,7 +419,13 @@ int luatpt_set_property(lua_State* l) w = XRES; h = YRES; } + if (i>=XRES || y>=YRES) + return luaL_error(l, "Coordinates out of range (%d,%d)", i, y); x = i; + if(x+w > XRES) + w = XRES-x; + if(y+h > YRES) + h = YRES-y; for (nx = x; nx>8; @@ -418,17 +440,23 @@ int luatpt_set_property(lua_State* l) } else { // Got coords or particle index if(i != -1 && y != -1){ + if (i>=XRES || y>=YRES) + return luaL_error(l, "Coordinates out of range (%d,%d)", i, y); i = pmap[y][i]>>8; + if (i >= NPART) + return 0; } - if (i < 0 || i >= NPART || (partsel && partsel != parts[i].type)) - return -1; + if (i < 0 || i >= NPART) + return luaL_error(l, "Invalid particle ID '%d'", i); + if (partsel && partsel != parts[i].type) + return 0; if(format==2){ *((float*)(((void*)&parts[i])+offset)) = f; } else { *((int*)(((void*)&parts[i])+offset)) = t; } } - return -1; + return 0; } int luatpt_get_property(lua_State* l) @@ -436,13 +464,17 @@ int luatpt_get_property(lua_State* l) int i, y; char *prop; prop = luaL_optstring(l, 1, ""); - i = luaL_optint(l, 2, 0); - y = luaL_optint(l, 3, -1); - if(y!=-1 && y < YRES && y > 0 && i < XRES && i > 0){ + i = abs(luaL_optint(l, 2, 0)); + y = abs(luaL_optint(l, 3, -1)); + if(y!=-1 && y < YRES && y >= 0 && i < XRES && i >= 0){ i = pmap[y][i]>>8; + if (i >= NPART) + return 0; } + else if (y!=-1) + return luaL_error(l, "Coordinates out of range (%d,%d)", i, y); if (i < 0 || i >= NPART) - return -1; + return luaL_error(l, "Invalid particle ID '%d'", i); if (parts[i].type) { if (strcmp(prop,"type")==0){ @@ -482,8 +514,7 @@ int luatpt_get_property(lua_State* l) return 1; } } - - return -1; + return luaL_error(l, "Particle does not exist", i); } int luatpt_drawpixel(lua_State* l) @@ -496,15 +527,22 @@ int luatpt_drawpixel(lua_State* l) b = luaL_optint(l, 5, 255); a = luaL_optint(l, 6, 255); - if(x > XRES || y > YRES || x < 0 || y < 0) - return -1; + if (x<0 || y<0 || x>=XRES+BARSIZE || y>=YRES+MENUSIZE) + return luaL_error(l, "Screen coordinates out of range (%d,%d)", x, y); + if (r<0) r = 0; + if (r>255) r = 255; + if (g<0) g = 0; + if (g>255) g = 255; + if (b<0) b = 0; + if (b>255) b = 255; + if (a<0) a = 0; + if (a>255) a = 255; if (vid_buf!=NULL) { drawpixel(vid_buf, x, y, r, g, b, a); return 0; } - return -1; - + return luaL_error(l, "Screen buffer does not exist"); } int luatpt_drawrect(lua_State* l) @@ -519,14 +557,26 @@ int luatpt_drawrect(lua_State* l) b = luaL_optint(l, 7, 255); a = luaL_optint(l, 8, 255); - if(x > XRES || y > YRES || x < 0 || y < 0) - return -1; + if (x<0 || y<0 || x>=XRES+BARSIZE || y>=YRES+MENUSIZE) + return luaL_error(l, "Screen coordinates out of range (%d,%d)", x, y); + if(x+w > XRES) + w = XRES-x; + if(y+h > YRES) + h = YRES-y; + if (r<0) r = 0; + if (r>255) r = 255; + if (g<0) g = 0; + if (g>255) g = 255; + if (b<0) b = 0; + if (b>255) b = 255; + if (a<0) a = 0; + if (a>255) a = 255; if (vid_buf!=NULL) { drawrect(vid_buf, x, y, w, h, r, g, b, a); return 0; } - return -1; + return luaL_error(l, "Screen buffer does not exist"); } int luatpt_fillrect(lua_State* l) @@ -541,14 +591,26 @@ int luatpt_fillrect(lua_State* l) b = luaL_optint(l, 7, 255); a = luaL_optint(l, 8, 255); - if(x > XRES || y > YRES || x < 0 || y < 0) - return -1; + if (x<0 || y<0 || x>=XRES+BARSIZE || y>=YRES+MENUSIZE) + return luaL_error(l, "Screen coordinates out of range (%d,%d)", x, y); + if(x+w > XRES) + w = XRES-x; + if(y+h > YRES) + h = YRES-y; + if (r<0) r = 0; + if (r>255) r = 255; + if (g<0) g = 0; + if (g>255) g = 255; + if (b<0) b = 0; + if (b>255) b = 255; + if (a<0) a = 0; + if (a>255) a = 255; if (vid_buf!=NULL) { fillrect(vid_buf, x, y, w, h, r, g, b, a); return 0; } - return -1; + return luaL_error(l, "Screen buffer does not exist"); } int luatpt_textwidth(lua_State* l) @@ -567,7 +629,7 @@ int luatpt_get_name(lua_State* l) lua_pushstring(l, ""); return 1; } - lua_pushstring(l, ""); + lua_pushstring(l, svf_user); return 1; } @@ -593,7 +655,7 @@ int luatpt_delete(lua_State* l) delete_part(arg1, arg2); return 0; } - return -1; + return luaL_error(l,"Invalid coordinates or particle ID"); } int luatpt_register_step(lua_State* l) @@ -608,9 +670,7 @@ int luatpt_register_step(lua_State* l) } else { //Supposed to prevent the registration of 2 functions, but this isn't working TODO: FIX! lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); if(lua_equal(l, 1, lua_gettop(l))){ - lua_pushstring(l, "function already registered"); - lua_error(l); - return -1; + return luaL_error(l, "Function already registered"); } } } diff --git a/src/main.c b/src/main.c index e2d681c..08f0f5c 100644 --- a/src/main.c +++ b/src/main.c @@ -1529,7 +1529,7 @@ int main(int argc, char *argv[]) void *http_ver_check, *http_session_check = NULL; char *ver_data=NULL, *check_data=NULL, *tmp; //char console_error[255] = ""; - int result, i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len, new_message_len; + int result, i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len, new_message_len=0; #ifdef INTERNAL int vs = 0; #endif -- cgit v0.9.2-21-gd62e