-2

I write this lex program but there is an error if you can check my code because i check it a lot, but i didn't find it.

This is the code:

%{
    #include <stdio.h>
    #include <string.h>
    
    int WordLength=0;
    char LongestWord[200];
    
    int arrayIndex=0;
    double num[2000]={0};
    
    int numofvowel=0;
    char VowelArray[100][100];
    
    int IngNum=0;
    
    int numLine=0;
%}

%%

[aeiouAEIOU][a-zA-Z]+  {strcpy(VowelArray[numofvowel],
    yytext);numofvowel+=1;}
    
[.+ing]+ {IngNum+=1;}
    
[0-9]+    {if(atof(yytext) > 500) 
    {num[arrayIndex]=atof(yytext);arrayIndex+=1;}  }
    
[a-zA-Z]+ {if (yyleng > WordLength)
              {
                  WordLength=yyleng;
                  strcpy(LongestWord,yytext);
                  }
              }
    
[/n]      {numLine++;}
    
. ;
    
%%
    
int yywrap(){
    return 1;}
    
int main(int argc[] , char **argv[]){
    char file[] = "";
    printf("Enter the file name : ");
    scanf("%s", file);
    extern FILE *yyin ;
    yyin=fopen(file, "r");
    yylex();
    
    printf("The longest word is %s ",LongestWord);
    
    printf("/n");
    
    printf("The numbers that are greater than 500 are: ");
    for(int i=0 ; i<arrayIndex ; i++){
    printf("%f, ",num[i]);}
    
    printf("There are %d words that start with vowel letter. They
    are:",numofvowel);
    for (int k = 0; k < numofvowel; k++){
        printf("%s, ", VowelArray[k]);}
    
    printf("The number of words that are ended by ”ing”: %d ",IngNum);
    
    printf("The number of lines %d ",numLine);
    return 0;
}

This error appears:

*** stack smashing detected ***: terminated
Aborted (core dumped)
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
nada 1422
  • 1
  • 1
  • 1
    If you want us to look at your code, you need to (1) copy and paste the code into the question, instead of using an image, which cannot be read by people using screenreaders and only with difficulty with smartphones; (2) explain the actual nature of the issue (What happened? With what input? What did you expect to happen?); (3) explain what you learned during your debugging efforts. – rici Sep 22 '22 at 22:19
  • 1
    Also, a brief description of what your program does can be useful. The help centre includes a guide on [ask], which you should take a couple of minutes to read. – rici Sep 22 '22 at 22:25
  • This problem seems to be connected to the C part of the program rather then lex. It would be easier if we known what input causes it. Mishandling some pointers and going beyond the allocated memory would cause such error so it's probably one of the string operations or something with array indexing. – Piotr Siupa Sep 23 '22 at 13:42
  • More about the error: https://stackoverflow.com/questions/1345670/stack-smashing-detected – Piotr Siupa Sep 23 '22 at 13:46
  • @NO_NAME probably identified the cause of that error message, but there are many other issues. First, you should *always* check return values of library functions (`scanf` and `fopen`, for example) to make sure that they don't return an error indication. Similarly, if you don't want to use `malloc` (and you really should learn how to use it), you *must* check to make sure that you don't exceed your arbitrary limits. Third, please review the [Flex pattern syntax](https://westes.github.io/flex/manual/Patterns.html); `[.+ing]` and `[/n]` do not do what you expect, for different reasons. – rici Sep 23 '22 at 18:17

1 Answers1

1

The error is caused by the few first lines of the main function.

Here is a minimal example that causes it:

#include <stdio.h>
int main(){
    char file[] = "";
    scanf("%s", file);
}

This code creates variable file that is only big enough to fit an empty string inside but the function scanf attempts to put a longer string inside.

A fast and unsafe solution would be to just increase the size of the buffer file, e.g:

    char file[100];  // No need to initialize it since scanf will overwrite it anyway.

If you want something more reliable (and you probably should), here are the best practices on avoiding such problems with scanf: How to prevent scanf causing a buffer overflow in C?.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65