Hello,
I am stuck with a weird segfault since yesterday. I call scanf, then the program crashes instantly, but only if another fgets is referenced later in the code.
Here are some snippets:
main.c
Code:
#include "../misc.h"
#include "../parse_maps.h"
#include "../search.h"
int main(int argc, char *argv[])
{
unsigned long remote_offset;
pid_t remote_pid;
map_line_t map_line;
FILE* map_file = NULL;
validate_start(argc, argv, &remote_pid); // The scanf call is within this function's body
open_map_file(remote_pid, map_file);
load_map_data_for_filename(map_file, &map_line, "[heap]"); // The fgets call is within this function's body
//remote_offset = memory_offsets_where_string_equals(remote_pid, map_line, "heapvartwo");
//printf("Offset is at %#lx\n", remote_offset);
//fclose(map_file);
return 0;
}
misc.c
Code:
#include "misc.h"
#include <stdio.h>
#include <stdlib.h> // atoi
void validate_start(int argc, char* argv[], pid_t* pid)
{
char tmp[100];
if (argc == 2) {
sprintf(tmp, "%s", argv[1]);
*pid = atoi(tmp);
}
else
{
puts("Enter the PID of target process: ");
int lol;
while(scanf("%d", &lol) == 0) // This is a little bugged, but gets the job done. The program crashes immediately after scanf succeeds
{
puts("Not a valid number, try again:");
getchar();
}
}
printf("yeye"); // This never prints, except when I comment out the fgets in the function below
}
Finally, parse_maps.c
Code:
#include "parse_maps.h"
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "valuetypes.h"
void open_map_file(pid_t pid, FILE* map_file)
{
//code
}
void read_map_line(char* line, map_line_t* map_line)
{
//code
}
void load_map_data_for_filename(FILE* map_file, map_line_t* map_line, char* filename)
{
char tmp[1000];
map_line_t curr_map;
while (fgets(tmp, 999, map_file) != NULL) // This line makes the scanf above crash for some reason
{
// read_map_line(tmp, &curr_map);
// if(strcmp(curr_map.filename, filename) == 0)
// {
// read_map_line(tmp, map_line);
// return;
// }
}
}
I really struggle to understand what is going on, I'm sure it's something really stupid like a bad pointer, but I've tried many things without success..
I use GCC 8.2.1 or tcc 0.9.27 to compile, without any difference
Do you need more info to reproduce the issue, or find out something?
Thanks for your replies!