I have this program that is basically a mini game played in the terminal where the player is in a dungeon and tries to kill a dragon. I have a function that tries to save the game when the player chooses too and i see that its not writing the correct input, thus causing the load of that saved file to crash with a segmentation fault. I used hexdump to see that indeed the input I want to save isn’t correct (I have a saved file that has been given to me with the correct contents for a specific game). The following is my save_game function and relevant structs to the game. Can someone help me identify the issue?
int save_game(const graph_t *graph, const player_t *player)
{
// Create the file's name which is basically the player's name with a ".sav" extension to it
char filename[NAME_SIZE];
snprintf(filename, sizeof(filename), "%s.sav", player->name);
// I open the file to write on it after creating it, or if it already exists I overwrite it
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) return -1;
/* Write the magic number which is a specific saved number that i write in the beggining and end of the file to identify them */
if(write(fd, MAGIC_NUMBER, sizeof(MAGIC_NUMBER) - 1) != sizeof(MAGIC_NUMBER)-1) // χωρίς το null terminator
{
close(fd);
return -1;
}
// Write player's details
if (write(fd, &player->arrows, sizeof(player->arrows)) != sizeof(player->arrows) ||
write(fd, &player->room->id, sizeof(player->room->id)) != sizeof(player->room->id))
{
close(fd);
return -1;
}
// Write graph's details (The graph is basically the dungeon)
if (write(fd, &graph->max_id, sizeof(graph->max_id)) != sizeof(graph->max_id) ||
write(fd, &graph->num_vertices, sizeof(graph->num_vertices)) != sizeof(graph->num_vertices))
{
close(fd);
return -1;
}
// Write every vertice's details (each vertex has an edge that leads to other vertices)
for (unsigned int i = 0; i < graph->num_vertices; i++)
{
vertex_t *vertex = graph->vertices[i];
if (write(fd, &vertex->id, sizeof(vertex->id)) != sizeof(vertex->id) ||
write(fd, &vertex->contents, sizeof(vertex->contents)) != sizeof(vertex->contents)) {
close(fd);
return -1;
}
unsigned int count = 0;
for (edge_t *edge = vertex->edges; edge != NULL; edge = edge->next)
{
count++;
}
if (write(fd, &count, sizeof(count)) != sizeof(count))
{
close(fd);
return -1;
}
for (edge_t *edge = vertex->edges; edge != NULL; edge = edge->next)
{
if (write(fd, &edge->to->id, sizeof(edge->to->id)) != sizeof(edge->to->id))
{
close(fd);
return -1;
}
}
}
// Write the magic number again at the end of the file
if (write(fd, MAGIC_NUMBER, sizeof(MAGIC_NUMBER) - 1) != sizeof(MAGIC_NUMBER) - 1) {
close(fd);
return -1;
}
close(fd);
return 0;
}
typedef struct {
char name[NAME_SIZE];
unsigned int arrows;
vertex_t *room;
} player_t;
typedef struct _vertex {
unsigned int id; // unique id
contents_t contents;
struct _edge *edges; // a vertex connects to a list of edges
} vertex_t;
typedef struct _edge {
vertex_t *to; // the other adjacent vertex
struct _edge *next;
} edge_t;
typedef struct {
unsigned int max_id; // maximum vertex id given at any time
unsigned int num_vertices;
vertex_t **vertices; // the graph is a dynamic array of pointers to vertices
} graph_t;
the following is the output i got from the hexdump after I compared the file’s content i got with the desired content of the saved file. Note that “Dr460N5D3N” is the magic number. The first is the file i created and the second one is the desired output.
2,23c2,24
< 00000010 00 00 0c 00 00 00 0d 00 00 00 01 00 00 00 00 00 |................|
< 00000020 00 00 03 00 00 00 0a 00 00 00 0b 00 00 00 0c 00 |................|
< 00000030 00 00 02 00 00 00 02 00 00 00 04 00 00 00 0d 00 |................|
< 00000040 00 00 08 00 00 00 09 00 00 00 0c 00 00 00 03 00 |................|
< 00000050 00 00 00 00 00 00 03 00 00 00 0b 00 00 00 07 00 |................|
< 00000060 00 00 0c 00 00 00 04 00 00 00 00 00 00 00 03 00 |................|
< 00000070 00 00 06 00 00 00 07 00 00 00 09 00 00 00 05 00 |................|
< 00000080 00 00 02 00 00 00 03 00 00 00 06 00 00 00 07 00 |................|
< 00000090 00 00 09 00 00 00 06 00 00 00 01 00 00 00 03 00 |................|
< 000000a0 00 00 0a 00 00 00 05 00 00 00 04 00 00 00 07 00 |................|
< 000000b0 00 00 04 00 00 00 03 00 00 00 03 00 00 00 05 00 |................|
< 000000c0 00 00 04 00 00 00 08 00 00 00 00 00 00 00 03 00 |................|
< 000000d0 00 00 0b 00 00 00 0a 00 00 00 02 00 00 00 09 00 |................|
< 000000e0 00 00 03 00 00 00 04 00 00 00 0d 00 00 00 05 00 |................|
< 000000f0 00 00 04 00 00 00 02 00 00 00 0a 00 00 00 01 00 |................|
< 00000100 00 00 03 00 00 00 08 00 00 00 06 00 00 00 01 00 |................|
< 00000110 00 00 0b 00 00 00 00 00 00 00 03 00 00 00 03 00 |................|
< 00000120 00 00 08 00 00 00 01 00 00 00 0c 00 00 00 01 00 |................|
< 00000130 00 00 03 00 00 00 03 00 00 00 02 00 00 00 01 00 |................|
< 00000140 00 00 0d 00 00 00 00 00 00 00 02 00 00 00 09 00 |................|
< 00000150 00 00 02 00 00 00 44 72 34 36 30 4e 35 44 33 4e |......Dr460N5D3N|
< 00000160
---
> 00000010 00 00 0d 00 00 00 0d 00 00 00 44 72 34 36 30 4e |..........Dr460N|
> 00000020 35 44 33 4e 01 00 00 00 00 00 00 00 02 00 00 00 |5D3N............|
> 00000030 02 00 00 00 03 00 00 00 00 00 00 00 04 00 00 00 |................|
> 00000040 00 00 00 00 05 00 00 00 02 00 00 00 06 00 00 00 |................|
> 00000050 01 00 00 00 07 00 00 00 04 00 00 00 08 00 00 00 |................|
> 00000060 00 00 00 00 09 00 00 00 03 00 00 00 0a 00 00 00 |................|
> 00000070 01 00 00 00 0b 00 00 00 00 00 00 00 0c 00 00 00 |................|
> 00000080 01 00 00 00 0d 00 00 00 00 00 00 00 03 00 00 00 |................|
> 00000090 0a 00 00 00 0b 00 00 00 0c 00 00 00 04 00 00 00 |................|
> 000000a0 0d 00 00 00 08 00 00 00 09 00 00 00 0c 00 00 00 |................|
> 000000b0 03 00 00 00 0b 00 00 00 07 00 00 00 0c 00 00 00 |................|
> 000000c0 03 00 00 00 06 00 00 00 07 00 00 00 09 00 00 00 |................|
> *
> 000000e0 03 00 00 00 0a 00 00 00 05 00 00 00 04 00 00 00 |................|
> 000000f0 03 00 00 00 03 00 00 00 05 00 00 00 04 00 00 00 |................|
> 00000100 03 00 00 00 0b 00 00 00 0a 00 00 00 02 00 00 00 |................|
> 00000110 04 00 00 00 0d 00 00 00 05 00 00 00 04 00 00 00 |................|
> 00000120 02 00 00 00 03 00 00 00 08 00 00 00 06 00 00 00 |................|
> 00000130 01 00 00 00 03 00 00 00 03 00 00 00 08 00 00 00 |................|
> 00000140 01 00 00 00 03 00 00 00 03 00 00 00 02 00 00 00 |................|
> 00000150 01 00 00 00 02 00 00 00 09 00 00 00 02 00 00 00 |................|
> 00000160 44 72 34 36 30 4e 35 44 33 4e |Dr460N5D3N|
> 0000016a
I tried hexdump and some lldb but i couldnt really pinpoint the issue. The sanitizer also didnt crash when saving.
user23903249 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.