Skip to content

Commit 7c79f06

Browse files
committed
smb: fix failed connection not freeing memory
1 parent 8f0526f commit 7c79f06

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

libretro-common/vfs/vfs_implementation_smb.c

Lines changed: 31 additions & 19 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 j = 0; j < num_contexts; j++)
68+
{
69+
if(smb_context_pool[j])
70+
smb2_destroy_context(smb_context_pool[j]);
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,7 @@ static bool smb_init(void)
113129
if (!smb_context)
114130
{
115131
RARCH_ERR("[SMB] Failed to create context %d\n", i);
132+
reset(max_context_configured);
116133
return false;
117134
}
118135

@@ -183,8 +200,7 @@ static bool smb_init(void)
183200
{
184201
RARCH_ERR("[SMB] Failed to connect to %s/%s: %s (errno: %d) with context: %d\n",
185202
server, share, smb2_get_error(smb_context), error_no, i);
186-
smb2_destroy_context(smb_context);
187-
smb_context = NULL;
203+
reset(max_context_configured);
188204
return false;
189205
}
190206

@@ -208,7 +224,7 @@ void smb_close_context(int index)
208224
}
209225
}
210226

211-
/* Shutdown SMB context */
227+
/* Shutdown SMB context - called on exit */
212228
void smb_shutdown()
213229
{
214230
int i;
@@ -219,16 +235,9 @@ void smb_shutdown()
219235
RARCH_DBG("[SMB] Shutting down SMB client pool\n");
220236

221237
for (i = 0; i < max_context_configured; i++)
222-
{
223238
smb_close_context(i);
224-
}
225-
226-
free(smb_context_pool);
227-
smb_context_pool = NULL;
228239

229-
smb_initialized = false;
230-
next_context_index = 0;
231-
max_context_configured = 0;
240+
reset(max_context_configured);
232241
}
233242

234243
/* Build full SMB path from settings */
@@ -366,7 +375,7 @@ int64_t retro_vfs_file_read_smb(libretro_vfs_implementation_file *stream,
366375
int ret;
367376
struct smb2_context *ctx;
368377

369-
if (!stream || stream->smb_fh < 0)
378+
if (!smb_initialized || !stream || stream->smb_fh < 0)
370379
return -1;
371380

372381
ctx = (struct smb2_context *)(void *)(uintptr_t)stream->smb_ctx;
@@ -386,7 +395,7 @@ int64_t retro_vfs_file_write_smb(libretro_vfs_implementation_file *stream,
386395
int ret;
387396
struct smb2_context *ctx;
388397

389-
if (!stream || stream->smb_fh < 0)
398+
if (!smb_initialized || !stream || stream->smb_fh < 0)
390399
return -1;
391400

392401
ctx = (struct smb2_context *)(void *)(uintptr_t)stream->smb_ctx;
@@ -408,7 +417,7 @@ int64_t retro_vfs_file_seek_smb(libretro_vfs_implementation_file *stream,
408417
struct smb2_context *ctx;
409418
int64_t ret;
410419

411-
if (!stream || !stream->smb_ctx)
420+
if (!smb_initialized || !stream || !stream->smb_ctx)
412421
return -1;
413422

414423
/* fd holds the pointer returned by smb2_open(); */
@@ -447,7 +456,7 @@ int64_t retro_vfs_file_tell_smb(libretro_vfs_implementation_file *stream)
447456
struct smb2_context *ctx;
448457
int64_t ret;
449458

450-
if (!stream || !stream->smb_ctx)
459+
if (!smb_initialized || !stream || !stream->smb_ctx)
451460
return -1;
452461

453462
if (stream->smb_fh == 0 || stream->smb_fh == (intptr_t)-1)
@@ -549,7 +558,7 @@ struct smbc_dirent* retro_vfs_readdir_smb(smb_dir_handle* dh)
549558
struct smb2dirent *ent;
550559
static struct smbc_dirent result;
551560

552-
if (!dh)
561+
if (!smb_initialized || !dh)
553562
return NULL;
554563

555564
if (!dh->ctx || !dh->dir)
@@ -573,7 +582,7 @@ struct smbc_dirent* retro_vfs_readdir_smb(smb_dir_handle* dh)
573582

574583
int retro_vfs_closedir_smb(smb_dir_handle* dh)
575584
{
576-
if (!dh)
585+
if (!smb_initialized || !dh)
577586
return -1;
578587

579588
if (!dh->ctx || !dh->dir)
@@ -632,6 +641,9 @@ int retro_vfs_file_error_smb(libretro_vfs_implementation_file *stream)
632641
struct smb2_context *ctx;
633642
const char *err;
634643

644+
if (!!smb_initialized)
645+
return -1;
646+
635647
if (!stream || stream->smb_fh == 0 || stream->smb_fh == (intptr_t)-1)
636648
return -1;
637649

0 commit comments

Comments
 (0)