@@ -305,7 +305,8 @@ struct libretro_vfs_implementation_file
305305libretro_vfs_implementation_file *retro_vfs_file_open_impl (
306306 const char *path, unsigned mode, unsigned hints)
307307{
308- char *dirpath, *filename;
308+ char dirpath[PATH_MAX_LENGTH];
309+ char filename[PATH_MAX_LENGTH];
309310 wchar_t *dirpath_wide;
310311 wchar_t *filename_wide;
311312 Platform::String^ filename_str;
@@ -322,20 +323,18 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(
322323 if (PATH_CHAR_IS_SLASH (path[strlen (path) - 1 ]))
323324 return NULL ;
324325
325- dirpath = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
326- fill_pathname_basedir (dirpath, path, PATH_MAX_LENGTH);
326+ dirpath[0 ] = filename[0 ] = ' \0 ' ;
327+
328+ fill_pathname_basedir (dirpath, path, sizeof (dirpath));
327329 dirpath_wide = utf8_to_utf16_string_alloc (dirpath);
328330 windowsize_path (dirpath_wide);
329331 dirpath_str = ref new Platform::String (dirpath_wide);
330332 free (dirpath_wide);
331- free (dirpath);
332333
333- filename = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
334- fill_pathname_base (filename, path, PATH_MAX_LENGTH);
334+ fill_pathname_base (filename, path, sizeof (filename));
335335 filename_wide = utf8_to_utf16_string_alloc (filename);
336336 filename_str = ref new Platform::String (filename_wide);
337337 free (filename_wide);
338- free (filename);
339338
340339 retro_assert (!dirpath_str->IsEmpty () && !filename_str->IsEmpty ());
341340
@@ -577,8 +576,8 @@ int retro_vfs_file_remove_impl(const char *path)
577576/* TODO: this may not work if trying to move a directory */
578577int retro_vfs_file_rename_impl (const char *old_path, const char *new_path)
579578{
580- char * new_file_name;
581- char * new_dir_path;
579+ char new_file_name[PATH_MAX_LENGTH] ;
580+ char new_dir_path[PATH_MAX_LENGTH] ;
582581 wchar_t *new_file_name_wide;
583582 wchar_t *old_path_wide, *new_dir_path_wide;
584583 Platform::String^ old_path_str;
@@ -588,24 +587,23 @@ int retro_vfs_file_rename_impl(const char *old_path, const char *new_path)
588587 if (!old_path || !*old_path || !new_path || !*new_path)
589588 return -1 ;
590589
590+ new_file_name[0 ] = ' \0 ' ;
591+ new_dir_path [0 ] = ' \0 ' ;
592+
591593 old_path_wide = utf8_to_utf16_string_alloc (old_path);
592594 old_path_str = ref new Platform::String (old_path_wide);
593595 free (old_path_wide);
594596
595- new_dir_path = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
596- fill_pathname_basedir (new_dir_path, new_path, PATH_MAX_LENGTH);
597+ fill_pathname_basedir (new_dir_path, new_path, sizeof (new_dir_path));
597598 new_dir_path_wide = utf8_to_utf16_string_alloc (new_dir_path);
598599 windowsize_path (new_dir_path_wide);
599600 new_dir_path_str = ref new Platform::String (new_dir_path_wide);
600601 free (new_dir_path_wide);
601- free (new_dir_path);
602602
603- new_file_name = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
604- fill_pathname_base (new_file_name, new_path, PATH_MAX_LENGTH);
603+ fill_pathname_base (new_file_name, new_path, sizeof (new_file_name));
605604 new_file_name_wide = utf8_to_utf16_string_alloc (new_file_name);
606605 new_file_name_str = ref new Platform::String (new_file_name_wide);
607606 free (new_file_name_wide);
608- free (new_file_name);
609607
610608 retro_assert (!old_path_str->IsEmpty () && !new_dir_path_str->IsEmpty () && !new_file_name_str->IsEmpty ());
611609
@@ -667,10 +665,14 @@ int retro_vfs_mkdir_impl(const char *dir)
667665 Platform::String^ parent_path_str;
668666 Platform::String^ dir_name_str;
669667 wchar_t *dir_name_wide, *parent_path_wide;
670- char *dir_local, *tmp, *dir_name, *parent_path;
668+ char *dir_local, *tmp, *dir_name;
669+ char parent_path[PATH_MAX_LENGTH];
670+ char dir_name[PATH_MAX_LENGTH];
671671 if (!dir || !*dir)
672672 return -1 ;
673673
674+ dir_name[0 ] = ' \0 ' ;
675+
674676 /* If the path ends with a slash, we have to remove
675677 * it for basename to work */
676678 dir_local = strdup (dir);
@@ -679,20 +681,16 @@ int retro_vfs_mkdir_impl(const char *dir)
679681 if (PATH_CHAR_IS_SLASH (*tmp))
680682 *tmp = 0 ;
681683
682- dir_name = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
683- fill_pathname_base (dir_name, dir_local, PATH_MAX_LENGTH);
684+ fill_pathname_base (dir_name, dir_local, sizeof (dir_name));
684685 dir_name_wide = utf8_to_utf16_string_alloc (dir_name);
685686 dir_name_str = ref new Platform::String (dir_name_wide);
686687 free (dir_name_wide);
687- free (dir_name);
688688
689- parent_path = (char *)malloc (PATH_MAX_LENGTH * sizeof (char ));
690- fill_pathname_parent_dir (parent_path, dir_local, PATH_MAX_LENGTH);
689+ fill_pathname_parent_dir (parent_path, dir_local, sizeof (parent_path));
691690 parent_path_wide = utf8_to_utf16_string_alloc (parent_path);
692691 windowsize_path (parent_path_wide);
693692 parent_path_str = ref new Platform::String (parent_path_wide);
694693 free (parent_path_wide);
695- free (parent_path);
696694
697695 retro_assert (!dir_name_str->IsEmpty ()
698696 && !parent_path_str->IsEmpty ());
0 commit comments