Guest User

bug.cpp - program for triggering bug in Lua 5.2.1

a guest
Oct 24th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4.  
  5. #include "lua.hpp"
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. lua_State *lstate1;
  14. lua_Integer size;
  15. int MANIFEST_stack_index;
  16.  
  17. lstate1 = luaL_newstate();
  18. luaL_openlibs(lstate1);
  19.  
  20. if (luaL_dofile(lstate1, "testmanifest.lua"))
  21. {
  22. cout<<"dofile() error"<<endl;
  23. return -1;
  24. }
  25.  
  26. cout<<"LUAI_MAXSTACK = "<<LUAI_MAXSTACK<<endl;
  27.  
  28. lua_getglobal(lstate1, "MANIFEST");
  29.  
  30. MANIFEST_stack_index = lua_absindex(lstate1, -1);
  31.  
  32. lua_len(lstate1, -1);
  33.  
  34. size = lua_tointeger(lstate1, -1);
  35.  
  36. lua_pop(lstate1, 1);
  37.  
  38. cout<<"MANIFEST size is "<<size<<endl;
  39.  
  40. for (int i = 0; i < size; i++)
  41. {
  42. const char *str_ptr;
  43.  
  44. cout<<"lua_gettop() = "<< lua_gettop(lstate1)<<endl;
  45.  
  46.  
  47. if (lua_istable(lstate1, MANIFEST_stack_index))
  48. {
  49. lua_pushinteger(lstate1, i + 1);
  50. lua_gettable(lstate1, MANIFEST_stack_index); //MANIFEST[i]
  51.  
  52. if (lua_istable(lstate1, -1))
  53. {
  54. lua_pushinteger(lstate1, 1);
  55. lua_gettable(lstate1, -2); //MANIFEST[i][1]
  56.  
  57. str_ptr = lua_tolstring(lstate1, -1, NULL);
  58.  
  59. if (!str_ptr)
  60. {
  61. cout<<"lua_tolstring() returned NULL!"<<endl;
  62.  
  63. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  64.  
  65. return -1;
  66. }
  67.  
  68. cout<<"MANIFEST["<<i<<"][1] = "<<str_ptr<<endl;
  69.  
  70. lua_pop(lstate1, 1);
  71.  
  72. /////////////////////////////////
  73.  
  74. lua_pushinteger(lstate1,2);
  75. lua_gettable(lstate1, -2); //MANIFEST[i][2]
  76.  
  77. str_ptr = lua_tolstring(lstate1, -1, NULL);
  78.  
  79. if (!str_ptr)
  80. {
  81. cout<<"lua_tolstring() returned NULL!"<<endl;
  82.  
  83. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  84.  
  85. return -1;
  86. }
  87.  
  88. cout<<"MANIFEST["<<i<<"][2] = "<<str_ptr<<endl;
  89.  
  90. lua_pop(lstate1, 1);
  91.  
  92. /////////////////////////////////
  93.  
  94. lua_pushinteger(lstate1,3);
  95. lua_gettable(lstate1, -2); //MANIFEST[i][3]
  96.  
  97. str_ptr = lua_tolstring(lstate1, -1, NULL);
  98.  
  99. if (!str_ptr)
  100. {
  101. cout<<"lua_tolstring() returned NULL!"<<endl;
  102.  
  103. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  104.  
  105. return -1;
  106. }
  107.  
  108. cout<<"MANIFEST["<<i<<"][3] = "<<str_ptr<<endl;
  109.  
  110. lua_pop(lstate1, 1);
  111.  
  112. }
  113. else
  114. {
  115. cout<<"MANIFEST["<<i<<"] value is not a table!"<<endl;
  116. return -1;
  117. }
  118.  
  119. lua_pop(lstate1, 1);
  120. }
  121. else
  122. {
  123. cout<<"error, not a table type"<<endl;
  124. return -1;
  125. }
  126. }
  127.  
  128. lua_close(lstate1);
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment