home · contact · privacy
Improved and defined more precisely textfile_sizes().
[plomrogue] / src / command_db.c
index 0d4cb944d56cc94cba117ae6d06a1965525d6429..bdcdcd1fa83a49faa343231360f4f72add0b4bcf 100644 (file)
@@ -87,20 +87,18 @@ extern void init_command_db(struct World * world)
     exit_err(NULL == file, world, err_o);
     uint16_t lines, linemax;
     exit_err(textfile_sizes(file, &linemax, &lines), world, err_s);
+    char line[linemax + 1];
 
-    char * line = malloc(linemax);
-    exit_err(NULL == line, world, err_m);
     struct Command * cmds = malloc(lines * sizeof(struct Command));
     exit_err(NULL == line, world, err_m);
     uint8_t i = 0;
-    while (fgets(line, linemax, file))
+    while (fgets(line, linemax + 1, file))
     {
         cmds[i].id = atoi(strtok(line, " "));
         copy_tokenized_string(world, &cmds[i].dsc_short, " ", err_m);
         copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err_m);
         i++;
     }
-    free(line);
     exit_err(fclose(file), world, err_c);
 
     world->cmd_db = malloc(sizeof(struct CommandDB));