Skip to content

Commit

Permalink
Remove fc_remove()
Browse files Browse the repository at this point in the history
QFile() provides an API that doesn't require dangerous explicit encoding
conversions.

See longturn#565.
  • Loading branch information
lmoureaux committed Jan 7, 2024
1 parent 242dfb2 commit 8371d45
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
22 changes: 11 additions & 11 deletions client/connectdlg_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QUuid>

#include <cstring>
#include <qglobal.h>

#ifdef FREECIV_MSWINDOWS
#include <windows.h>
Expand Down Expand Up @@ -50,7 +51,7 @@

bool server_quitting = false;

static char challenge_fullname[MAX_LEN_PATH];
static QString challenge_fullname{};
static bool client_has_hack = false;

int internal_server_port;
Expand Down Expand Up @@ -318,9 +319,9 @@ static void randomize_string(char *str, size_t n)
and then sends the string to the server. If the server can open
and read the string, then the client is given hack access.
*/
void send_client_wants_hack(const char *filename)
void send_client_wants_hack(const QString &filename)
{
if (filename[0] != '\0') {
if (!filename.isEmpty()) {
struct packet_single_want_hack_req req;
struct section_file *file;
auto sdir = freeciv_storage_dir();
Expand All @@ -334,9 +335,7 @@ void send_client_wants_hack(const char *filename)
}

QDir().mkpath(sdir);

fc_snprintf(challenge_fullname, sizeof(challenge_fullname), "%s/%s",
qUtf8Printable(sdir), filename);
challenge_fullname = sdir + QStringLiteral("/") + filename;

// generate an authentication token
randomize_string(req.token, sizeof(req.token));
Expand All @@ -345,7 +344,7 @@ void send_client_wants_hack(const char *filename)
secfile_insert_str(file, req.token, "challenge.token");
if (!secfile_save(file, challenge_fullname)) {
qCritical("Couldn't write token to temporary file: %s",
challenge_fullname);
qUtf8Printable(challenge_fullname));
}
secfile_destroy(file);

Expand All @@ -360,11 +359,12 @@ void send_client_wants_hack(const char *filename)
void handle_single_want_hack_reply(bool you_have_hack)
{
// remove challenge file
if (challenge_fullname[0] != '\0') {
if (fc_remove(challenge_fullname) == -1) {
qCritical("Couldn't remove temporary file: %s", challenge_fullname);
if (!challenge_fullname.isEmpty()) {
if (!QFile::remove(challenge_fullname)) {
qCritical("Couldn't remove temporary file: %s",
qUtf8Printable(challenge_fullname));
}
challenge_fullname[0] = '\0';
challenge_fullname.clear();
}

if (you_have_hack) {
Expand Down
2 changes: 1 addition & 1 deletion client/connectdlg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void client_kill_server(bool force);
bool is_server_running();
bool can_client_access_hack();

void send_client_wants_hack(const char *filename);
void send_client_wants_hack(const QString &filename);
void send_save_game(const char *filename);

void set_ruleset(const char *ruleset);
19 changes: 0 additions & 19 deletions utility/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,6 @@ FILE *fc_fopen(const char *filename, const char *opentype)
#endif // FREECIV_MSWINDOWS
}

/**
Wrapper function for remove() with filename conversion to local
encoding on Windows.
*/
int fc_remove(const char *filename)
{
#ifdef FREECIV_MSWINDOWS
int result;
char *filename_in_local_encoding =
internal_to_local_string_malloc(filename);

result = remove(filename_in_local_encoding);
free(filename_in_local_encoding);
return result;
#else // FREECIV_MSWINDOWS
return remove(filename);
#endif // FREECIV_MSWINDOWS
}

/**
Returns last error code.
*/
Expand Down
1 change: 0 additions & 1 deletion utility/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ int fc_strcoll(const char *str0, const char *str1);
int fc_stricoll(const char *str0, const char *str1);

FILE *fc_fopen(const char *filename, const char *opentype);
int fc_remove(const char *filename);

fc_errno fc_get_errno();
const char *fc_strerror(fc_errno err);
Expand Down

0 comments on commit 8371d45

Please sign in to comment.