This is driving me crazy, so any help will be appreciated.
I am developing a program that scans for Wi-fi access points and manages them with different functionalities. One of those is to save the access points list to a file, another is to read access points from a file and add them to the current list.
Access points are stored in a linked list where ‘p’ contains the info of one access point (mac, mode, encrypted, essid, etc..), and ‘next’ is the pointer to the next node.
Here is part of the function i use to save:
void store_info_in_file(list l)
{
list aux;
aux = l;
FILE *file;
..
..
//After opening the file with a name chosen by the user,it traverses
//the list and with casts to char, saves each part of each access point.
//Conversion to char to ease storage in a binary file.
char essid_len = (char)strlen((aux->p).essid);
char enc = (char)(aux->p).encrypted; //originally an int
fwrite(&essid_len,1,1,file);//i'm guessing the size is sizeof(char) now
And the same for all the parts.
Analyzing the file output with a hex editor, i can see that the data has been stored correctly.
This is the problematic part of the read function:(i read the first character [number of access points contained in the file] and store it for later conversion to int).
//new_apns is for later use, when i will fread the rest of the file at once.
char new_apns;
////wifi_collector_list.c:1103
fread(&new_apns,1,1,fp);
This causes a segmentation fault due to an invalid read of size 4.
Here is the output for:
“valgrind –leak-check=full –show-reachable=yes –track-origins=yes ./app”
Edit:
I am definitely stupid, I had written:
fp = fopen(string,"rb");
if(fp != NULL)
{
//fread here
}
And for some reason changed it to:
if(fopen(string,"rb"))
{
//fread here, so yes, it's null...
}
That’s a huge hint that
fpisNULLat the time of thefread. Maybe you didn’t check it when youfopened it ?