C   120

bug c

Guest on 21st June 2022 05:52:03 PM

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct js_State js_State;
  6. typedef struct js_Ast js_Ast;
  7.  
  8. static void jsR_run(js_State *J);
  9. static void js_error(js_State *J);
  10.  
  11. struct js_State
  12. {
  13.         int top;
  14. };
  15.  
  16. enum js_AstType { AST_LIST, AST_IDENTIFIER };
  17.  
  18. struct js_Ast
  19. {
  20.         enum js_AstType type;
  21.         js_Ast *a, *b;
  22.         const char *string;
  23. };
  24.  
  25. static void js_throw(js_State *J)
  26. {
  27.         printf("SUCCESS: exit with exception as expected!\n");
  28.         exit(0);
  29. }
  30.  
  31. static void js_pop(js_State *J)
  32. {
  33.         // XXX BUG NEEDS THIS LINE
  34.         J->top = 0;
  35.         // XXX BUG NEEDS THIS LINE
  36.         // NOTE is infinite recursion messing things up?
  37.         js_error(J);
  38. }
  39.  
  40. static void js_error(js_State *J) {
  41.         // XXX BUG NEEDS THIS LINE
  42.         jsR_run(J);
  43.         // XXX BUG NEEDS THIS LINE
  44.         // NOTE is infinite recursion messing things up?
  45.         js_pop(J);
  46.         js_throw(J);
  47. }
  48.  
  49. // XXX MUST NOT BE STATIC
  50. void js_pushstring(js_State *J, const char *v)
  51. {
  52.         if (v[0] == 0)
  53.                 // XXX BUG NEEDS THIS LINE
  54.                 js_error(J);
  55. }
  56.  
  57. static void jsC_error(js_State *J, js_Ast *node, const char *fmt, ...)
  58. {
  59.         // XXX BUG NEEDS THIS LINE
  60.         js_pushstring(J, "hello");
  61.         js_throw(J);
  62. }
  63.  
  64. int this_is_a_stupid_global = 0;
  65.  
  66. static void checkfutureword(js_State *J, js_Ast *exp)
  67. {
  68.         // XXX ADDING THIS LINE HIDES THE BUG
  69.         //++this_is_a_stupid_global;
  70.         if (!strcmp(exp->string, "const")) {
  71.                 // NOTE cannot inline this call
  72.                 jsC_error(J, exp, "'%s' is a future reserved word", exp->string);
  73.         }
  74. }
  75.  
  76. // XXX MUST BE STATIC (if externally visible, no bug)
  77. static void cparams(js_State *J, js_Ast *list)
  78. {
  79.         printf("cparams %s\n", list->a->string);
  80.         checkfutureword(J, list->a);
  81. }
  82.  
  83. // XXX MUST BE STATIC (if externally visible, no bug)
  84. static void cfunbody(js_State *J, js_Ast *params, js_Ast *body)
  85. {
  86.         cparams(J, params);
  87.  
  88.         // XXX BUG NEEDS THIS LINE
  89.         // NOTE is null deref messing things up here?
  90.         checkfutureword(J, body->a);
  91. }
  92.  
  93. static void jsR_run(js_State *J)
  94. {
  95.         // XXX BUG NEEDS THIS LINE
  96.         cfunbody(J, NULL, NULL);
  97. }
  98.  
  99. static js_Ast *jsP_newnode(js_State *J, enum js_AstType type, js_Ast *a, js_Ast *b)
  100. {
  101.         js_Ast *node = malloc(sizeof *node);
  102.         node->type = type;
  103.         node->a = a;
  104.         node->b = b;
  105.         node->string = NULL;
  106.         return node;
  107. }
  108.  
  109. static js_Ast *jsP_newstrnode(js_State *J, enum js_AstType type, const char *s)
  110. {
  111.         js_Ast *node = jsP_newnode(J, type, 0, 0);
  112.         node->string = s;
  113.         return node;
  114. }
  115.  
  116. int main(int argc, char **argv)
  117. {
  118.         js_State J = {};
  119.         cparams(&J, jsP_newnode(&J, AST_LIST, jsP_newstrnode(&J, AST_IDENTIFIER, "const"), 0));
  120.         puts("FAIL: should NOT get here");
  121. }

Raw Paste


Login or Register to edit or fork this paste. It's free.