Skip to content

Commit 1c1dd1b

Browse files
authored
smb: fix failed connection not freeing memory (#18604)
1 parent 8f0526f commit 1c1dd1b

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

libretro-common/vfs/vfs_implementation_smb.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static struct smb2_context *get_smb_context()
4444
if (!smb_initialized)
4545
return NULL;
4646

47-
if (max_context_configured == 0)
47+
if (!smb_context_pool || max_context_configured == 0)
4848
return NULL;
4949

5050
if (next_context_index < 0 || next_context_index >= max_context_configured)
@@ -62,8 +62,24 @@ static struct smb2_context *get_smb_context()
6262
return smb_context_pool[idx];
6363
}
6464

65+
void reset(unsigned num_contexts)
66+
{
67+
for (unsigned i = 0; i < num_contexts; i++)
68+
{
69+
if(smb_context_pool[i])
70+
smb2_destroy_context(smb_context_pool[i]);
71+
}
72+
73+
free(smb_context_pool);
74+
smb_context_pool = NULL;
75+
76+
smb_initialized = false;
77+
next_context_index = 0;
78+
max_context_configured = 0;
79+
}
80+
6581
/* Initialize SMB context */
66-
static bool smb_init(void)
82+
static bool smb_init()
6783
{
6884
settings_t *settings;
6985
char server[256];
@@ -113,6 +129,8 @@ static bool smb_init(void)
113129
if (!smb_context)
114130
{
115131
RARCH_ERR("[SMB] Failed to create context %d\n", i);
132+
smb2_destroy_context(smb_context);
133+
reset(max_context_configured);
116134
return false;
117135
}
118136

@@ -183,8 +201,9 @@ static bool smb_init(void)
183201
{
184202
RARCH_ERR("[SMB] Failed to connect to %s/%s: %s (errno: %d) with context: %d\n",
185203
server, share, smb2_get_error(smb_context), error_no, i);
204+
186205
smb2_destroy_context(smb_context);
187-
smb_context = NULL;
206+
reset(max_context_configured);
188207
return false;
189208
}
190209

@@ -208,7 +227,7 @@ void smb_close_context(int index)
208227
}
209228
}
210229

211-
/* Shutdown SMB context */
230+
/* Shutdown SMB context - called on exit */
212231
void smb_shutdown()
213232
{
214233
int i;
@@ -219,16 +238,9 @@ void smb_shutdown()
219238
RARCH_DBG("[SMB] Shutting down SMB client pool\n");
220239

221240
for (i = 0; i < max_context_configured; i++)
222-
{
223241
smb_close_context(i);
224-
}
225242

226-
free(smb_context_pool);
227-
smb_context_pool = NULL;
228-
229-
smb_initialized = false;
230-
next_context_index = 0;
231-
max_context_configured = 0;
243+
reset(max_context_configured);
232244
}
233245

234246
/* Build full SMB path from settings */
@@ -366,7 +378,7 @@ int64_t retro_vfs_file_read_smb(libretro_vfs_implementation_file *stream,
366378
int ret;
367379
struct smb2_context *ctx;
368380

369-
if (!stream || stream->smb_fh < 0)
381+
if (!smb_initialized || !stream || stream->smb_fh < 0)
370382
return -1;
371383

372384
ctx = (struct smb2_context *)(void *)(uintptr_t)stream->smb_ctx;
@@ -386,7 +398,7 @@ int64_t retro_vfs_file_write_smb(libretro_vfs_implementation_file *stream,
386398
int ret;
387399
struct smb2_context *ctx;
388400

389-
if (!stream || stream->smb_fh < 0)
401+
if (!smb_initialized || !stream || stream->smb_fh < 0)
390402
return -1;
391403

392404
ctx = (struct smb2_context *)(void *)(uintptr_t)stream->smb_ctx;
@@ -408,7 +420,7 @@ int64_t retro_vfs_file_seek_smb(libretro_vfs_implementation_file *stream,
408420
struct smb2_context *ctx;
409421
int64_t ret;
410422

411-
if (!stream || !stream->smb_ctx)
423+
if (!smb_initialized || !stream || !stream->smb_ctx)
412424
return -1;
413425

414426
/* fd holds the pointer returned by smb2_open(); */
@@ -447,7 +459,7 @@ int64_t retro_vfs_file_tell_smb(libretro_vfs_implementation_file *stream)
447459
struct smb2_context *ctx;
448460
int64_t ret;
449461

450-
if (!stream || !stream->smb_ctx)
462+
if (!smb_initialized || !stream || !stream->smb_ctx)
451463
return -1;
452464

453465
if (stream->smb_fh == 0 || stream->smb_fh == (intptr_t)-1)
@@ -549,7 +561,7 @@ struct smbc_dirent* retro_vfs_readdir_smb(smb_dir_handle* dh)
549561
struct smb2dirent *ent;
550562
static struct smbc_dirent result;
551563

552-
if (!dh)
564+
if (!smb_initialized || !dh)
553565
return NULL;
554566

555567
if (!dh->ctx || !dh->dir)
@@ -573,7 +585,7 @@ struct smbc_dirent* retro_vfs_readdir_smb(smb_dir_handle* dh)
573585

574586
int retro_vfs_closedir_smb(smb_dir_handle* dh)
575587
{
576-
if (!dh)
588+
if (!smb_initialized || !dh)
577589
return -1;
578590

579591
if (!dh->ctx || !dh->dir)
@@ -632,6 +644,9 @@ int retro_vfs_file_error_smb(libretro_vfs_implementation_file *stream)
632644
struct smb2_context *ctx;
633645
const char *err;
634646

647+
if (!!smb_initialized)
648+
return -1;
649+
635650
if (!stream || stream->smb_fh == 0 || stream->smb_fh == (intptr_t)-1)
636651
return -1;
637652

0 commit comments

Comments
 (0)