Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 19a4f5b

Browse files
committed
refacto: resolve memory shenanigan
1 parent 5ab4bce commit 19a4f5b

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

‎src/commands/dir.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
static bool is_path_allowed(char const *home, char const *newpath)
1818
{
19-
char const *real_newpath = realpath(newpath, NULL);
19+
char real_newpath[PATH_MAX];
2020

21-
if (real_newpath == NULL)
21+
if (realpath(newpath, real_newpath) == NULL)
2222
return false;
2323
return strncmp(home, real_newpath, strlen(home)) == 0;
2424
}
2525

2626
void cmd_pwd_handler(client_t *client, __attribute__((unused)) const char *_)
2727
{
28-
write_msg(client, "257", "%s", realpath(client->currPath, NULL));
28+
char path_buff[PATH_MAX];
29+
30+
realpath(client->currPath, path_buff);
31+
write_msg(client, "257", "%s", path_buff);
2932
}
3033

3134
void cmd_cdup_handler(client_t *client, __attribute__((unused)) const char *_)
@@ -48,19 +51,18 @@ static int check_for_path_errors(const char *args, const char *realpath_buff,
4851
void cmd_cwd_handler(client_t *client, const char *args)
4952
{
5053
char new_path[PATH_MAX] = {0};
51-
char *realpath_buff;
54+
char realpath_buff[PATH_MAX] = {0};
5255

5356
if (args[0] == '/') {
54-
realpath_buff = realpath(args, NULL);
57+
realpath(args, realpath_buff);
5558
} else {
5659
strcpy(new_path, client->currPath);
5760
strcat(new_path, "/");
5861
strcat(new_path, args);
59-
realpath_buff = realpath(new_path, NULL);
62+
realpath(new_path, realpath_buff);
6063
if (check_for_path_errors(args, realpath_buff, client) == -1)
6164
return;
6265
}
6366
strcpy(client->currPath, realpath_buff);
64-
free(realpath_buff);
6567
write_msg(client, "250", "Requested file action okay, completed.");
6668
}

‎src/commands/files.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ static void compute_dele_path(char *path_buffer, const char *given_path,
151151
void cmd_dele_handler(client_t *client, const char *args)
152152
{
153153
char path_buff[BUFSIZ] = {0};
154-
char *dele_path;
154+
char delete_path[BUFSIZ];
155155

156156
compute_dele_path(path_buff, args, client);
157-
dele_path = realpath(path_buff, NULL);
158-
if (!is_path_allowed(client->home, dele_path)) {
157+
if (!is_path_allowed(client->home, realpath(path_buff, delete_path))) {
159158
write_msg(client, "550", "Requested action not taken.");
160159
return;
161160
}
@@ -165,5 +164,4 @@ void cmd_dele_handler(client_t *client, const char *args)
165164
write_msg(client, "250",
166165
"250 Requested file action okay, completed.");
167166
}
168-
free(dele_path);
169167
}

‎src/myftp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int print_help(void)
3333

3434
int main(const int argc, const char *argv[])
3535
{
36-
server_t server = {0};
36+
static server_t server = {0};
3737

3838
setenv("LC_ALL", "C", 1);
3939
if (argc != 3)

‎src/server.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ static void searching_new_clients(server_t *server)
4747
client->control_fd = accept(server->server_fd,
4848
(struct sockaddr*) &client_addr, &client_len);
4949
init_client(client, server);
50-
strcpy(client->currPath, realpath(server->anonymous_default_path,
51-
NULL));
50+
realpath(server->anonymous_default_path, client->currPath);
5251
printf("[INFO] New client connection\n");
5352
write_welcome(client);
5453
}
@@ -119,15 +118,13 @@ void events_loop(server_t *server)
119118

120119
static int load_anon_home(server_t *server)
121120
{
122-
char *homepath;
121+
char homepath_buff[PATH_MAX];
123122

124-
homepath = realpath(server->anonymous_default_path, NULL);
125-
if (homepath == NULL) {
123+
if (realpath(server->anonymous_default_path, homepath_buff) == NULL) {
126124
fprintf(stderr, "myftp: invalid anonymous home path\n");
127125
return -1;
128126
}
129-
strcpy(server->anonymous_default_path, homepath);
130-
free(homepath);
127+
strcpy(server->anonymous_default_path, homepath_buff);
131128
printf("[INFO] Account 'Anonymous' home is: %s\n",
132129
server->anonymous_default_path);
133130
return 0;

0 commit comments

Comments
 (0)