(/home/sate/Testcases/c/cve/wireshark-1.2.0/tools/lemon/lemon.c) |
| |
| 2539 | | | void Parse(struct lemon *gp) |
| 2540 | | | { |
| 2541 | | | struct pstate ps; |
| 2542 | | | FILE *fp; |
| 2543 | | | char *filebuf; |
| 2544 | | | long filesize; |
| 2545 | | | int lineno; |
| 2546 | | | char c; |
| 2547 | | | char *cp, *nextcp; |
| 2548 | | | int startline = 0; |
| 2549 | | | |
| 2550 | | | memset(&ps, '\0', sizeof(ps)); |
| 2551 | | | ps.gp = gp; |
| 2552 | | | ps.filename = gp->filename; |
| 2553 | | | ps.errorcnt = 0; |
| 2554 | | | ps.state = INITIALIZE; |
| 2555 | | | ps.prevrule = NULL; |
| 2556 | | | ps.preccounter = 0; |
| 2557 | | | ps.lastrule = NULL; |
| 2558 | | | ps.firstrule = NULL; |
| 2559 | | | ps.lhs = NULL; |
| 2560 | | | ps.nrhs = 0; |
| 2561 | | | ps.lhsalias = NULL; |
| 2562 | | | ps.declkeyword = NULL; |
| 2563 | | | ps.declargslot = NULL; |
| 2564 | | | ps.declassoc = UNK; |
| 2565 | | | ps.fallback = NULL; |
| 2566 | | | |
| 2567 | | | |
| 2568 | | | fp = fopen(ps.filename,"rb"); |
| 2569 | | | if( fp==0 ){ |
| 2570 | | | ErrorMsg(ps.filename,0,"Can't open this file for reading."); |
| 2571 | | | gp->errorcnt++; |
| 2572 | | | return; |
| 2573 | | | } |
| 2574 | | | fseek(fp,0,2); |
Ignored Return Value
The return value of fseek() is never checked in the highlighted execution scenario. - If the return value can indicate an error, the error will be ignored if the highlighted code executes.
- CodeSonar is configured to enforce Ignored Return Value checks for fseek(). (To change the set of enforced Ignored Return Value checks, use configuration file parameters RETURN_CHECKER_CHECKED_FUNCS and RETURN_CHECKER_IGNORED_FUNCS).
Show: All events | Only primary events |
|
| 2575 | | | filesize = ftell(fp); |
| 2576 | | | rewind(fp); |
| 2577 | | | |
| 2578 | | | filebuf = (char *)malloc( filesize+1 ); |
| 2579 | | | if( filebuf==0 ){ |
Event 2:
Skipping " if". filebuf == 0 evaluates to false.
hide
|
|
| 2580 | | | ErrorMsg(ps.filename,0,"Can't allocate %ld of memory to hold this file.", |
| 2581 | | | filesize+1); |
| 2582 | | | gp->errorcnt++; |
| 2583 | | | return; |
| 2584 | | | } |
| 2585 | | | if( fread(filebuf,1,filesize,fp)!=(size_t)filesize ){ |
Event 3:
Skipping " if". fread(...) != (size_t)filesize evaluates to false.
hide
|
|
| 2586 | | | ErrorMsg(ps.filename,0,"Can't read in all %ld bytes of this file.", |
| 2587 | | | filesize); |
| 2588 | | | free(filebuf); |
| 2589 | | | gp->errorcnt++; |
| 2590 | | | return; |
| 2591 | | | } |
| 2592 | | | fclose(fp); |
| 2593 | | | filebuf[filesize] = 0; |
| 2594 | | | |
| 2595 | | | |
| 2596 | | | preprocess_input(filebuf); |
| 2597 | | | |
| 2598 | | | |
| 2599 | | | lineno = 1; |
| 2600 | | | for(cp=filebuf; (c= *cp)!=0; ){ |
| 2601 | | | if( c=='\n' ) lineno++; |
| 2602 | | | if( safe_isspace(c) ){ cp++; continue; } |
| 2603 | | | if( c=='/' && cp[1]=='/' ){ |
| 2604 | | | cp+=2; |
| 2605 | | | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2606 | | | continue; |
| 2607 | | | } |
| 2608 | | | if( c=='/' && cp[1]=='*' ){ |
| 2609 | | | cp+=2; |
| 2610 | | | while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){ |
| 2611 | | | if( c=='\n' ) lineno++; |
| 2612 | | | cp++; |
| 2613 | | | } |
| 2614 | | | if( c ) cp++; |
| 2615 | | | continue; |
| 2616 | | | } |
| 2617 | | | ps.tokenstart = cp; |
| 2618 | | | ps.tokenlineno = lineno; |
| 2619 | | | if( c=='\"' ){ |
| 2620 | | | cp++; |
| 2621 | | | while( (c= *cp)!=0 && c!='\"' ){ |
| 2622 | | | if( c=='\n' ) lineno++; |
| 2623 | | | cp++; |
| 2624 | | | } |
| 2625 | | | if( c==0 ){ |
| 2626 | | | ErrorMsg(ps.filename,startline, |
| 2627 | | | "String starting on this line is not terminated before the end of the file."); |
| 2628 | | | ps.errorcnt++; |
| 2629 | | | nextcp = cp; |
| 2630 | | | }else{ |
| 2631 | | | nextcp = cp+1; |
| 2632 | | | } |
| 2633 | | | }else if( c=='{' ){ |
| 2634 | | | int level; |
| 2635 | | | cp++; |
| 2636 | | | for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){ |
| 2637 | | | if( c=='\n' ) lineno++; |
| 2638 | | | else if( c=='{' ) level++; |
| 2639 | | | else if( c=='}' ) level--; |
| 2640 | | | else if( c=='/' && cp[1]=='*' ){ |
| 2641 | | | char prevc; |
| 2642 | | | cp = &cp[2]; |
| 2643 | | | prevc = 0; |
| 2644 | | | while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){ |
| 2645 | | | if( c=='\n' ) lineno++; |
| 2646 | | | prevc = c; |
| 2647 | | | cp++; |
| 2648 | | | } |
| 2649 | | | }else if( c=='/' && cp[1]=='/' ){ |
| 2650 | | | cp = &cp[2]; |
| 2651 | | | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2652 | | | if( c ) lineno++; |
| 2653 | | | }else if( c=='\'' || c=='\"' ){ |
| 2654 | | | char startchar, prevc; |
| 2655 | | | startchar = c; |
| 2656 | | | prevc = 0; |
| 2657 | | | for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){ |
| 2658 | | | if( c=='\n' ) lineno++; |
| 2659 | | | if( prevc=='\\' ) prevc = 0; |
| 2660 | | | else prevc = c; |
| 2661 | | | } |
| 2662 | | | } |
| 2663 | | | } |
| 2664 | | | if( c==0 ){ |
| 2665 | | | ErrorMsg(ps.filename,ps.tokenlineno, |
| 2666 | | | "C code starting on this line is not terminated before the end of the file."); |
| 2667 | | | ps.errorcnt++; |
| 2668 | | | nextcp = cp; |
| 2669 | | | }else{ |
| 2670 | | | nextcp = cp+1; |
| 2671 | | | } |
| 2672 | | | }else if( safe_isalnum(c) ){ |
| 2673 | | | while( (c= *cp)!=0 && (safe_isalnum(c) || c=='_') ) cp++; |
| 2674 | | | nextcp = cp; |
| 2675 | | | }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ |
| 2676 | | | cp += 3; |
| 2677 | | | nextcp = cp; |
| 2678 | | | }else if( (c=='/' || c=='|') && safe_isalpha(cp[1]) ){ |
| 2679 | | | cp += 2; |
| 2680 | | | while( (c = *cp)!=0 && (safe_isalnum(c) || c=='_') ) cp++; |
| 2681 | | | nextcp = cp; |
| 2682 | | | }else{ |
| 2683 | | | cp++; |
| 2684 | | | nextcp = cp; |
| 2685 | | | } |
| 2686 | | | c = *cp; |
| 2687 | | | *cp = 0; |
| 2688 | | | parseonetoken(&ps); |
| 2689 | | | *cp = c; |
| 2690 | | | cp = nextcp; |
| 2691 | | | } |
| 2692 | | | free(filebuf); |
| 2693 | | | gp->rule = ps.firstrule; |
| 2694 | | | gp->errorcnt = ps.errorcnt; |
| 2695 | | | } |
| |