From f6c98358c24a333a66b99a11d35a60bfbc91ae1d Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 21 Mar 2023 18:36:58 +0200 Subject: [PATCH 15/29] lua_command(): Use fc_stat() instead of opening the file Fixes file descriptor leak in the process See osdn #47609 Signed-off-by: Marko Lindqvist --- server/stdinhand.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/server/stdinhand.c b/server/stdinhand.c index 02ec6855b5..1bb860682a 100644 --- a/server/stdinhand.c +++ b/server/stdinhand.c @@ -1239,7 +1239,7 @@ static bool read_init_script_real(struct connection *caller, && (script_file = fc_fopen(real_filename, "r"))) { char buffer[MAX_LEN_CONSOLE_LINE]; - /* the size is set as to not overflow buffer in handle_stdin_input */ + /* The size is set as to not overflow buffer in handle_stdin_input */ while (fgets(buffer, MAX_LEN_CONSOLE_LINE - 1, script_file)) { /* Execute script contents with same permissions as caller */ handle_stdin_input_real(caller, buffer, check, read_recursion + 1); @@ -1285,9 +1285,9 @@ static bool write_init_script(char *script_filename) if (is_reg_file_for_access(real_filename, TRUE) && (script_file = fc_fopen(real_filename, "w"))) { fprintf(script_file, - "#FREECIV SERVER COMMAND FILE, version %s\n", VERSION_STRING); + "#FREECIV SERVER COMMAND FILE, version %s\n", VERSION_STRING); fputs("# These are server options saved from a running freeciv-server.\n", - script_file); + script_file); /* First rulesetdir. Setting rulesetdir resets the settings to their * default value, so they would be lost if placed before this. */ @@ -1316,7 +1316,7 @@ static bool write_init_script(char *script_filename) fprintf(script_file, "metamessage %s\n", get_meta_message_string()); } - /* then, the 'set' option settings */ + /* Then, the 'set' option settings */ settings_iterate(SSET_ALL, pset) { fprintf(script_file, "set %s \"%s\"\n", setting_name(pset), @@ -4893,7 +4893,7 @@ static bool reset_command(struct connection *caller, char *arg, bool check, _("Reset all settings and rereading the server start " "script.")); settings_reset(); - /* load initial script */ + /* Load initial script */ if (NULL != srvarg.script_filename && !read_init_script_real(NULL, srvarg.script_filename, TRUE, FALSE, read_recursion + 1)) { @@ -4980,7 +4980,7 @@ static const char *lua_accessor(int i) static bool lua_command(struct connection *caller, char *arg, bool check, int read_recursion) { - FILE *script_file; + struct stat statbuf; const char extension[] = ".lua", *real_filename = NULL; char luafile[4096], tilde_filename[4096]; char *tokens[1], *luaarg = NULL; @@ -5113,14 +5113,12 @@ static bool lua_command(struct connection *caller, char *arg, bool check, _("Loading Freeciv script file '%s'."), real_filename); if (is_reg_file_for_access(real_filename, FALSE) - && (script_file = fc_fopen(real_filename, "r"))) { + && !fc_stat(real_filename, &statbuf)) { ret = script_server_do_file(caller, real_filename); - goto cleanup; } else { cmd_reply(CMD_LUA, caller, C_FAIL, _("Cannot read Freeciv script '%s'."), real_filename); ret = FALSE; - goto cleanup; } break; case LUA_UNSAFE_FILE: @@ -5128,14 +5126,12 @@ static bool lua_command(struct connection *caller, char *arg, bool check, _("Loading Freeciv script file '%s'."), real_filename); if (is_reg_file_for_access(real_filename, FALSE) - && (script_file = fc_fopen(real_filename, "r"))) { + && !fc_stat(real_filename, &statbuf)) { ret = script_server_unsafe_do_file(caller, real_filename); - goto cleanup; } else { cmd_reply(CMD_LUA, caller, C_FAIL, _("Cannot read Freeciv script '%s'."), real_filename); ret = FALSE; - goto cleanup; } break; } -- 2.39.2