1

I have to write a code for my uni programming course, and I'm still a beginner so I need some help figuring out why the value of 'word1' disappears while the program is still running. I have to write a program which opens a .c file and counts specific reserved words, which I have saved in a .txt file. The code is:

int countResWords(FILE *filep){
    int count=0;
    char word1[20],word2[20],*pos,*token,*pch,*pch2;
    FILE *rp;
    rp=fopen("reservedWords.txt","r+");
    if(rp==NULL){
        perror("File cannot be opened.");
        return -1;
    }
    else{
        while(!feof(rp)){
            fscanf(rp,"%s",&word1);
            count=0;
            while(!feof(filep)){
                fscanf(filep,"%s",&word2);
                if((pch=strchr(word2,'('))!=NULL){
                    token=strtok(word2,"(");
                    while(token!=NULL){
                        if((pch2=strchr(token,')'))!=NULL) strtok(token,")");
                        if(strcmp(token,word1)==0) count++;
                        token=strtok(NULL,"(");
                    }
                }else if(strcmp(word2,word1)==0) count++;
            }
            printf("The reserved word %s was used %d times in the given code.",word1,count);
            rewind(filep);
        }
    }
    fclose(rp);
}

int main(int argc, char *argv[]) {
    char filename[100];
    FILE *fp;
    puts("Give a FILE NAME with its FULL PATH.");
    scanf("%s",&filename);
    fp=fopen(filename,"r+");
    if(fp==NULL){
        perror("File cannot be opened.");
        return -1;
    }
    else countResWords(fp);
    fclose(fp);
}

and the .c file i'm currently testing is:

#include <stdio.h>
#include <stdlib.h>

void selection(void);
void enterText(void);
void enterVoc(void);
void correctText(void);
void statistics(void);
void addWord(void);
void replaceWord(void);
void count(void);

void selection(void){
    puts("selection");
}

void enterText(void){
    puts("enterText");
}

void enterVoc(void){
    puts("enterVoc");
}

void correctText(void){
    puts("correctText");
}

void statistics(void){
    puts("statistics");
}

void addWord(void){
    puts("addWord");
}

void replaceWord(void){
    puts("replaceWord");
}

void count(void){
    puts("count");
}

int main(int argc, char *argv[]) {
    selection();
    enterText();
    enterVoc();
    correctText();
    statistics();
    addWord();
    replaceWord();
    count();
    return 0;
}

somewhere along the line the variable word1 loses its value, and at the printing at the bottom of the countResWords function it doesn't print the word1 like it doesn't exist.

user229044
  • 232,980
  • 40
  • 330
  • 338
Nežumi
  • 61
  • 1
  • 7
  • 3
    Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh May 09 '17 at 18:07
  • `scanf("%s",&filename);` --> `scanf("%99s",filename);` – Sourav Ghosh May 09 '17 at 18:08
  • `word` can hold 19 characters (plus one null terminator) at most. If the C code you scan looks like the one you've posted here, you'll exceed that limit: `scanf("%s", ...)` treats everything that isn't whitespace as word, so the string `if((pch=strchr(word2,'('))!=NULL){` will overflow the buffer. – M Oehm May 09 '17 at 18:08
  • You can enforce a limit on the size of the string to scan with `scanf("%19s", word)`. And don't be stingy with memory: Make the char buffers 80 or more chars. – M Oehm May 09 '17 at 18:11
  • the reserved words, which go into word1, are no more than 9 characters. I've increases the char buffer in word2 to see if something will change. – Nežumi May 09 '17 at 18:17
  • 1
    _So it wasn't something serious._ -- That's undefined behaviour and it is serious. In your case, you just overwrote something else, but overflowing the buffer may well cause the program to crash. You should always enforce that you stay within the valid array bounds.. – M Oehm May 09 '17 at 18:32
  • 1
    If you want to mark your question "resolved", post an answer and mark the answer as "Accepted". Please don't edit answers into your question, and don't put pseduo-tags like `[resolved]` in your titles. – user229044 May 09 '17 at 18:35
  • I also advise you to declare variables only before you use them. Declaring everything at the beginning at the function is useless and confusing. Let's say you don't manage to open the first file, then you'd have declared seven variables for nothing. – Badda May 09 '17 at 18:43

2 Answers2

1

Your code has multiple buffer overrun vulnerabilities, hence it has undefined behaviors if they are triggered.

The particular one that fails the test is: the C test file has lines longer than 20 characters. fscanf(filep,"%s",&word2) causes buffer overwrites when it encounters these lines. If you increases the size of word2 to, say, 120, then your code would print something. However, I didn't verify whether the result is correct.

Chris Tang
  • 567
  • 7
  • 18
stensal
  • 401
  • 4
  • 8
0

The problem was the buffer of word2, which previously was 20. I had to increase it, because when it was more than 19 chars there was a problem with the strcmp() function.

Nežumi
  • 61
  • 1
  • 7