jscript: Added a few missing allocation checks (Coverity).
[wine] / dlls / jscript / lex.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <math.h>
20
21 #include "jscript.h"
22 #include "activscp.h"
23 #include "objsafe.h"
24 #include "engine.h"
25
26 #include "parser.tab.h"
27
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32
33 static const WCHAR breakW[] = {'b','r','e','a','k',0};
34 static const WCHAR caseW[] = {'c','a','s','e',0};
35 static const WCHAR catchW[] = {'c','a','t','c','h',0};
36 static const WCHAR continueW[] = {'c','o','n','t','i','n','u','e',0};
37 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
38 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
39 static const WCHAR doW[] = {'d','o',0};
40 static const WCHAR elseW[] = {'e','l','s','e',0};
41 static const WCHAR falseW[] = {'f','a','l','s','e',0};
42 static const WCHAR finallyW[] = {'f','i','n','a','l','l','y',0};
43 static const WCHAR forW[] = {'f','o','r',0};
44 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
45 static const WCHAR ifW[] = {'i','f',0};
46 static const WCHAR inW[] = {'i','n',0};
47 static const WCHAR instanceofW[] = {'i','n','s','t','a','n','c','e','o','f',0};
48 static const WCHAR newW[] = {'n','e','w',0};
49 static const WCHAR nullW[] = {'n','u','l','l',0};
50 static const WCHAR returnW[] = {'r','e','t','u','r','n',0};
51 static const WCHAR switchW[] = {'s','w','i','t','c','h',0};
52 static const WCHAR thisW[] = {'t','h','i','s',0};
53 static const WCHAR throwW[] = {'t','h','r','o','w',0};
54 static const WCHAR trueW[] = {'t','r','u','e',0};
55 static const WCHAR tryW[] = {'t','r','y',0};
56 static const WCHAR typeofW[] = {'t','y','p','e','o','f',0};
57 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
58 static const WCHAR varW[] = {'v','a','r',0};
59 static const WCHAR voidW[] = {'v','o','i','d',0};
60 static const WCHAR whileW[] = {'w','h','i','l','e',0};
61 static const WCHAR withW[] = {'w','i','t','h',0};
62
63 static const struct {
64     const WCHAR *word;
65     int token;
66 } keywords[] = {
67     {breakW,       kBREAK},
68     {caseW,        kCASE},
69     {catchW,       kCATCH},
70     {continueW,    kCONTINUE},
71     {defaultW,     kDEFAULT},
72     {deleteW,      kDELETE},
73     {doW,          kDO},
74     {elseW,        kELSE},
75     {falseW,       kFALSE},
76     {finallyW,     kFINALLY},
77     {forW,         kFOR},
78     {functionW,    kFUNCTION},
79     {ifW,          kIF},
80     {inW,          kIN},
81     {instanceofW,  kINSTANCEOF},
82     {newW,         kNEW},
83     {nullW,        kNULL},
84     {returnW,      kRETURN},
85     {switchW,      kSWITCH},
86     {thisW,        kTHIS},
87     {throwW,       kTHROW},
88     {trueW,        kTRUE},
89     {tryW,         kTRY},
90     {typeofW,      kTYPEOF},
91     {undefinedW,   kUNDEFINED},
92     {varW,         kVAR},
93     {voidW,        kVOID},
94     {whileW,       kWHILE},
95     {withW,        kWITH}
96 };
97
98 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
99 {
100     ctx->hres = hres;
101     return -1;
102 }
103
104 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
105 {
106     const WCHAR *p1 = ctx->ptr;
107     const WCHAR *p2 = word;
108
109     while(p1 < ctx->end && *p2) {
110         if(*p1 != *p2)
111             return *p1 - *p2;
112         p1++;
113         p2++;
114     }
115
116     if(*p2 || (p1 < ctx->end && isalnumW(*p1)))
117         return 1;
118
119     *lval = ctx->ptr;
120     ctx->ptr = p1;
121     return 0;
122 }
123
124 /* ECMA-262 3rd Edition    7.3 */
125 static BOOL is_endline(WCHAR c)
126 {
127     return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
128 }
129
130 static BOOL is_identifier_char(WCHAR c)
131 {
132     return isalnumW(c) || c == '$' || c == '_' || c == '\\';
133 }
134
135 static int hex_to_int(WCHAR c)
136 {
137     if('0' <= c && c <= '9')
138         return c-'0';
139
140     if('a' <= c && c <= 'f')
141         return c-'a'+10;
142
143     if('A' <= c && c <= 'F')
144         return c-'A'+10;
145
146     return -1;
147 }
148
149 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
150 {
151     int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
152
153     while(min <= max) {
154         i = (min+max)/2;
155
156         r = check_keyword(ctx, keywords[i].word, lval);
157         if(!r)
158             return keywords[i].token;
159
160         if(r > 0)
161             min = i+1;
162         else
163             max = i-1;
164     }
165
166     return 0;
167 }
168
169 static void skip_spaces(parser_ctx_t *ctx)
170 {
171     while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
172         if(is_endline(*ctx->ptr++))
173             ctx->nl = TRUE;
174     }
175 }
176
177 static BOOL skip_comment(parser_ctx_t *ctx)
178 {
179     if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
180         return FALSE;
181
182     switch(ctx->ptr[1]) {
183     case '*':
184         ctx->ptr += 2;
185         while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
186             ctx->ptr++;
187
188         if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
189             ctx->ptr += 2;
190         }else {
191             WARN("unexpected end of file (missing end of comment)\n");
192             ctx->ptr = ctx->end;
193         }
194         break;
195     case '/':
196         ctx->ptr += 2;
197         while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
198             ctx->ptr++;
199         break;
200     default:
201         return FALSE;
202     }
203
204     return TRUE;
205 }
206
207 static BOOL unescape(WCHAR *str)
208 {
209     WCHAR *pd, *p, c;
210     int i;
211
212     pd = p = str;
213     while(*p) {
214         if(*p != '\\') {
215             *pd++ = *p++;
216             continue;
217         }
218
219         p++;
220         c = 0;
221
222         switch(*p) {
223         case '\'':
224         case '\"':
225         case '\\':
226             c = *p;
227             break;
228         case 'b':
229             c = '\b';
230             break;
231         case 't':
232             c = '\t';
233             break;
234         case 'n':
235             c = '\n';
236             break;
237         case 'v':
238             c = '\v';
239             break;
240         case 'f':
241             c = '\f';
242             break;
243         case 'r':
244             c = '\r';
245             break;
246         case '0':
247             break;
248         case 'x':
249             i = hex_to_int(*++p);
250             if(i == -1)
251                 return FALSE;
252             c = i << 16;
253
254             i = hex_to_int(*++p);
255             if(i == -1)
256                 return FALSE;
257             c += i;
258             break;
259         case 'u':
260             i = hex_to_int(*++p);
261             if(i == -1)
262                 return FALSE;
263             c = i << 24;
264
265             i = hex_to_int(*++p);
266             if(i == -1)
267                 return FALSE;
268             c += i << 16;
269
270             i = hex_to_int(*++p);
271             if(i == -1)
272                 return FALSE;
273             c += 1 << 8;
274
275             i = hex_to_int(*++p);
276             if(i == -1)
277                 return FALSE;
278             c += i;
279             break;
280         default:
281             c = *p;
282         }
283
284         *pd++ = c;
285         p++;
286     }
287
288     *pd = 0;
289     return TRUE;
290 }
291
292 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
293 {
294     const WCHAR *ptr = ctx->ptr++;
295     WCHAR *wstr;
296     int len;
297
298     while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
299         ctx->ptr++;
300
301     len = ctx->ptr-ptr;
302
303     *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
304     memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
305     wstr[len] = 0;
306
307     /* FIXME: unescape */
308     return tIdentifier;
309 }
310
311 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
312 {
313     const WCHAR *ptr = ++ctx->ptr;
314     WCHAR *wstr;
315     int len;
316
317     while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
318         if(*ctx->ptr++ == '\\')
319             ctx->ptr++;
320     }
321
322     if(ctx->ptr == ctx->end) {
323         WARN("unexpected end of file\n");
324         return lex_error(ctx, E_FAIL);
325     }
326
327     len = ctx->ptr-ptr;
328
329     *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
330     memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
331     wstr[len] = 0;
332
333     ctx->ptr++;
334
335     if(!unescape(wstr)) {
336         WARN("unescape failed\n");
337         return lex_error(ctx, E_FAIL);
338     }
339
340     return tStringLiteral;
341 }
342
343 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
344 {
345     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
346
347     ret->vt = VT_I4;
348     ret->u.lval = l;
349
350     return ret;
351 }
352
353 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
354 {
355     double d, tmp = 1.0;
356
357     if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
358         ERR("No digit after point\n");
359         return 0;
360     }
361
362     d = int_part;
363     while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
364         d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
365
366     if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
367         int sign = 1, e = 0;
368
369         ctx->ptr++;
370         if(ctx->ptr < ctx->end) {
371             if(*ctx->ptr == '+') {
372                 ctx->ptr++;
373             }else if(*ctx->ptr == '-') {
374                 sign = -1;
375                 ctx->ptr++;
376             }else if(!isdigitW(*ctx->ptr)) {
377                 WARN("Expected exponent part\n");
378                 return lex_error(ctx, E_FAIL);
379             }
380         }
381
382         if(ctx->ptr == ctx->end) {
383             WARN("unexpected end of file\n");
384             return lex_error(ctx, E_FAIL);
385         }
386
387         while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
388             e = e*10 + *ctx->ptr++ - '0';
389         e *= sign;
390
391         d *= pow(10, e);
392     }
393
394     *literal = parser_alloc(ctx, sizeof(literal_t));
395     (*literal)->vt = VT_R8;
396     (*literal)->u.dval = d;
397
398     return tNumericLiteral;
399 }
400
401 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
402 {
403     LONG l, d;
404
405     l = *ctx->ptr++ - '0';
406     if(ctx->ptr == ctx->end) {
407         *literal = alloc_int_literal(ctx, l);
408         return tNumericLiteral;
409     }
410
411     if(!l) {
412         if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
413             if(++ctx->ptr == ctx->end) {
414                 ERR("unexpexted end of file\n");
415                 return 0;
416             }
417
418             while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
419                 l = l*16 + d;
420                 ctx->ptr++;
421             }
422
423             if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
424                 WARN("unexpected identifier char\n");
425                 return lex_error(ctx, E_FAIL);
426             }
427
428             *literal = alloc_int_literal(ctx, l);
429             return tNumericLiteral;
430         }
431
432         if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
433             WARN("wrong char after zero\n");
434             return lex_error(ctx, E_FAIL);
435         }
436
437         *literal = alloc_int_literal(ctx, 0);
438     }
439
440     while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
441         l = l*10 + *(ctx->ptr++)-'0';
442
443     if(ctx->ptr < ctx->end) {
444         if(*ctx->ptr == '.') {
445             ctx->ptr++;
446             return parse_double_literal(ctx, l, literal);
447         }
448
449         if(is_identifier_char(*ctx->ptr)) {
450             WARN("unexpected identifier char\n");
451             return lex_error(ctx, E_FAIL);
452         }
453     }
454
455     *literal = alloc_int_literal(ctx, l);
456     return tNumericLiteral;
457 }
458
459 int parser_lex(void *lval, parser_ctx_t *ctx)
460 {
461     int ret;
462
463     ctx->nl = FALSE;
464
465     do {
466         skip_spaces(ctx);
467         if(ctx->ptr == ctx->end)
468             return 0;
469     }while(skip_comment(ctx));
470
471     if(isalphaW(*ctx->ptr)) {
472         ret = check_keywords(ctx, lval);
473         if(ret)
474             return ret;
475
476         return parse_identifier(ctx, (const WCHAR**)lval);
477     }
478
479     if(isdigitW(*ctx->ptr))
480         return parse_numeric_literal(ctx, lval);
481
482     switch(*ctx->ptr) {
483     case '{':
484     case '(':
485     case ')':
486     case '[':
487     case ']':
488     case ';':
489     case ',':
490     case '~':
491     case '?':
492     case ':':
493         return *ctx->ptr++;
494
495     case '}':
496         *(const WCHAR**)lval = ctx->ptr++;
497         return '}';
498
499     case '.':
500         if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
501             return parse_double_literal(ctx, 0, lval);
502         return '.';
503
504     case '<':
505         if(++ctx->ptr == ctx->end) {
506             *(int*)lval = EXPR_LESS;
507             return tRelOper;
508         }
509
510         switch(*ctx->ptr) {
511         case '=':  /* <= */
512             ctx->ptr++;
513             *(int*)lval = EXPR_LESSEQ;
514             return tRelOper;
515         case '<':  /* << */
516             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
517                 ctx->ptr++;
518                 *(int*)lval = EXPR_ASSIGNLSHIFT;
519                 return tAssignOper;
520             }
521             *(int*)lval = EXPR_LSHIFT;
522             return tShiftOper;
523         default: /* < */
524             *(int*)lval = EXPR_LESS;
525             return tRelOper;
526         }
527
528     case '>':
529         if(++ctx->ptr == ctx->end) { /* > */
530             *(int*)lval = EXPR_GREATER;
531             return tRelOper;
532         }
533
534         switch(*ctx->ptr) {
535         case '=':  /* >= */
536             ctx->ptr++;
537             *(int*)lval = EXPR_GREATEREQ;
538             return tRelOper;
539         case '>':  /* >> */
540             if(++ctx->ptr < ctx->end) {
541                 if(*ctx->ptr == '=') {  /* >>= */
542                     ctx->ptr++;
543                     *(int*)lval = EXPR_ASSIGNRSHIFT;
544                     return tAssignOper;
545                 }
546                 if(*ctx->ptr == '>') {  /* >>> */
547                     if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* >>>= */
548                         ctx->ptr++;
549                         *(int*)lval = EXPR_ASSIGNRRSHIFT;
550                         return tAssignOper;
551                     }
552                     *(int*)lval = EXPR_RRSHIFT;
553                     return tRelOper;
554                 }
555             }
556             *(int*)lval = EXPR_RSHIFT;
557             return tShiftOper;
558         default:
559             *(int*)lval = EXPR_GREATER;
560             return tRelOper;
561         }
562
563     case '+':
564         ctx->ptr++;
565         if(ctx->ptr < ctx->end) {
566             switch(*ctx->ptr) {
567             case '+':  /* ++ */
568                 ctx->ptr++;
569                 return tINC;
570             case '=':  /* += */
571                 ctx->ptr++;
572                 *(int*)lval = EXPR_ASSIGNADD;
573                 return tAssignOper;
574             }
575         }
576         return '+';
577
578     case '-':
579         ctx->ptr++;
580         if(ctx->ptr < ctx->end) {
581             switch(*ctx->ptr) {
582             case '-':  /* -- */
583                 ctx->ptr++;
584                 return tDEC;
585             case '=':  /* -= */
586                 ctx->ptr++;
587                 *(int*)lval = EXPR_ASSIGNSUB;
588                 return tAssignOper;
589             }
590         }
591         return '-';
592
593     case '*':
594         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
595             ctx->ptr++;
596             *(int*)lval = EXPR_ASSIGNMUL;
597             return tAssignOper;
598         }
599         return '*';
600
601     case '%':
602         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
603             ctx->ptr++;
604             *(int*)lval = EXPR_ASSIGNMOD;
605             return tAssignOper;
606         }
607         return '%';
608
609     case '&':
610         if(++ctx->ptr < ctx->end) {
611             switch(*ctx->ptr) {
612             case '=':  /* &= */
613                 ctx->ptr++;
614                 *(int*)lval = EXPR_ASSIGNAND;
615                 return tAssignOper;
616             case '&':  /* && */
617                 ctx->ptr++;
618                 return tANDAND;
619             }
620         }
621         return '&';
622
623     case '|':
624         if(++ctx->ptr < ctx->end) {
625             switch(*ctx->ptr) {
626             case '=':  /* |= */
627                 ctx->ptr++;
628                 *(int*)lval = EXPR_ASSIGNOR;
629                 return tAssignOper;
630             case '|':  /* || */
631                 ctx->ptr++;
632                 return tOROR;
633             }
634         }
635         return '|';
636
637     case '^':
638         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* ^= */
639             ctx->ptr++;
640             *(int*)lval = EXPR_ASSIGNXOR;
641             return tAssignOper;
642         }
643         return '^';
644
645     case '!':
646         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* != */
647             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* !== */
648                 ctx->ptr++;
649                 *(int*)lval = EXPR_NOTEQEQ;
650                 return tEqOper;
651             }
652             *(int*)lval = EXPR_NOTEQ;
653             return tEqOper;
654         }
655         return '!';
656
657     case '=':
658         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* == */
659             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* === */
660                 ctx->ptr++;
661                 *(int*)lval = EXPR_EQEQ;
662                 return tEqOper;
663             }
664             *(int*)lval = EXPR_EQ;
665             return tEqOper;
666         }
667         return '=';
668
669     case '/':
670         if(++ctx->ptr < ctx->end) {
671             if(*ctx->ptr == '=') {  /* /= */
672                 ctx->ptr++;
673                 *(int*)lval = EXPR_ASSIGNDIV;
674                 return tAssignOper;
675             }
676         }
677         return '/';
678
679     case '\"':
680     case '\'':
681         return parse_string_literal(ctx, (const WCHAR**)lval, *ctx->ptr);
682
683     case '_':
684     case '$':
685         return parse_identifier(ctx, lval);
686     }
687
688     WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
689     return 0;
690 }
691
692 static void add_object_literal(parser_ctx_t *ctx, DispatchEx *obj)
693 {
694     obj_literal_t *literal = parser_alloc(ctx, sizeof(obj_literal_t));
695
696     literal->obj = obj;
697     literal->next = ctx->obj_literals;
698     ctx->obj_literals = literal;
699 }
700
701 literal_t *parse_regexp(parser_ctx_t *ctx)
702 {
703     const WCHAR *re, *flags;
704     DispatchEx *regexp;
705     literal_t *ret;
706     DWORD re_len;
707     HRESULT hres;
708
709     TRACE("\n");
710
711     re = ctx->ptr;
712     while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
713         if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
714             ctx->ptr++;
715     }
716
717     if(ctx->ptr == ctx->end) {
718         WARN("unexpected end of file\n");
719         return NULL;
720     }
721
722     re_len = ctx->ptr-re;
723
724     flags = ++ctx->ptr;
725     while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
726         ctx->ptr++;
727
728     hres = create_regexp_str(ctx->script, re, re_len, flags, ctx->ptr-flags, &regexp);
729     if(FAILED(hres))
730         return NULL;
731
732     add_object_literal(ctx, regexp);
733
734     ret = parser_alloc(ctx, sizeof(literal_t));
735     ret->vt = VT_DISPATCH;
736     ret->u.disp = (IDispatch*)_IDispatchEx_(regexp);
737     return ret;
738 }