Program crashing when reading text from files
I'm writing a code obfuscator in C. Debugger shows no errors in the code, but the program crashes after compiling-- what can be the problem here? I'm guessing it has something to do with while loops or reading data from files, but I'm not sure how to fix it. Thanks in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// list off all replaced elements
typedef struct ReplaceList
{
char *from; // from string
char *to; // to string (random)
struct ReplaceList *next;
} ReplaceList;
// ANSI C keywords that can't be changed
typedef struct Data
{
char *kword;
struct Data *next;
} Data;
// add keyword to list
Data* AddToList(Data **head, Data *d)
{
d->next = *head;
*head = d;
return d;
}
// delete all elements from list
void FreeList(Data *head)
{
Data *prev;
while (head != NULL)
{
prev = head;
head = head->next;
free(prev);
}
}
// Generating random numbers
int random1()
{
return ((rand() % 6) + 1);
}
// return other characters from file and erase comments
char *getOtherCharacters(FILE *file){
char *buf = (char*)malloc(sizeof(char));
char a;
int n=0;
while(!feof(file)){
a= fgetc(file);
if(
(!((a >= 'a') && (a<='z'))) &&
(!((a >= 'A') && (a <= 'Z'))) &&
(a != '_')
)
{
// comment
if(a == '/'){
a = fgetc(file);
// until end of line
if(a == '/'){
while(!feof(file)){
a = fgetc(file);
if(a == '\n')
break;
}
}
else if(a == '*'){
while(!feof(file)){
a = fgetc(file);
if(a=='*'){
a = fgetc(file);
if(a == '/')
break;
else
fseek(file,ftell(file)-1,SEEK_SET);
}
}
}
// other chars
else{
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
}
else{
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
}
else{
fseek(file,ftell(file)-1,SEEK_SET);
buf = (char*)realloc(buf,++n);
buf[n-1] = '\0';
return buf;
}
}
return NULL;
}
// return word from file
char *getWord(FILE *file){
char *buf = (char*)malloc(sizeof(char));
int n=0;
char a;
int ok = 0;
while(!feof(file)){
a = fgetc(file);
if(
((a >= 'a') && (a<='z')) ||
((a >= 'A') && (a <= 'Z')) ||
( (ok==1)&& (a>='0') && (a<='9')) ||
(a == '_')
)
{
ok = 1;
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
else if(ok==1){
if(!feof(file))
fseek(file,ftell(file)-1,SEEK_SET);
buf = (char*)realloc(buf,++n);
buf[n-1] = '\0';
return buf;
}
else
return NULL;
}
return NULL;
}
// check if word exist in ReplaceList
ReplaceList *checkWord_ReplaceList(char *word,ReplaceList *Head){
ReplaceList *replaceList;
replaceList = Head;
while(replaceList != NULL){
if(strcmp(word, replaceList->from) == 0){
return replaceList;
}
replaceList = replaceList->next;
}
return NULL;
}
// check if word exist in Data struct
Data *checkWord_Data(char *word,Data *Head){
Data *data;
data = Head;
while(data != NULL){
if(strcmp(word, data->kword) == 0){
return data;
}
data = data->next;
}
return NULL;
}
// generate new element of ReplaceList and return its instance
ReplaceList *newReplaceListElement(ReplaceList *begin, char *from){
int random;
ReplaceList *tmp;
int i;
tmp = (ReplaceList*)malloc(sizeof(ReplaceList));
tmp->to = (char*)malloc(sizeof(char)*11);
for(i=0; i<10;i++)
tmp->to[i] = rand()%26 + 'a';
tmp->to[i] = '\0';
tmp->from = from;
tmp->next = begin;
begin = tmp;
return tmp;
}
int main(int argc, char *argv[])
{
FILE * input;
FILE * output;
FILE *kwords;
Data *dataBegin = NULL;
ReplaceList *replaceListBegin = NULL;
int a, i=0;
char c;
srand(time(NULL));
a = random1();
kwords = fopen("ckeywords.txt","r");
// get all magic words
if(kwords != NULL){
// read data to the first list
while (!feof(kwords))
{
Data *d;
d = (Data *) malloc( sizeof(Data)); // allocate new element
d->kword = getWord(kwords);
fscanf(kwords,"\n");
AddToList(&dataBegin, d);
}
fclose(kwords);
}
input = fopen("inputcode.txt","r");
output = fopen("outputcode.txt","w");
// parse file
if(input != NULL){
while(!feof(input)){
char *word;
Data* data;
word = getWord(input);
if(word != NULL){
data = checkWord_Data(word,dataBegin);
if(data!=NULL){
fprintf(output,"%s",word);
}
else{
ReplaceList *replaceList;
replaceList = checkWord_ReplaceList(word,replaceListBegin);
if(replaceList == NULL){
replaceList = newReplaceListElement(replaceListBegin,word);
}
fprintf(output,"%s",replaceList->to);
}
}
else{
word = getOtherCharacters(input);
fprintf(output,"%s",word);
}
}
}
fclose(input);
fclose(output);
if (strcmp(argv[i], "-i") == 0)
while (!feof(input))
{
c=(char)fgetc(input);
printf("%c", c);
}
if (strcmp(argv[i], "-o") == 0)
while (!feof(output))
{
c=(char)fgetc(output);
printf("%c", c);
}
return 0;
}
Re: Program crashing when reading text from files
Quote:
Debugger shows no errors in the code
The debugger is there to help you debug your code - it doesn't show compile errors. The compiler will show these - or the linker will show linking errors.
If the program compiles and links without error then the problem is a logic error within the program. For that you need to use the debugger to trace through the code to see where the execution deviates from that expected from your design.
Quote:
but the program crashes after compiling
What error is shown?
What os/compiler are you using?
Re: Program crashing when reading text from files
Quote:
Originally Posted by
moedea
Debugger shows no errors in the code, but the program crashes after compiling
You have misconceptions as to what the compiler and debugger do. If you had a program that is supposed to add two numbers, but instead by mistake you coded it to subtract two numbers, is that a valid 'C' program? It is valid if it compiles OK, but of course it doesn't do what you expected it to do, so you debug the program.
All the compiler does is to confirm that the code is syntactically correct and if correct, creates an intermediate file called object code. The compiler does not prove that the program will work correctly. The linker program then takes the object code and creates the final executable, which you then run to confirm that the program is coded correctly.
When you run the executable, you are responsible for fixing any runtime bugs. To do that, you debug your program using the debugger. The bottom line is that just because a program compiles OK doesn't mean it will run OK.
Quote:
what can be the problem here? I'm guessing
Don't guess -- run the program using the debugger and debug the code. That is what everyone here would do to address your problem.
Quote:
but I'm not sure how to fix it.
But didn't you write the code? If you did, then you must have had a plan (as 2kaud mentioned) to allow you to write such code. So debug the code and see where your plan falls apart. Once you see where this is, then you fix the code (or write the code differently, depending on your findings).
Regards,
Paul McKenzie
Re: Program crashing when reading text from files
In addition, you should ALWAYS check that a pointer is not NULL before using it.
Your getOtherCharacters() function can return NULL which will cause your program to crash when you attempt to write the data to the stream.
Best regards,
Doron Moraz